简体   繁体   中英

Spring ApplicationContext Running twice

I want to migrate Spring WebMvcConfigurerAdapter Configuration with Spring security. Now i get the problem that i get 2 ApplicationContextes and my Singelten Classes are Statefull. Here my web.xml and my FrontendConfig.

Please be indulgent with me. Im a newbie :)

    <?xml version="1.0" encoding="UTF-8"?>
<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">


  <context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
  </context-param>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      de.wiegand.mytransport.frontend.config.FrontendConfig
    </param-value>
  </context-param>

  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>
      org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
  </filter>

  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            </param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                de.wiegand.mytransport.frontend.config.FrontendConfig
            </param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

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


    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>


    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>

</web-app>

Configuration class

@Import({ StackEnvironment.class }) 
@EnableWebMvc
@ComponentScan({ "de.wiegand" })
@EnableTransactionManagement
@EnableAspectJAutoProxy
@Configuration
@ImportResource({ "/WEB-INF/spring-security.xml", "/WEB-INF/springsecurity.taglib.xml" })
public class FrontendConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        // 2DO: Wrong ??????
        registry.addResourceHandler("/assets/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/")
                .setCachePeriod(31556926);
        registry.addResourceHandler("/css/**").addResourceLocations("/css/")
                .setCachePeriod(31556926);
        registry.addResourceHandler("/img/**").addResourceLocations("/img/")
                .setCachePeriod(31556926);
        registry.addResourceHandler("/js/**").addResourceLocations("/js/")
                .setCachePeriod(31556926);
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/pages/");  //
        resolver.setSuffix(".xhtml");

        return resolver;
    }
}

THX

You've declared the same @Configuration class

 de.wiegand.mytransport.frontend.config.FrontendConfig

in both the ContextLoaderListener and the DispatcherServlet .

Both of these create an ApplicationContext . Since the DispatcherServlet context (child) has access to the beans in the ContextLoaderListener context (parent), you'll end up with duplicate beans or even initialization errors (depending on the config).

Your FrontendConfig is a WebMvcConfigurerAdapter and should therefore be the context that the DisaptcherServlet loads.

If you have no other context (application-wide contxt), remove your ContextLoaderListener declaration in the web.xml .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM