我在JBoss中部署了一个基于spring的应用程序。我有一些带有@PostConstruct方法的bean。当我启动应用程序时,这些bean被初始化两次。似乎正在加载两个不同的上下文。我可以在日志中看到以下内容:
2014-04-09 16:56:49 INFO [ServerService Thread Pool -- 58] web.context.ContextLoader:285 - Root WebApplicationContext: initialization started
2014-04-09 16:56:49 INFO [ServerService Thread Pool -- 58] context.support.AbstractApplicationContext:513 - Refreshing Root WebApplicationContext: startup date [Wed Apr 09 16:56:49 EST 2014]; root of context hierarchy
2014-04-09 16:56:49 INFO [ServerService Thread Pool -- 58] factory.xml.XmlBeanDefinitionReader:316 - Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
.........
2014-04-09 16:56:49 INFO [ServerService Thread Pool -- 58] com.router.SingleSocketMessageTransporter:150 - START : Initializing MessageTransporter [Host: localhost, Port: 8787]
2014-04-09 16:56:49 INFO [ServerService Thread Pool -- 58] com.router.SocketFactoryImpl:54 - Sockect connection established with host localhost at port 8787
2014-04-09 16:56:49 INFO [ServerService Thread Pool -- 58] com.router.SingleSocketMessageTransporter:165 - END : Successfully initialized MessageTransporter [Host: localhost, Port: 8787]
2014-04-09 16:56:49 DEBUG [ServerService Thread Pool -- 58] com.router.MessageBrokerImpl:157 - START: Message broker initialization.
2014-04-09 16:56:49 DEBUG [ServerService Thread Pool -- 58] com.router.MessageBrokerImpl:182 - END: Message Broker Initialization
2014-04-09 16:56:50 INFO [ServerService Thread Pool -- 58] com.service.NetworkMgmtServiceImp:115 - Network Post construct.
.........
2014-04-09 16:56:50 INFO [ServerService Thread Pool -- 58] web.context.ContextLoader:325 - Root WebApplicationContext: initialization completed in 1748 ms
.........
2014-04-09 16:56:50 INFO [ServerService Thread Pool -- 58] web.servlet.FrameworkServlet:479 - FrameworkServlet 'mvc-dispatcher': initialization started
2014-04-09 16:56:50 INFO [ServerService Thread Pool -- 58] context.support.AbstractApplicationContext:513 - Refreshing WebApplicationContext for namespace 'mvc-dispatcher-servlet': startup date [Wed Apr 09 16:56:50 EST 2014]; parent: Root WebApplicationContext
2014-04-09 16:56:50 INFO [ServerService Thread Pool -- 58] factory.xml.XmlBeanDefinitionReader:316 - Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
..........
2014-04-09 16:56:51 INFO [ServerService Thread Pool -- 58] com.router.SingleSocketMessageTransporter:150 - START : Initializing MessageTransporter [Host: localhost, Port: 8787]
2014-04-09 16:56:51 INFO [ServerService Thread Pool -- 58] com.router.SocketFactoryImpl:54 - Sockect connection established with host localhost at port 8787
2014-04-09 16:56:51 INFO [ServerService Thread Pool -- 58] com.router.SingleSocketMessageTransporter:165 - END : Successfully initialized MessageTransporter [Host: localhost, Port: 8787]
2014-04-09 16:56:51 DEBUG [ServerService Thread Pool -- 58] com.router.MessageBrokerImpl:157 - START: Message broker initialization.
2014-04-09 16:56:51 DEBUG [ServerService Thread Pool -- 58] com.router.MessageBrokerImpl:182 - END: Message Broker Initialization
2014-04-09 16:56:51 INFO [ServerService Thread Pool -- 58] com.service.NetworkMgmtServiceImp:115 - Network Post construct.
我的web.xml配置是:
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
任何人都可以解释一下这里的行为以及如何避免我的bean双重初始化?
由于
答案 0 :(得分:0)
在web.xml中,您在侦听器中指定了ContextLoader / ContextLoaderListener,并且未指定context-param“contextConfigLocation”(根上下文)。如果未明确指定“contextConfigLocation”,则根上下文实现应使用默认位置(使用XmlWebApplicationContext:“/ WEB -INF / applicationContext.xml”)。所以这将从applicationContext文件加载bean。 同样,这是在mvc-dispatcher(servlet名称)的init-param contextConfigLocation中明确指定的。这将导致第二次加载bean。
注意: 如果未指定mvc-dispatcher(servlet名称)的init-param contextConfigLocation,则它将在/WEB-INF/mvc-dispatcher-servlet.xml中查找文件。 根据经验,将公共服务层移动到根上下文。如果其ear模块具有多种war类型结构,则需要在父上下文中配置公共服务。