0

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

amique
  • 2,176
  • 7
  • 34
  • 53
  • possible duplicate of [What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?](http://stackoverflow.com/questions/11708967/what-is-the-difference-between-applicationcontext-and-webapplicationcontext-in-s) – Sotirios Delimanolis Apr 09 '14 at 07:08
  • 1
    You need to tell more about your environment and how you deploy your app – Stephane Nicoll Apr 09 '14 at 07:47
  • By provided logs we see two components (`WebApplicationContext`, `FrameworkServlet `), which are initialized correctly. To convince us that there is really an issue you have to provide more info about your app and more logs. Now we don't see an `initialized twice` issue. – Artem Bilan Apr 09 '14 at 08:22
  • If you think it's loading twice, out a `System.out.println()` in one of your classes and see if it gets instantiated twice. My guess is that the others are right, and it's not. – CodeChimp Apr 09 '14 at 16:39
  • From the given web.xml, it seemed that I have not defined any application context (context-param contextConfigLocation) and my servlet context is being loaded first as application context and later as servlet context. Due to this beans are initialized twice in different context. Fixed the problem by defining my applicationContext.xml in context-param and adding an empty servletContext.xml in dispatcher servlet context. – amique Apr 09 '14 at 23:31

1 Answers1

0

In your web.xml, you have specified ContextLoader/ContextLoaderListener in listener and have not specified context-param "contextConfigLocation" (root context). If "contextConfigLocation" is not explicitly specified, the root context implementation is supposed to use a default location (with XmlWebApplicationContext: "/WEB-INF/applicationContext.xml"). So this will load beans from applicationContext file. Again, this was explicitly specified in init-param contextConfigLocation of mvc-dispatcher(servlet name). This will cause to load the beans second time.

Note: If init-param contextConfigLocation of mvc-dispatcher(servlet name) is not specified, it will look for file at /WEB-INF/mvc-dispatcher-servlet.xml. As a rule of thumb, move your common service layer to root context. If its ear module with multiple war type structure, then you need to configure common services in parent context.

Prasad
  • 3,785
  • 2
  • 14
  • 23
  • Fixed the problem by defining my applicationContext.xml in context-param and adding an empty servletContext.xml in dispatcher servlet context – amique Apr 10 '14 at 05:32