title: Cannot initialize context, already a root application context present
body: <p>In my last <a href="https://stackoverflow.com/questions/27708036/spring-secutiry-custom-xhtml-authenticationmanager-always-is-null">question</a> I got answer to create <code>ContextLoaderListener</code>. I'm searching how can I add <code>ContextLoaderListener</code> without <code>applicationContext.xml</code>. I found Quick Start - Hello Spring Security
  <a href="https://github.com/spring-projects/spring-security-javaconfig/blob/master/quickstart.md" rel="nofollow noreferrer">enter link description here</a>.
  After add I get error information</p>
  <p><code>Caused by: java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!</code></p>
  <p>But I dont config ContextLoaderListener in web.xml</p>
  <p>web.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot; xmlns:web=&quot;http://java.sun.com/xml/ns/javaee/web-      app_2_5.xsd&quot;
  xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;
  id=&quot;WebApp_ID&quot; version=&quot;2.5&quot; metadata-complete=&quot;true&quot;&gt;
  
  &lt;display-name&gt;PrimeFaces Web Application&lt;/display-name&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;primefaces.THEME&lt;/param-name&gt;
      &lt;param-value&gt;bootstrap&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;!-- Change to &quot;Production&quot; when you are ready to deploy --&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;org.jboss.jbossfaces.WAR_BUNDLES_JSF_IMPL&lt;/param-name&gt; 
      &lt;param-value&gt;true&lt;/param-value&gt; &lt;/context-param --&gt;
  &lt;!-- Welcome page --&gt;
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;faces/index.xhtml&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  
  &lt;!-- JSF 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;!-- Map these files with JSF --&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;/servlet-mapping&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&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;*.faces&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;*.xhtml&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  <p>My RootConfiguration</p>
  <pre><code>package com.pl.war.ldap.config;
  @Configuration
  @EnableAutoConfiguration
  @ComponentScan(&quot;com.pl.war.ldap.*&quot;)
  public class Application extends SpringBootServletInitializer {
  
  private static final Logger LOG = LoggerFactory.getLogger(AppConfig.class);
  
  
  @Bean
  public DataSource dataSource() {
      DataSource dataSource = null;
      JndiTemplate jndi = new JndiTemplate();
      try {
          dataSource = (DataSource) jndi
                  .lookup(&quot;java:jboss/datasources/test&quot;);
      } catch (NamingException e) {
          LOG.error(&quot;NamingException for java:jboss/datasources/test&quot;, e);
      }
      return dataSource;
  }
  
  
  @Bean
  LocalContainerEntityManagerFactoryBean entityManagerFactory(
          DataSource dataSource) {
  
      HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
      adapter.setDatabase(Database.POSTGRESQL);
      adapter.setGenerateDdl(true);
  
      LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
      factoryBean.setPackagesToScan(&quot;com.pl.war.model&quot;);
      factoryBean.setPersistenceXmlLocation(&quot;META-INF/persistence.xml&quot;);
      factoryBean.setJpaVendorAdapter(adapter);
      factoryBean.setDataSource(dataSource);
  
      return factoryBean;
  }
  
  
  protected Properties buildHibernateProperties() {
      Properties hibernateProperties = new Properties();
  
      hibernateProperties.setProperty(&quot;hibernate.dialect&quot;,
              &quot;org.hibernate.dialect.PostgreSQL9Dialect&quot;);
      hibernateProperties.setProperty(&quot;hibernate.show_sql&quot;, &quot;false&quot;);
      hibernateProperties.setProperty(&quot;hibernate.use_sql_comments&quot;, &quot;false&quot;);
      hibernateProperties.setProperty(&quot;hibernate.format_sql&quot;, &quot;false&quot;);
      hibernateProperties.setProperty(&quot;hibernate.hbm2ddl.auto&quot;, &quot;false&quot;);
  
      hibernateProperties.setProperty(&quot;hibernate.generate_statistics&quot;,
              &quot;false&quot;);
  
      hibernateProperties.setProperty(&quot;javax.persistence.validation.mode&quot;,
              &quot;none&quot;);
  
      // Audit History flags
      hibernateProperties.setProperty(
              &quot;org.hibernate.envers.store_data_at_delete&quot;, &quot;true&quot;);
      hibernateProperties.setProperty(
              &quot;org.hibernate.envers.global_with_modified_flag&quot;, &quot;true&quot;);
  
      return hibernateProperties;
  }
  
  @Bean
  public PlatformTransactionManager transactionManager() {
      return new JpaTransactionManager();
  }
  
  @Bean
  public TransactionTemplate transactionTemplate() {
      return new TransactionTemplate(transactionManager());
  }
  
  @Bean
  public HibernateExceptionTranslator hibernateExceptionTranslator() {
      return new HibernateExceptionTranslator();
  }
  @Bean
  public InternalResourceViewResolver viewResolver() {
      InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
      viewResolver.setSuffix(&quot;.xhtml&quot;);
      return viewResolver;
  }
  
  @Override
  protected SpringApplicationBuilder configure(
          SpringApplicationBuilder application) {
      return application.sources(Application.class);
  }
  }
  </code></pre>
  <p>My <code>getServletConfigClasses</code></p>
  <pre><code> @Configuration
   @EnableWebSecurity
   @EnableGlobalAuthentication
   public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
      // http.addFilter(casAuthenticationFilter());
      http.csrf().disable().authorizeRequests()
              .antMatchers(&quot;/home&quot;, &quot;/css/**&quot;, &quot;/**/*.css*&quot;, &quot;/&quot;).permitAll()
              .anyRequest().authenticated().and().formLogin()
              .loginPage(&quot;/login&quot;).permitAll().and().logout()
              .logoutUrl(&quot;/logout&quot;).invalidateHttpSession(true)
              .logoutSuccessUrl(&quot;/&quot;);
      // http.exceptionHandling().authenticationEntryPoint(
      // casAuthenticationEntryPoint());
  }
  
  @Override
  protected void configure(AuthenticationManagerBuilder auth)
          throws Exception {
      auth.inMemoryAuthentication().withUser(&quot;user&quot;).password(&quot;password&quot;)
              .roles(&quot;USER&quot;);
  }
  
  @Bean
  public AuthenticationManager authenticationManager() throws Exception {
      return super.authenticationManagerBean();
  
  }
  
  }
  </code></pre>
  <p>and <code>SpringMvcInitializer</code></p>
  <pre><code>    @Order(1)
      public class SpringMvcInitializer extends
      AbstractAnnotationConfigDispatcherServletInitializer {
  
  @Override
  protected Class&lt;?&gt;[] getRootConfigClasses() {
      return new Class[] { Application.class };
  }
  
  @Override
  protected Class&lt;?&gt;[] getServletConfigClasses() {
      return new Class[] { WebSecurityConfig.class };
  }
  
  @Override
  protected String[] getServletMappings() {
      return new String[] { &quot;/&quot; };
  
  }
  }
  
  
  public class MessageSecurityWebApplicationInitializer extends
      AbstractSecurityWebApplicationInitializer {
  
  }
  </code></pre>
  <p>I found a few similar issues</p>
  <p><code>https://stackoverflow.com/questions/22729725/why-this-spring-application-with-java-based-configuration-dont-work-properly</code></p>
  <p>or example</p>
  <p><code>http://www.mkyong.com/spring-security/spring-security-hello-world-annotation-example/</code></p>
  <p>But I can't resolve my problem</p>
  <p>Thanks for help</p>