Unable to initialize context, application context root is already present

In my last question, I got an answer to create ContextLoaderListener

. I am looking for how to add ContextLoaderListener

without applicationContext.xml

. I found Quick Start - Hello Spring Security enter link here . After adding, I get error information

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!

But I am not setting up ContextLoaderListener in web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-      app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5" metadata-complete="true">

<display-name>PrimeFaces Web Application</display-name>
<context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>bootstrap</param-value>
</context-param>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<!-- context-param> <param-name>org.jboss.jbossfaces.WAR_BUNDLES_JSF_IMPL</param-name> 
    <param-value>true</param-value> </context-param -->
<!-- Welcome page -->
<welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>

<!-- JSF mapping -->
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Map these files with JSF -->
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

</web-app>

      

My RootConfiguration

package com.pl.war.ldap.config;
@Configuration
@EnableAutoConfiguration
@ComponentScan("com.pl.war.ldap.*")
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("java:jboss/datasources/test");
    } catch (NamingException e) {
        LOG.error("NamingException for java:jboss/datasources/test", 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("com.pl.war.model");
    factoryBean.setPersistenceXmlLocation("META-INF/persistence.xml");
    factoryBean.setJpaVendorAdapter(adapter);
    factoryBean.setDataSource(dataSource);

    return factoryBean;
}


protected Properties buildHibernateProperties() {
    Properties hibernateProperties = new Properties();

    hibernateProperties.setProperty("hibernate.dialect",
            "org.hibernate.dialect.PostgreSQL9Dialect");
    hibernateProperties.setProperty("hibernate.show_sql", "false");
    hibernateProperties.setProperty("hibernate.use_sql_comments", "false");
    hibernateProperties.setProperty("hibernate.format_sql", "false");
    hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "false");

    hibernateProperties.setProperty("hibernate.generate_statistics",
            "false");

    hibernateProperties.setProperty("javax.persistence.validation.mode",
            "none");

    // Audit History flags
    hibernateProperties.setProperty(
            "org.hibernate.envers.store_data_at_delete", "true");
    hibernateProperties.setProperty(
            "org.hibernate.envers.global_with_modified_flag", "true");

    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(".xhtml");
    return viewResolver;
}

@Override
protected SpringApplicationBuilder configure(
        SpringApplicationBuilder application) {
    return application.sources(Application.class);
}
}

      

My getServletConfigClasses

 @Configuration
 @EnableWebSecurity
 @EnableGlobalAuthentication
 public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    // http.addFilter(casAuthenticationFilter());
    http.csrf().disable().authorizeRequests()
            .antMatchers("/home", "/css/**", "/**/*.css*", "/").permitAll()
            .anyRequest().authenticated().and().formLogin()
            .loginPage("/login").permitAll().and().logout()
            .logoutUrl("/logout").invalidateHttpSession(true)
            .logoutSuccessUrl("/");
    // http.exceptionHandling().authenticationEntryPoint(
    // casAuthenticationEntryPoint());
}

@Override
protected void configure(AuthenticationManagerBuilder auth)
        throws Exception {
    auth.inMemoryAuthentication().withUser("user").password("password")
            .roles("USER");
}

@Bean
public AuthenticationManager authenticationManager() throws Exception {
    return super.authenticationManagerBean();

}

}

      

and SpringMvcInitializer

    @Order(1)
    public class SpringMvcInitializer extends
    AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[] { Application.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class[] { WebSecurityConfig.class };
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/" };

}
}


public class MessageSecurityWebApplicationInitializer extends
    AbstractSecurityWebApplicationInitializer {

}

      

I found several similar problems

https://stackoverflow.com/questions/22729725/why-this-spring-application-with-java-based-configuration-dont-work-properly

or example

http://www.mkyong.com/spring-security/spring-security-hello-world-annotation-example/

But I cannot solve my problem.

thanks for the help

+3


source to share





All Articles