title: java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
body: <p>I need to add Spring Security with customized login page and connection to database to my Spring MVC project. I am receiving following error message, based on answers of other questions, I tried to change the code, for example I changed my Spring Security Schema version to 4.0 but the code returns following error:</p>
  
  <p><strong>Changed schema to 4.0</strong></p>
  
  <pre><code>http://www.springframework.org/schema/security/spring-security-4.0.xsd
  </code></pre>
  
  <p><strong>Error</strong></p>
  
  <pre><code>Cannot initialize context because there is already a root application context 
  present - check whether you have multiple ContextLoader* definitions in your 
  web.xml!
  </code></pre>
  
  <p><strong>My code</strong></p>
  
  <p><strong>my-security.xml</strong></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.2.xsd
      http://www.springframework.org/schema/security
      http://www.springframework.org/schema/security/spring-security-3.2.xsd'&gt;
  
      &lt;beans:import resource="security-db.xml" /&gt;
  
      &lt;http auto-config="true" access-denied-page="/notFound.jsp"
          use-expressions="true"&gt;
          &lt;intercept-url pattern="/" access="permitAll" /&gt;
      &lt;/http&gt;
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app 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"
      version="3.0"&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.apache.tiles.extras.complete.CompleteAutoloadTilesListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;my&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&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;my&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/config/my-security.xml
          &lt;/param-value&gt;
      &lt;/context-param&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>security-db.xml</strong></p>
  
  <pre><code>&lt;beans xmlns="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.1.xsd"&gt;
  
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost/dbproj" /&gt;
          &lt;property name="username" value="jack" /&gt;
          &lt;property name="password" value="jack" /&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>