views:

36

answers:

1

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  A: 

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

axtavt
An interesting idea ... but no. The bean that gets called twice is in exactly one .xml file. But I do have both the servlet and the dispatcher, and that's wrong, is it not?
bmargulies
@bmargulies: Show the `web.xml`, please.
axtavt
Prob now described in edit. contextConfigLocation included both the context file for security also also a file names springapp-servlet.xml. The dispatcher picked that up automatically, so I ended up with it being loaded *twice*.
bmargulies