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.
- Access the Spring-ApplicationContext from everywhere in your Application
- How to access a value defined in the application.properties file in Spring Boot ( Externalized Conf)
- The Easiest Way to Add Menus in Your Application
- Profile your application in the simulator with 'Zombies' Instrument.
- How to access Dropbox from the command line in Linux
- Restrict Access to the Shell by Running Your Application Full Screen
- How to resolve "your security settings have blocked an untrusted application from running" in Mac
- Enabling Tracing in Your Application
- Letter from China: The sex industry is everywhere but nowhere
- Launch your application in Vista under the local system account without the UAC popup
- Launch your application in Vista under the local system account without the UAC popup
- BeanFactory and ApplicationContext in Spring
- BeanFactory or ApplicationContext in Spring
- Running Your iOS App in the Simulator From The Command Line
- Secure Remote Access from Your Desktop
- the virtual machine is in use by an application on your host computer
- The entitlements specified in your application’s Code Signing Entitlements file do not match those
- 虚拟机the virtual machine is in use by an application on your host computer解决
- XXLB
- 使用docker模拟并发访问服务器
- nexus5 博通芯片WIFI详解 (1)
- Hi3520d 嵌入式ftp服务器vsftpd移植及配置
- Spark快速大数据分析——互动出版网
- Access the Spring-ApplicationContext from everywhere in your Application
- 我的java 之旅——day 1
- XXSXB
- Firefox OS启动过程分析-nuwa进程启动
- ARM指令集
- java session 解析
- android-async-http源码解读(一)
- Java:关于super()的用法
- nexus5 博通芯片WIFI详解 (2)