2015-04-20 145 views
2

春季(MVC)4.0.3,JAVA 1.7.0_71下,Tomcat 7.0.57,Windows 7的爲什麼被初始化兩次

我有一個Spring MVC應用程序,與2周的ApplicationContexts,該applicationContextRoot裝有彈簧豆:

AbstractAnnotationConfigDispatcherServletInitializer: protected Class<?>[] getRootConfigClasses { 
    return new Class<?>[] { AppConfig.class } 
} 

WebApplicationContext被加載。

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

了AppConfig類包含焦點bean的bean定義(Dropwizard的MetricRegistry

@Configuration 
public class AppConfig { 
    ... 
    @Bean 
    public MetricRegistry metricRegistry() { 
     MetricRegistry result = new MetricRegistry(); 
     LOG.info("testing", new Exception("trace me")); 
     configureReporters(result); 
     return result; 
} 

我看到trace me stacktrace叫了兩聲。堆棧跟蹤是不同的:

....zipped 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) [spring-context-4.0.3.RELEASE.jar:4.0.3.RELEASE] 
> at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) [spring-web-4.0.3.RELEASE.jar:4.0.3.RELEASE] 
> at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) [spring-web-4.0.3.RELEASE.jar:4.0.3.RELEASE] 
> at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) [spring-web-4.0.3.RELEASE.jar:4.0.3.RELEASE] 
> at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4992) [catalina.jar:7.0.57] 
> at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492) [catalina.jar:7.0.57] 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:7.0.57] 

對比(幾個msecs之後)。

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) [spring-context-4.0.3.RELEASE.jar:4.0.3.RELEASE] 
> at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658) [spring-webmvc-4.0.3.RELEASE.jar:4.0.3.RELEASE] 
> at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:530) [spring-webmvc-4.0.3.RELEASE.jar:4.0.3.RELEASE] 
> at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484) [spring-webmvc-4.0.3.RELEASE.jar:4.0.3.RELEASE] 
> at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) [spring-webmvc-4.0.3.RELEASE.jar:4.0.3.RELEASE] 
> at javax.servlet.GenericServlet.init(GenericServlet.java:158) [servlet-api.jar:3.0.FR] 
> at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284) [catalina.jar:7.0.57] 
> at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1090) [catalina.jar:7.0.57] 
> at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5231) [catalina.jar:7.0.57] 
> at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5518) [catalina.jar:7.0.57] 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:7.0.57] 

第一次豆在web.context.ContextLoader上下文而其在web.servlet.FrameworkServlet web.xml中創建的第二時間創建的是從安全約束開空的,則初始化經由的AbstractAnnotationConfigDispatcherServletInitializer

一個實例進行我不確定這種行爲的影響,但不是我所期望的(bean是singleton等)。如果我將每個@autowired註釋都刪除到這個bean,這種行爲仍然存在。我確實檢查了只有一個@componentscan(在WebMvcConfig中)

+1

聽起來好像你在'WebMvcConfig'組件掃描,但不排除'Configuration'類。例如'@ComponentScan(basePackages = {「mypackage」},excludeFilters = @ ComponentScan.Filter(type = FilterType.ANNOTATION,value = Configuration.class))' – beerbajay

+0

@beerbajay謝謝!如果你讓它成爲答案,我會獎勵它。 –

回答

2

聽起來好像你在WebMvcConfig的組件掃描,但不排除配置類。

你可以這樣做:

@ComponentScan(
    basePackages = {"mypackage"}, 
    excludeFilters = 
     @ComponentScan.Filter(
      type = FilterType.ANNOTATION, 
      value = Configuration.class 
     ) 
) 
相關問題