无法初始化上下文,已存在根应用程序上下文

时间:2015-01-04 13:28:32

标签: spring spring-mvc spring-security

在我的上一篇question中,我得到了创建ContextLoaderListener的答案。我正在搜索如何在没有ContextLoaderListener的情况下添加applicationContext.xml。我找到了快速入门 - Hello Spring Security enter link description here。 添加后我得到错误信息

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!

但我没有在web.xml中配置ContextLoaderListener

的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>

我的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);
}
}

我的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();

}

}

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 {

}

我发现了一些类似的问题

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

或示例

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

但我无法解决我的问题

感谢您的帮助

0 个答案:

没有答案