lundi 20 avril 2015

Why gets the spring bean initialized twice

Spring (mvc) 4.0.3, java 1.7.0_71, tomcat 7.0.57, windows 7

I have a Spring MVC application, with 2 applicationContexts, the applicationContextRoot is loaded with:

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

The WebApplicationContext is loaded with.

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

The AppConfig class contains the bean definition for the Bean in focus (Dropwizard's MetricRegistry)

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

I see the trace me stacktrace called twice. The stacktraces are different:

....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]

versus (few msecs later).

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]

The first time the bean is created in the web.context.ContextLoader context whereas 2nd time its created in the web.servlet.FrameworkServlet web.xml is empty apart from security constraints, the initialization is done via an instance of AbstractAnnotationConfigDispatcherServletInitializer

I'm not sure of the impact of this behaviour, but its not what I would expect (beans being singleton etc). Also this behavior remains if I take out every @autowired annotation to this bean. I did check that there is only one @componentscan (in WebMvcConfig)

Aucun commentaire:

Enregistrer un commentaire