Making a Spring bean Applicationcontext aware

Suppose we have a webapplication with spring,
and we are initializing spring’s WebApplicationContext by configuring ContextLoaderListener in web.xml.
How can we get an instance of spring’s applicationContext object?

Possible solutions:
While configuring spring through web.xml the spring ApplicationContext object is set in the ServletContext.
To get ApplicationContext we say

ApplicationContext applicationContext = (ApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

So we can get spring ApplicationContext only where ServletContext is avaliable i.e. in Servlet,ServletFilter or any implementation class of ServletContextListener.
What if we dont want to write a servletFilter or ServletContextListener .

What if we need to access a spring bean before a servletFilter or servlet is called(for authentication)and we dont want to write a ServletContextListener.

How can we obtain spring’s applicationContext in this case?
One way to achieve this would be to use the ApplicationContextAware interface provided by Spring.

Create a class which implements ApplicationContextAware. The method, “setApplicationContext(…)” gets called during the creation of this bean, providing the reference to the context. Our program should store this for a later interaction with the context.
       package com.abc;
      import org.springframework.beans.BeansException;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.ApplicationContextAware;

      public class ApplicationContextProvider implements ApplicationContextAware {
           private static ApplicationContext applicationContext = null;

            public static ApplicationContext getApplicationContext() {
                return applicationContext;
            }
            public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
                  / / Assign the ApplicationContext into a static variable
                 this.applicationContext = applicationContext;
            }
      }
Initialize the new bean in applicationContext.xml
<bean id=”applicationContextProvider” class=”com.abc.ApplicationContextProvider”></bean>
Now we can call the static method, ApplicationContextProvider.getApplicationContext(), from any class in our application, to get assess to the Spring ApplicationContext.

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

HEllo!!

Hey, thanks so much for your solution, it was very useful in a problem that I have communicating the jaxws2.0 with the spring beans, the beans created with jaxws2.0 have the daos in null, even that spring has injected this.

Thank u.

radhika,

Thanks for the valuable information. Please keep posting.

Regards
Mat

Thank you. This was very helpful.

In a web environnement, you can use: org.springframework.web.context.support.WebApplicationContextUtils .

thank u very helpful

Hi Radhika,
The article was extremely helpful. It explained me how Spring framework treated only one context as Application Context even though there were multiple contexts available in my application.

Actually it tries to return the context wherein we have declared the bean of the class which implements “ApplicationContextAware” interface at run time.

Hey!
Thank you very much for this. I had to create a jaws bean and initialize it as singleton just one in a spring batch process and this made the trick!
Thanks!

Leave a comment

(required)

(required)