在jetty中初始化spring beans 7.1.4
当我启动一个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.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
实际上,你有两个Spring应用程序上下文:
ContextLoaderListener
从context-param
指定的XML文件中加载contextConfigLocation
(或applicationContext.xml
,默认情况下)。 DispatcherServlet
从调度程序servlet的名为contextConfigLocation
(或<servletname>-servlet.xml
(默认情况下))的init-param
指定的XML文件中加载上下文。 前者是后者的家长背景。
所以,你不知何故在上下文中得到了同一个bean的定义(也许, contextConfigLocation
指向相同的XML文件)。
上一篇: initialization of spring beans in jetty 7.1.4
下一篇: JSR 303 Validator injection with Spring in Jersey classes