Access the Spring-ApplicationContext from everywhere in your Application

来源:互联网 发布:截面数据 编辑:程序博客网 时间:2024/06/05 13:22

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