Access the Spring-ApplicationContext from everywhere in your Application
-- from http://blog.jdevelop.eu/2008/07/06/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 theApplicationContextAware. 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;
-
-
-
-
-
-
-
-
-
-
-
-
public class ApplicationContextProvider implements ApplicationContextAware {
-
-
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
-
-
AppContext.setApplicationContext(ctx);
-
}
-
}
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;
-
-
-
-
-
-
-
-
public class AppContext {
-
-
private static ApplicationContext ctx;
-
-
-
-
-
-
public static void setApplicationContext(ApplicationContext applicationContext) {
-
ctx = applicationContext;
-
}
-
-
-
-
-
-
-
public static ApplicationContext getApplicationContext() {
-
return ctx;
-
}
-
}
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.
分享到:
相关推荐
Spring中ApplicationContext加载机制
Spring中ApplicationContext和beanfactory区别.rar
spring 获得applicationcontext公用方法
Spring中ApplicationContext对事件传递
Spring Web MVC开发 xml配置文件格式,无bean之类 Spring Web MVC开发配置文件 applicationContext
《Spring的数据源配置文件模板》applicationContext.zip
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0"> <property name="interceptors"> <list> <ref bean="doubleSubmitInterceptor"/> </list> ...
spring4的“所有jar包”+applicationContext.xml+web.xml====全套
这个jar文件包含Web应用开发时...包括一些动载入Web ApplicationContext 特性的类、Lister类、文件上传的支持类、便捷的工具辅助类、Spring HTTP调用、用于集成其他web框架的基础结构以及技术,例如(@RequestMapping)
主要介绍了 Spring获取ApplicationContext对象工具类的实现方法的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下
spring的Applicationcontext对事件的监听,在一个applicationContext范围内,实现类似MQ的效果
Spring配置文件ApplicationContext,内容齐全,有需要的可以下载。
Spring ApplicationContext.xml配置的12个技巧演示教学.pdf
本篇文章主要介绍了详解spring applicationContext.xml 配置文件 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
applicationContext.xml+hiberniate属性+Session属性+自动代理+拦截器!!!! 上传的限制去除了jar包!
NULL 博文链接:https://ylxy3058.iteye.com/blog/2223687
spring定时任务必须的包和spring配置说明
spring2.5的applicationContext配置文件.
今天小编就为大家分享一篇关于spring中通过ApplicationContext getBean获取注入对象的方法实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
这个工程主要实现了: Spring中Bean的生命周期 applicationcontext的应用(实现国际化,事件的传递)