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

 

你可能感兴趣的:(在Web中任意位置得到Spring ApplicationContext)