Search This Blog

Saturday, 24 November 2012

Springs' ContextLoaderListener

In an earlier example we show how to load beans from multiple configuration files. This is mostly needed in web applications where we write the different bean definitions in different layers.
As we saw in our basic MVC example, Spring 's MVC dispatcher servlet is responsible for loading the beans in our XML configuration file.This is from Spring Documentation:
A web application can define any number of DispatcherServlets. 
Each servlet will operate in its own namespace, loading its own 
application context with mappings, handlers, etc
This implies that any beans that are loaded from the dispatcher servlet are only accessible in the scope of the Dispatcher servlet code. If there is any other servlet, it wont be able to access any of the beans. The only way to access the beans is :
Only the root application context as loaded by ContextLoaderListener, if any, will be shared.
This means that beans loaded using the servlet listener will be available to all servlets.
<!-- The context params that read by ContextLoaderListener  -->
<!-- This listener will load other application context files in addition 
            to springweb-servlet.xml -->
The Listener is an implementation of ServletContextListener. On context initialization it loads the beans defined in the context param and adds them to the root application context (in this case the WebApplicationContext) Any DispatcherServlets or other Spring managed Servlets like the MessageDispatcherServlet define their own WebAplicationContexts which inherit from the root context. Thus the beans made available via the WebApplicationContext are now accessible to all.


  1. Wow. Awesome article. Please do more articles like this in the future. Very informational and knowledgeable. I will expect more from you in the future. For now i will just bookmark your page and surely I'm gonna come back later to read more. Thank you to the writer!


  2. Reading your article is such a privilege. It does inspire me, I hope that you can share more positive thoughts. Visit my site too. The link is posted below.

  3. There are actually a number of details like that to take into consideration. That may be a nice point to carry up. I offer the ideas above as normal inspiration but clearly there are questions just like the one you bring up where crucial thing shall be working in sincere good faith. I don?t know if best practices have emerged around things like that, but I'm sure that your job is clearly identified as a good game. Both boys and girls really feel the impression of just a second’s pleasure, for the rest of their lives. best online casino