码头中春豆的双重初始化7.1.4

时间:2010-07-12 17:29:24

标签: spring spring-mvc

当我启动一个在jetty中具有弹簧配置的webapp时,我看到两个初始化调用(@PostConstruct方法)。 web.xml声明一个ContextLoaderListener并提供contextConfigLocation。

这似乎不对。 (春季3.0.3)。我有没有做过什么?

修改

我的web.xml调用DispatcherServlet和org.springframework.web.context.ContextLoaderListener。如果我删除后者,我会停止看两次初始化 - 但是我的应用程序在jetty-maven-plugin中失败了

No WebApplicationContext found: no ContextLoaderListener registered?

编辑2

知道了。调度servlet将按常规名称(* -servlet.xml)找到一些上下文文件,因此在contextConfigLocation中再次调用该文件导致它被加载两次。

堆栈跟踪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

堆栈跟踪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 个答案:

答案 0 :(得分:2)

实际上,您有两个Spring应用程序上下文:

  • ContextLoaderListener从名为context-param的{​​{1}}指定的XML文件加载的上下文(默认情况下为contextConfigLocation)。
  • applicationContext.xml从调度程序servlet DispatcherServlet指定的init-param指定的XML文件加载的上下文(默认情况下为contextConfigLocation)。

前者是后者的父背景。

所以,你在两个上下文中都得到了同一个bean的定义(也许,<servletname>-servlet.xml都指向相同的XML文件。)