Spring ApplicationContext运行两次

时间:2013-08-31 16:19:45

标签: java spring-mvc applicationcontext

我想使用Spring安全性迁移Spring WebMvcConfigurerAdapter配置。 现在我遇到了问题,我得到了2个ApplicationContextes,而我的Singelten类是Statefull。 这是我的web.xml和我的FrontendConfig。

请放纵我。我是新手:))

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

配置类

@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

1 个答案:

答案 0 :(得分:0)

您已声明相同的@Configuration

 de.wiegand.mytransport.frontend.config.FrontendConfig
<{1}}和ContextLoaderListener中的

这两个都创建了DispatcherServlet。由于ApplicationContext上下文(子)可以访问DispatcherServlet上下文(父)中的bean,因此最终会出现重复的bean甚至初始化错误(取决于配置)。

您的ContextLoaderListenerFrontendConfig,因此应该是WebMvcConfigurerAdapter加载的上下文。

如果您没有其他上下文(应用程序范围的续),请删除DisaptcherServlet中的ContextLoaderListener声明。