Spring application context aware

I want my spring application context to be retrieved any time anywhere in my application. Luckily, ApplicationContextAware and BeanFactoryPostProcessor helped me.

Spring-beans.xml:


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:jee="http://www.springframework.org/schema/jee"

xmlns:lang="http://www.springframework.org/schema/lang"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:util="http://www.springframework.org/schema/util"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd

http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

     <context:component-scan base-package="spring.examples"/>

</beans>  

Spring context Wrapper:

package spring.examples;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextWrapper implements ApplicationContextAware,
        BeanFactoryPostProcessor {

    private ApplicationContext appContext;
    private ConfigurableListableBeanFactory factory;

    public void postProcessBeanFactory(ConfigurableListableBeanFactory factory)
            throws BeansException {
        this.factory = factory;
    }

    public void setApplicationContext(ApplicationContext c)
            throws BeansException {
        this.appContext = c;
    }
    public ApplicationContext getAppContext() {
        return appContext;
    }
    public void setAppContext(ApplicationContext appContext) {
        this.appContext = appContext;
    }
    public ConfigurableListableBeanFactory getFactory() {
        return factory;
    }
    public void setFactory(ConfigurableListableBeanFactory factory) {
        this.factory = factory;
    }
}

Test:

package spring.examples;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringContextWrapperTest {
    @Autowired
    SpringContextWrapper springContextWrapper;

    void createSpringContext() {
        AutowireCapableBeanFactory factory = null;
        ApplicationContext appContext = new ClassPathXmlApplicationContext("spring-beans.xml");
        factory = appContext.getAutowireCapableBeanFactory();
        factory.autowireBeanProperties(this,
                AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    }
    public static void main(String[] args) {
        SpringContextWrapperTest springContextWrapperTest = new SpringContextWrapperTest();

        // Once Spring context is created, It will call the following post back methods in SpringContextWrapper
        // 1. postProcessBeanFactory
        // 2. setApplicationContext
        springContextWrapperTest.createSpringContext();

        // Check if the application context is injected in SpringContextWrapper.
        if (springContextWrapperTest.springContextWrapper.getAppContext() != null) {
            System.out.println("Application context is retreived from spring context wrapper.");
        } else {
            System.out.println("No Application context found.");
        }
    }
}

Output:

Inside setApplicationContext method
Inside postProcessBeanFactory method
Application context is retreived from spring context wrapper.

Now, I can able to retrieve spring application context anywhere in my application.

Advertisements

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: