January 05, 2010

ApplicationContextAware - access application context from bean

« Spring scope | Main | OpenJDK or Java Sun JDK on Ubuntu »
Sometimes you need to access your Spring application context from a Spring instrumentalized Bean. For this use case you need to implement the ApplicationContextAware interface. The following code snippets desribes the usage:

Implementation class:
public class ApplicationContextProvider implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

}
Spring configuration:
<bean id="applicationContextProvider" class="org.developers.blog.spring.examples.ApplicationContextProvider"></bean>
Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 11:48 AM in Spring

 

[Trackback URL for this entry]

Comment: Siva at Do, 16 Sep 2:14 PM

Its really helpful and solved my problem, thank you

Comment: Marcos at Di, 22 Feb 9:41 PM

Really helpful!! Save my day!

Your comment:

(not displayed)
 
 
 

Live Comment Preview: