Fancy UI

All About Crazy IT Stuff

Get to Spring ApplicationContext from everywhere (The clean way)

with 18 comments

In this article I’ll show you a tip on how to get to your spring Application from any place on your application.

Let say that you are writing a Fat client java application (using Swing for example) and you use Spring in it and want a clean way to retrieve a certain bean from the loaded ApplicationContext how can you do that ???

First create a class named ApplicationContextProvider, this class must implement the ApplicationContextAware interface. A bean which implements this interface, will be called back on creation using the interface’s setApplicationContext(ctx) method, and provided with a reference of the whole context. I’m going to declare the ApplicationContextClass as a singleton and store the value for the context to be reused.

public class ApplicationContextProvider implements ApplicationContextAware {
    private static ApplicationContext context;

    public ApplicationContext getApplicationContext() {
        return context;
    }

    @Override
    public void setApplicationContext(ApplicationContext ctx) {
        context = ctx;
    }
}

This bean must be declared on you ApplicationContext configuration file :

<bean id="applicationContextProvder" class="org.myApp.ApplicationContextProvider"/>

To load a certain bean, all you need to write is this :

MyBean c = ApplicationContextProvider.getApplicationContext.getBean("BeanId", MyBean.class);
Advertisements

Written by imrabti

September 28, 2011 at 7:50 am

Posted in Spring

Tagged with ,

18 Responses

Subscribe to comments with RSS.

  1. Thank you. Your tip save my life.:) But you code doesn’t work becouse “public ApplicationContext getApplicationContext()” must be declarated as static method and in “MyBean c = ApplicationContextProvider.getApplicationContext.getBean(“BeanId”, MyBean.class)” you forget “()”

    Damian

    January 4, 2013 at 12:00 am

    • Thank you for the heads up 🙂
      I’ll fix that.

      imrabti

      January 4, 2013 at 8:22 am

      • Isn’t fixed yet!

        Luca

        May 7, 2013 at 8:35 am

  2. Thank you for your helpful entry!

    Esteban Flores

    April 9, 2013 at 7:50 am

  3. You saved my day! Great and easy way to solve that problem. Thank you!

    Alex

    July 29, 2013 at 4:29 pm

  4. This is really a nice solution I was struggling for two days. Finally got the idea..Thanks a lot 🙂

    Narendra Verma

    October 7, 2013 at 11:14 am

  5. thank you very much, simple and clear

    federico marcuzzo

    February 7, 2014 at 4:44 pm

  6. Thanks! You really saved me with this!

    Erwin

    March 13, 2014 at 11:48 am

  7. Hi, Nice solution but ApplicationContextProvider.getApplicationContext() will not work until you declare getApplicationContext() method static.

    Vishal

    June 7, 2014 at 4:50 am

    • Yes you are right it is just a small mistake I’ll fix it.

      imrabti

      June 7, 2014 at 12:54 pm

  8. Before you do this you need to create the context from spring configuration file, right?

    Ying

    September 25, 2014 at 8:45 pm

    • Yes of course, you first need to start the context.

      imrabti

      September 26, 2014 at 1:29 pm

      • So the questions is what is the best way to start the context in a standalone application?

        Ying

        September 26, 2014 at 2:42 pm

      • It is not a web application right ?
        The simplest way to start a Spring context is by instantiating it like this :

        ApplicationContext context = new ClassPathXmlApplicationContext(“”classpath*:/META-INF/applicationContext.xml””);

        Another more advanced way would be to use the new Spring-boot.

        imrabti

        September 26, 2014 at 2:51 pm

  9. Thanks for the quik reply, I mean how do I make sure I am creating a single instance of the context in a standalone application?

    Ying

    September 26, 2014 at 3:05 pm

  10. Hi,
    when i try this,

    ApplicationContextProvider.getApplicationContext gives NullpointerException

    Could u give me web.xml, Everyone uses that like but none writes totaly config

    İ think , my web.xml did not properly work.

    this is enough ?

    contextConfigLocation
    /WEB-INF/applicationContext.xml

    org.springframework.web.context.ContextLoaderListener

    when i include this

    sampleServlet

    org.springframework.web.servlet.DispatcherServlet

    giving this

    SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
    java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener
    at java.lang.ClassLoader.findBootstrapClass(Native Method)

    could u help ?

    Guest

    March 2, 2015 at 11:35 am

  11. Thank you!

    Franco

    November 25, 2015 at 1:26 am

  12. Of course you are screwed if you have two spring contexts (for example two components), and each has a ApplicationContextProvider bean.

    sceptic

    December 23, 2015 at 11:12 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: