代码之家  ›  专栏  ›  技术社区  ›  bmargulies

jetty7.1.4中springbeans的双重初始化

  •  0
  • bmargulies  · 技术社区  · 14 年前

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

    这似乎是错的(弹簧3.0.3)。我做了什么?

    编辑

    我的web.xml同时调用DispatcherServlet和org.springframework.web.context.ContextLoaderListener。如果我删除了后者,我就看不到两个初始化了——但是我的应用程序在jetty maven插件中失败了

    No WebApplicationContext found: no ContextLoaderListener registered?
    

    :

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

    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 回复  |  直到 14 年前
        1
  •  2
  •   axtavt    14 年前

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

    • 上下文加载者 ContextLoaderListener 从指定的XML文件 context-param 命名 contextConfigLocation (或 applicationContext.xml
    • 上下文加载者 DispatcherServlet init-param 上下文配置位置 <servletname>-servlet.xml 默认情况下)。

    前者是后者的父上下文。

    因此,您不知何故在两种上下文中都得到了相同bean的定义(也许,两者都是) 上下文配置位置 指向相同的XML文件)。