Cannot initialize context, already a root application context present

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

In my last question I got answer to create ContextLoaderListener. I'm searching how can I add ContextLoaderListener without applicationContext.xml. I found Quick Start - Hello Spring Security enter link description here. After add 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 dont config 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 a few similar issues

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 can't resolve my problem

Thanks for help

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!