double-initialization of spring beans in jetty 7.1

2019-09-18 05:19发布

When I start a webapp that has a spring configuration in jetty, I see two calls to initialization (@PostConstruct methods). web.xml declares a ContextLoaderListener and provides contextConfigLocation.

This seems wrong. (spring 3.0.3). Is there something I've done?

EDIT

My web.xml calls out both the DispatcherServlet and the org.springframework.web.context.ContextLoaderListener. If I remove the later, I stop seeing two initializations --- but my app fails in the jetty-maven-plugin with

No WebApplicationContext found: no ContextLoaderListener registered?

EDIT 2:

Got it. The dispatch servlet will find some context files by conventional name (*-servlet.xml), and so calling that file out again in the contextConfigLocation led to it being loaded twice.

Stack trace 1:

Segmenter.initialize() line: 63 
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39  
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25  
Method.invoke(Object, Object...) line: 597  
InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(Object) line: 340    
InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(Object, String) line: 293    
CommonAnnotationBeanPostProcessor(InitDestroyAnnotationBeanPostProcessor).postProcessBeforeInitialization(Object, String) line: 130 
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).applyBeanPostProcessorsBeforeInitialization(Object, String) line: 394    
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).initializeBean(String, Object, RootBeanDefinition) line: 1405    
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean(String, RootBeanDefinition, Object[]) line: 519 
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean(String, RootBeanDefinition, Object[]) line: 456   
AbstractBeanFactory$1.getObject() line: 291 
DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton(String, ObjectFactory) line: 222  
DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 288    
DefaultListableBeanFactory(AbstractBeanFactory).getBean(String) line: 190   
DefaultListableBeanFactory.preInstantiateSingletons() line: 574 
XmlWebApplicationContext(AbstractApplicationContext).finishBeanFactoryInitialization(ConfigurableListableBeanFactory) line: 895 
XmlWebApplicationContext(AbstractApplicationContext).refresh() line: 425    
ContextLoaderListener(ContextLoader).createWebApplicationContext(ServletContext, ApplicationContext) line: 276  
ContextLoaderListener(ContextLoader).initWebApplicationContext(ServletContext) line: 197    
ContextLoaderListener.contextInitialized(ServletContextEvent) line: 47  
JettyWebAppContext(ContextHandler).startContext() line: 645 
JettyWebAppContext(ServletContextHandler).startContext() line: 200  
JettyWebAppContext(WebAppContext).startContext() line: 995  
JettyWebAppContext(ContextHandler).doStart() line: 588  
JettyWebAppContext(WebAppContext).doStart() line: 381   
JettyWebAppContext.doStart() line: 114  
JettyWebAppContext(AbstractLifeCycle).start() line: 55  
ContextHandlerCollection(HandlerCollection).doStart() line: 165 
ContextHandlerCollection.doStart() line: 162    
ContextHandlerCollection(AbstractLifeCycle).start() line: 55    
HandlerCollection.doStart() line: 165   
HandlerCollection(AbstractLifeCycle).start() line: 55   
JettyServer(HandlerWrapper).doStart() line: 92  
JettyServer(Server).doStart() line: 228 
JettyServer.doStart() line: 67

Stack trace 2:

Segmenter.initialize() line: 63 
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39  
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25  
Method.invoke(Object, Object...) line: 597  
InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(Object) line: 340    
InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(Object, String) line: 293    
CommonAnnotationBeanPostProcessor(InitDestroyAnnotationBeanPostProcessor).postProcessBeforeInitialization(Object, String) line: 130 
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).applyBeanPostProcessorsBeforeInitialization(Object, String) line: 394    
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).initializeBean(String, Object, RootBeanDefinition) line: 1405    
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean(String, RootBeanDefinition, Object[]) line: 519 
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean(String, RootBeanDefinition, Object[]) line: 456   
AbstractBeanFactory$1.getObject() line: 291 
DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton(String, ObjectFactory) line: 222  
DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 288    
DefaultListableBeanFactory(AbstractBeanFactory).getBean(String) line: 190   
DefaultListableBeanFactory.preInstantiateSingletons() line: 574 
XmlWebApplicationContext(AbstractApplicationContext).finishBeanFactoryInitialization(ConfigurableListableBeanFactory) line: 895 
XmlWebApplicationContext(AbstractApplicationContext).refresh() line: 425    
DispatcherServlet(FrameworkServlet).createWebApplicationContext(ApplicationContext) line: 442   
DispatcherServlet(FrameworkServlet).createWebApplicationContext(WebApplicationContext) line: 458    
DispatcherServlet(FrameworkServlet).initWebApplicationContext() line: 339   
DispatcherServlet(FrameworkServlet).initServletBean() line: 306 
DispatcherServlet(HttpServletBean).init() line: 127 
DispatcherServlet(GenericServlet).init(ServletConfig) line: 241 
ServletHolder.initServlet() line: 421   
ServletHolder.doStart() line: 245   
ServletHolder(AbstractLifeCycle).start() line: 55   
ServletHandler(ServletHandler).initialize() line: 691   
JettyWebAppContext(ServletContextHandler).startContext() line: 204  
JettyWebAppContext(WebAppContext).startContext() line: 995  
JettyWebAppContext(ContextHandler).doStart() line: 588  
JettyWebAppContext(WebAppContext).doStart() line: 381   
JettyWebAppContext.doStart() line: 114  
JettyWebAppContext(AbstractLifeCycle).start() line: 55  
ContextHandlerCollection(HandlerCollection).doStart() line: 165 
ContextHandlerCollection.doStart() line: 162    
ContextHandlerCollection(AbstractLifeCycle).start() line: 55    
HandlerCollection.doStart() line: 165   
HandlerCollection(AbstractLifeCycle).start() line: 55   
JettyServer(HandlerWrapper).doStart() line: 92  
JettyServer(Server).doStart() line: 228 
JettyServer.doStart() line: 67

1条回答
太酷不给撩
2楼-- · 2019-09-18 05:48

Actally, you have two Spring application contexts:

  • Context loaded by ContextLoaderListener from the XML files specified by context-param named contextConfigLocation (or applicationContext.xml by default).
  • Context loaded by DispatcherServlet from the XML files specified by the dispatcher servlet's init-param named contextConfigLocation (or <servletname>-servlet.xml by default).

The former is a parent context of the latter.

So, you somehow got a definitions of the same bean in both contexts (perhaps, both contextConfigLocations point to the same XML files).

查看更多
登录 后发表回答