I have a spring based application deployed in JBoss. I have some beans with @PostConstruct method. When I start the application, these beans are initialized twice. It seems two different contexts are being loaded. I can see following in my logs:
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.
My web.xml config is:
<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>
Can anyone please explain the behavior here and how can avoid my beans double initialization?
Thanks