Making a Spring bean Applicationcontext aware 7

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.
      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=””></bean>
Now we can call the static method, ApplicationContextProvider.getApplicationContext(), from any class in our application, to get assess to the Spring ApplicationContext.

Leave a comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

7 thoughts on “Making a Spring bean Applicationcontext aware

  • Axel Osorio


    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.

  • Rushi Shah

    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.

  • Luis

    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!