Access the Spring-ApplicationContext from everywhere in your Application

In this blog i will show you a short hint how you can access your Spring-ApplicationContext from everywhere in your Application.

Imagine you have an application (e.g. a web or swing-application) which you now want to be Spring-enabled. Ok you add the Spring libraries and the Configuration-file and create your Spring-beans. But there are still some old class-files which you can’t use in this way. These files still need access to the Spring-Honeypot where all the goodies exists and you don’t want to redesign your application.

First create the class “ApplicationContextProvider“. This class implements the ApplicationContextAware. A bean which implements the ApplicationContextAware-interface and is deployed into the context, will be called back on creation of the bean, using the interface’s setApplicationContext(…) method, and provided with a reference to the context, which may be stored for later interaction with the context.

ApplicationContextProvider.java

package context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * This class provides an application-wide access to the
 * Spring ApplicationContext! The ApplicationContext is
 * injected in a static method of the class "AppContext".
 *
 * Use AppContext.getApplicationContext() to get access
 * to all Spring Beans.
 *
 * @author Siegfried Bolz
 */
public class ApplicationContextProvider implements ApplicationContextAware {

    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        // Wiring the ApplicationContext into a static method
        AppContext.setApplicationContext(ctx);
    }
} // .EOF

This bean must be initialized in the Spring-Configuration file:

applicationContext.xml

<bean id="contextApplicationContextProvider" class="context.ApplicationContextProvider"></bean>

The key for success is located in the in the method “setApplicationContext(…)“, here we are wiring the injected ApplicationContext into the static method “public static void setApplicationContext(ApplicationContext applicationContext) of the class “AppContext“:

AppContext.java

package context;

import org.springframework.context.ApplicationContext;

/**
 * This class provides application-wide access to the Spring ApplicationContext.
 * The ApplicationContext is injected by the class "ApplicationContextProvider".
 *
 * @author Siegfried Bolz
 */
public class AppContext {

    private static ApplicationContext ctx;

    /**
     * Injected from the class "ApplicationContextProvider" which is automatically
     * loaded during Spring-Initialization.
     */
    public static void setApplicationContext(ApplicationContext applicationContext) {
        ctx = applicationContext;
    }


    /**
     * Get access to the Spring ApplicationContext from everywhere in your Application.
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return ctx;
    }
} // .EOF

Now you have access from all class-files to the Spring-ApplicationContext in your application, if you call the static method:

Poorfile.java

ApplicationContext ctx = AppContext.getApplicationContext();
Honeypotbean honey = (HoneyPotBean) ctx.getBean("honey");

Do you have any improvements? Please drop a comment below.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值