在Web中任意位置得到Spring ApplicationContext

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

  1. package  context; 
  2.  
  3. import  org.springframework.beans.BeansException; 
  4. import  org.springframework.context.ApplicationContext; 
  5. import  org.springframework.context.ApplicationContextAware; 
  6.  
  7. /**
  8.  * This class provides an application-wide access to the
  9.  * Spring ApplicationContext! The ApplicationContext is
  10.  * injected in a static method of the class "AppContext".
  11.  *
  12.  * Use AppContext.getApplicationContext() to get access
  13.  * to all Spring Beans.
  14.  *
  15.  * @author Siegfried Bolz
  16.  */  
  17. public   class  ApplicationContextProvider  implements  ApplicationContextAware { 
  18.  
  19.      public   void  setApplicationContext(ApplicationContext ctx)  throws  BeansException { 
  20.          // Wiring the ApplicationContext into a static method  
  21.         AppContext.setApplicationContext(ctx); 
  22.     } 
  23. // .EOF  


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

applicationContext.xml

  1. < 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

  1. package  context; 
  2.  
  3. import  org.springframework.context.ApplicationContext; 
  4.  
  5. /**
  6.  * This class provides application-wide access to the Spring ApplicationContext.
  7.  * The ApplicationContext is injected by the class "ApplicationContextProvider".
  8.  *
  9.  * @author Siegfried Bolz
  10.  */  
  11. public   class  AppContext { 
  12.  
  13.      private   static  ApplicationContext ctx; 
  14.  
  15.      /**
  16.      * Injected from the class "ApplicationContextProvider" which is automatically
  17.      * loaded during Spring-Initialization.
  18.      */  
  19.      public   static   void  setApplicationContext(ApplicationContext applicationContext) { 
  20.         ctx = applicationContext; 
  21.     } 
  22.  
  23.      /**
  24.      * Get access to the Spring ApplicationContext from everywhere in your Application.
  25.      *
  26.      * @return
  27.      */  
  28.      public   static  ApplicationContext getApplicationContext() { 
  29.          return  ctx; 
  30.     } 
  31. // .EOF  


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

Poorfile.java

  1. ApplicationContext ctx = AppContext.getApplicationContext(); 
  2. 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、付费专栏及课程。

余额充值