title: BeanFactory not initialized or already closed - call 'refresh' before accessing beans
body: <p>I'm trying to add spring security to a regular JSF application. After repeated tries and I'm failing with the following error on tomcat bring-up.</p>
  
  <p>Here is the entire stack trace. Any help is appreciated.</p>
  
  <p><strong>EDIT:</strong> upon further tests, I notice the following stack trace, before the second set of SEVERE exception about BeanFactory not initialized or already closed</p>
  
  <pre><code>SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderList
  ener
  java.lang.NoSuchMethodError: org.springframework.web.context.support.XmlWebApplicationContext.getEnvironment()Lorg/springframework/
  core/env/ConfigurableEnvironment;
          at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:87)
          at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicat
  ionContext.java:130)
          at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:46
  7)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397)
          at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
          at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
          at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
          at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4205)
          at org.apache.catalina.core.StandardContext.start(StandardContext.java:4704)
          at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
          at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
          at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943)
          at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)
          at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)
          at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1315)
          at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
          at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
          at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1061)
          at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
          at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
          at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
          at org.apache.catalina.core.StandardService.start(StandardService.java:525)
          at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
          at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          at java.lang.reflect.Method.invoke(Method.java:597)        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
          at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
  
  After this error, the mojarra 2.1.4 is initialized before the BeanFactory exception shows up
  
  SEVERE: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener
  java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
          at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:171)
          at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1048)
          at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1022)
          at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:970)
          at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:556)
          at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:142)
          at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4245)
          at org.apache.catalina.core.StandardContext.stop(StandardContext.java:4886)
          at org.apache.catalina.core.StandardContext.start(StandardContext.java:4750)
          at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
          at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
          at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
          at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943)
          at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)
          at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)
          at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
          at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
          at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
          at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
          at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
          at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
          at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
          at org.apache.catalina.core.StandardService.start(StandardService.java:525)
          at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
          at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          at java.lang.reflect.Method.invoke(Method.java:597)
          at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
          at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
  </code></pre>
  
  <p>my web.xml is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt;
  
      &lt;context-param&gt;
        &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
        &lt;param-value&gt;
           /WEB-INF/applicationContext.xml
           /WEB-INF/applicationContext-security.xml
         &lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.PROJECT_STAGE&lt;/param-name&gt;
          &lt;param-value&gt;Development&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
      &lt;param-name&gt;javax.faces.VALIDATE_EMPTY_FIELDS&lt;/param-name&gt;
      &lt;param-value&gt;false&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
      &lt;param-name&gt;javax.faces.FACELETS_SKIP_COMMENTS&lt;/param-name&gt;
      &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;com.icesoft.faces.debugDOMUpdate&lt;/param-name&gt;
          &lt;param-value&gt;false&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;com.icesoft.faces.concurrentDOMViews&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;com.icesoft.faces.synchronousUpdate&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Faces Servlet --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
      &lt;!-- &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; --&gt;
      &lt;/servlet&gt;
      &lt;servlet&gt;
      &lt;servlet-name&gt;Resource Servlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.icesoft.faces.webapp.CompatResourceServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  &lt;!-- Extension Mapping --&gt;
      &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt;
      &lt;url-pattern&gt;*.jsf&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/icefaces/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;session-config&gt;
      &lt;session-timeout&gt;90&lt;/session-timeout&gt;
      &lt;/session-config&gt;
  
  
      &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Resource Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/xmlhttp/*&lt;/url-pattern&gt;
      &lt;url-pattern&gt;/resources/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;filter&gt;
          &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.security.web.session.HttpSessionEventPublisher&lt;/listener-class&gt;
      &lt;/listener&gt;
  
  
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>And my applicationContext-security.xml is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/security"
               xmlns:beans="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/security
             http://www.springframework.org/schema/security/spring-security-3.0.xsd"&gt;
  
  &lt;!--  key configuration here is an entry point to be used by security intercepts --&gt;
  &lt;http realm="Sample Realm" entry-point-ref="authenticationEntryPoint" auto-config="false"&gt;
  
      &lt;custom-filter ref="sessionManagementFilter" before="SESSION_MANAGEMENT_FILTER" /&gt;
      &lt;!-- any role that is used to protect a directory, can be multiples --&gt;
      &lt;intercept-url pattern='/secure/**' access='ROLE_READER' /&gt;
  
      &lt;!-- enable form login to use UsernamePasswordAuthenticationFilter [/j_spring_security_check] --&gt;
      &lt;form-login login-page="/general/logins/htmlLogin.faces"  
                      authentication-failure-url="/general/logins/loginFailed.jsf"/&gt;
  
      &lt;!-- logout page uses the default LogoutFilter, no changes are needed as IT accepts a GET call... --&gt;
      &lt;!-- here is an example logout link:
      &lt;a href="#{request.contextPath}/j_spring_security_logout"&gt;Logout&lt;/a&gt; --&gt;
          &lt;logout logout-url="/j_spring_security_logout"
                  logout-success-url="/general/main.jsf"
                  invalidate-session="true"/&gt;
      &lt;/http&gt;
  
      &lt;beans:bean id="authenticationEntryPoint"
          class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"&gt;
          &lt;beans:property name="loginFormUrl" value="/general/logins/login.jsf" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;!-- test with this before you hook up your LDAP or other Authentication Manager --&gt;
      &lt;authentication-manager alias="authenticationManager"&gt;
          &lt;authentication-provider&gt;
              &lt;user-service&gt;
                  &lt;user name="joe.blow@gr.am" password="pass1234" authorities="ROLE_READER"/&gt;
                  &lt;user name="tester@gr.com" password="pass5678" authorities="ROLE_READER"/&gt;
              &lt;/user-service&gt;
          &lt;/authentication-provider&gt;
      &lt;/authentication-manager&gt;
  
      &lt;beans:bean id="sessionManagementFilter" class="org.springframework.security.web.session.SessionManagementFilter"&gt;
          &lt;beans:constructor-arg name="securityContextRepository" ref="httpSessionSecurityContextRepository" /&gt;
          &lt;beans:property name="invalidSessionUrl" value="/general/logins/sessionExpired.jsf" /&gt;
          &lt;!-- this permits redirection to session timeout page from javascript/ajax or http --&gt;
          &lt;beans:property name="redirectStrategy" ref="jsfRedirectStrategy" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;beans:bean id="jsfRedirectStrategy" class="com.palm.safe.spring.security.JsfRedirectStrategy"/&gt;
      &lt;beans:bean id="httpSessionSecurityContextRepository" class="org.springframework.security.web.context.HttpSessionSecurityContextRepository"/&gt;
  
  &lt;/beans:beans&gt;
  </code></pre>