Jerry Jacob's Blog

Making your Spring application, ‘Context Aware’

Posted in Java/ J2EE by jerlinworld on July 20, 2009

Suppose you have some java classes which are unable (or you don’t want it) to be wired to the Spring application context; for example, an auto generated web service client class! But you do want to use the dependency injection feature of Spring to get some of the other beans injected in to this class. How can we make this happen? One way to achieve this would be to use the ApplicationContextAware interface provided by Spring.

Create a class named ApplicationContext which implements  ApplicationContextAware. The inherited method, ‘setApplicationContext(…)’ will get 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 jerry.jacob;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContextProvider implements ApplicationContextAware {
    private static ApplicationContext ctx = null;
    public static ApplicationContext getApplicationContext() {         
        return ctx;    
    }
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        // Assign the ApplicationContext into a static method
        this.ctx = ctx;
    }
}

Initialize the new bean in applicationContext.xml

<bean id="applicationContextProvider" class="jerry.jacob.ApplicationContextProvider"></bean>

Now you can call the static method, getApplicationContext(), from any class in your application, to get assess to the Spring ApplicationContext.

About these ads

6 Responses

Subscribe to comments with RSS.

  1. Greg said, on October 28, 2009 at 2:52 pm

    This has the smell of a static singleton, and I haven’t liked them in the past. Makes things very untestable.

    However, you have presented an interested side effect that may make it palatable: access from something not yet known when the context is generated. I will have to think about this some more.

  2. Dkumar said, on November 8, 2010 at 3:49 pm

    I have been trying out this piece of code. But it’s returning null value for application context. Why is it so?

  3. sumit said, on January 18, 2011 at 7:11 pm

    thanks!

  4. kiran said, on August 24, 2011 at 5:26 am

    in my web page, i have using tiles(header.jsp,error.jsp,body.jsp). And My requirement is when the form present in body has some errors. I want to display those errors in error.jsp.

    I am using spring web flow

    my validations are going on in my ${modelname}Validator class

    is it possible to show one jsp form’s errors in other jsp(in error.jsp)

  5. Maximus1927 said, on October 26, 2011 at 8:45 am

    it’s fully working. TKS A LOT!

  6. Carmelo said, on November 24, 2011 at 8:57 am

    sorry for resuming an old post, but I have my own implementation of the class:
    public class BellerophonteContext implements ApplicationContextAware {

    static ApplicationContext ctx= null;

    @Override
    public void setApplicationContext(ApplicationContext arg0)
    throws BeansException {
    if (arg0!= null)
    ctx= arg0;

    }
    public synchronized static ApplicationContext getApplicationContext(){
    if (ctx!=null)
    return ctx;
    ctx = new ClassPathXmlApplicationContext(“beans.xml”);
    return ctx;
    }
    }

    That would fix some problems, at least in my opinion.. Any feedback?


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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: