title: Getting Error like "java.lang.UnsupportedOperationException: Section 4.4 of the Servlet 3.0 specification does not permit this method to be called"
body: <p>I am migrating Spring XML based configuration to Java based configuration. The below class which implements <strong>HttpSessionListener</strong> and <strong>ApplicationContextAware</strong> was working fine with XML configuration. But with Java configuration, getting below error while starting the Tomcat server.</p>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionListener' defined in com.grc.config.WebConfig: Initialization of bean failed; nested exception is java.lang.UnsupportedOperationException: Section 4.4 of the Servlet 3.0 specification does not permit this method to be called from a ServletContextListener that was not defined in web.xml, a web-fragment.xml file nor annotated with @WebListener
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:584)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
  </code></pre>
  
  <p><strong>Class:</strong></p>
  
  <pre><code>public class SessionListener implements HttpSessionListener,ApplicationContextAware {
  
      private static final Logger logger = ExtendedLoggerFactory.getLogger(Info.HTTP_LOGGER);
  
      @Autowired
      private JdbcHSqlRepository jdbcHSqlRepository;
  
      @Autowired
      private LoginEventRepository loginEventRepo;
  
      @SuppressWarnings("unused") 
      private ApplicationContext applicationContext;
  
      @Override 
      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 
          this.applicationContext = applicationContext;
  
          if(applicationContext instanceof WebApplicationContext) {
              ((WebApplicationContext)applicationContext).getServletContext().addListener(this); 
          } 
      }
  
      @Override 
      public void sessionCreated(HttpSessionEvent httpSessionEvent) {
          httpSessionEvent.getSession().setMaxInactiveInterval(5*60); 
      }
  
      @Override
      public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
          HttpSession httpSession = httpSessionEvent.getSession();
  
          //mark user login event as looged out
          LoginEventVO event = (LoginEventVO)httpSession.getAttribute(Constants.LOGIN_EVENT);
          if(event != null) {
              event.setActive(false);
  
              loginEventRepo.saveAndFlush(event.transform());
          }
  
          logger.debug("Session destroyed : " + httpSession.getId());
          jdbcHSqlRepository.dropSessionTables(httpSession);
      }
  }
  </code></pre>
  
  <p>When I tried without implementing <strong>ApplicationContextAware</strong>, able to start the server without any error, but getting below error when sessionDestroyed method is executed.</p>
  
  <p>Error:</p>
  
  <pre><code>java.lang.NullPointerException: null
      at com.grc.riskanalysis.interceptor.SessionListener.sessionDestroyed(SessionListener.java:45)
      at org.apache.catalina.session.StandardSession.expire(StandardSession.java:801)
      at org.apache.catalina.session.StandardSession.isValid(StandardSession.java:659)
      at org.apache.catalina.session.ManagerBase.processExpires(ManagerBase.java:573)
      at org.apache.catalina.session.ManagerBase.backgroundProcess(ManagerBase.java:558)
      at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5474)
      at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1398)
      at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1402)
      at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1402)
      at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1370)
      at java.lang.Thread.run(Thread.java:748)
  </code></pre>
  
  <p>Is there anything I missed out with Java configuration?</p>
  
  <p>After @lucid inputs, manually injected required dependencies in sessionDestroyed() method and it worked fine.</p>
  
  <pre><code>@Override
      public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
          HttpSession httpSession = httpSessionEvent.getSession();
  
          //mark user login event as looged out
          LoginEventVO event = (LoginEventVO)httpSession.getAttribute(Constants.LOGIN_EVENT);
          ApplicationContext ctx = WebApplicationContextUtils.
                  getWebApplicationContext(httpSession.getServletContext());
          if(event != null) {
              event.setActive(false);
              if(loginEventRepo == null) {
                  this.loginEventRepo = (LoginEventRepository) 
                          ctx.getBean("loginEventRepository");
              }
              loginEventRepo.saveAndFlush(event.transform());
          }
  
          logger.debug("Session destroyed : " + httpSession.getId());
          if(jdbcHSqlRepository == null) {
              this.jdbcHSqlRepository = (JdbcHSqlRepository) 
                      ctx.getBean("jdbcHSqlRepository");
          }
  
          jdbcHSqlRepository.dropSessionTables(httpSession);
      }
  </code></pre>