title: Spring 3.2 Error when start tomcat server 7 - BeanFactory not initialized or already closed
body: <p>I have this problem when I start tomcat 7:</p>
  
  <pre><code>    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:1090)
  at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1064)
  at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:1010)
  at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:559)
  at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:143)
  at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4721)
  at org.apache.catalina.core.StandardContext$4.call(StandardContext.java:5423)
  at org.apache.catalina.core.StandardContext$4.call(StandardContext.java:5402)
  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
  at java.lang.Thread.run(Thread.java:680)
  </code></pre>
  
  <p>here is my application-context.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
      &lt;beans xmlns="http://www.springframework.org/schema/beans" 
   xmlns:aop="http://www.springframework.org/schema/aop" 
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
  xmlns:jee="http://www.springframework.org/schema/jee"
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  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.2.xsd    
                      http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                      http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                      http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.2.xsd"
  default-autowire="byName" default-lazy-init="true"&gt;
  
  &lt;context:annotation-config /&gt;
  &lt;context:spring-configured /&gt;
  &lt;context:component-scan base-package="com.xxx" annotation-config="true" /&gt;
  
  
  &lt;bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource"&gt;
      &lt; data source connection &gt;
  &lt;/bean&gt;
  
  
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="schemaUpdate" value="false" /&gt;
  
      &lt;property name="annotatedClasses"&gt;
          &lt;list&gt;
              ... list of classes mapped
          &lt;/list&gt;
      &lt;/property&gt;
  
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.Oracle10gDialect&lt;/prop&gt;
              &lt;prop key="hibernate.cache.use_second_level_cache"&gt;false&lt;/prop&gt;
              &lt;prop key="hibernate.cache.use_query_cache"&gt;false&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="auditServiceAOP" class="com.xxx.aop.AuditServiceAOP"&gt;
      &lt;constructor-arg ref="xxxSession" /&gt;
      &lt;constructor-arg ref="auditorshipService" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="auditLoginAOP" class="com.xxx.aop.AuditLoginAOP"&gt;
      &lt;constructor-arg ref="auditorshipService" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="exceptionLoggerAOP" class="com.xxx.aop.ExceptionLoggingAOP" /&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;aop:aspectj-autoproxy proxy-target-class="false" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>And this is my spring-security.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:security="http://www.springframework.org/schema/security"
     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.1.xsd
            http://www.springframework.org/schema/security  
  http://www.springframework.org/schema/security/spring-security-3.1.xsd"&gt;
  
  &lt;security:http auto-config="false" use-expressions="true" access-denied-page="/pages/login.xhtml"&gt;
      &lt;security:intercept-url pattern="/javax.faces.resource/**" access="permitAll" /&gt;
      &lt;security:intercept-url pattern="/pages/login.xhtml" access="permitAll" /&gt;
      &lt;security:intercept-url pattern="/pages/**" access="isFullyAuthenticated()" /&gt;
  
      &lt;security:logout invalidate-session="true" 
          logout-success-url="/pages/login.xhtml"
          logout-url="/j_spring_security_logout" 
          delete-cookies="JSESSIONID,SPRING_SECURITY_REMEMBER_ME_COOKIE" /&gt;
  
      &lt;security:form-login authentication-failure-url="/pages/login.xhtml?error=true" 
          default-target-url="/pages/home.xhtml" 
          login-page="/pages/login.xhtml" /&gt;
  &lt;/security:http&gt;
  
  &lt;security:authentication-manager alias="authenticationManager"&gt;
      &lt;security:authentication-provider ref="xxxAuthenticationProvider" /&gt;
  &lt;/security:authentication-manager&gt;
  </code></pre>
  
  <p></p>
  
  <p>The version of spring that I'm using is 3.2.2 and the spring security version is 3.1.4.</p>
  
  <p>I searched in stackoverflow but I didn't find anything that solve my problem.</p>
  
  <p>I appreciate the help! Thanks!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>web.xml:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app id="WebApp_ID" version="2.5"
      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_2_5.xsd"&gt;
  &lt;display-name&gt;xxx&lt;/display-name&gt;
  &lt;!-- Spring --&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, /WEB-INF/applicationContext-email.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;primefaces.THEME&lt;/param-name&gt;
      &lt;param-value&gt;ui-lightness&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;com.sun.faces.expressionFactory&lt;/param-name&gt;
      &lt;param-value&gt;org.jboss.el.ExpressionFactoryImpl&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;PrimeFaces FileUpload Filter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.primefaces.webapp.filter.FileUploadFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;PrimeFaces FileUpload Filter&lt;/filter-name&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&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.web.context.request.RequestContextListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;!-- Spring security --&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;dispatcher&gt;FORWARD&lt;/dispatcher&gt;
      &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
  &lt;/filter-mapping&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;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;mime-mapping&gt;
      &lt;extension&gt;jpg&lt;/extension&gt;
      &lt;mime-type&gt;image/jpeg&lt;/mime-type&gt;
  &lt;/mime-mapping&gt;
  &lt;mime-mapping&gt;
      &lt;extension&gt;png&lt;/extension&gt;
      &lt;mime-type&gt;image/x-png&lt;/mime-type&gt;
  &lt;/mime-mapping&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>