你的位置:首页 > Java教程

[Java教程]spring服务定位器,可在任何地方获取bean


通过持有的Spring应用场景ApplicationContext,可在任何地方获取bean。

 1 import org.apache.commons.logging.Log; 2 import org.apache.commons.logging.LogFactory; 3 import org.springframework.beans.factory.DisposableBean; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.ApplicationContextAware; 6  7 /** 8  * 服务定位器 9  * 持有Spring的应用场景, 可在任何地方获取bean.10 */11 public final class ServiceLocator implements ApplicationContextAware, DisposableBean {12   13   private static Log logger = LogFactory.getLog(ServiceLocator.class);14   private static ApplicationContext context = null;15   16   /**17    * 实现ApplicationContextAware接口, 注入Context到静态变量中.18    * @param context19   */20   @Override21   public void setApplicationContext(ApplicationContext context) {    22     logger.debug("Injected the ApplicationContext into ServiceLocator:" + context);23     if (ServiceLocator.context != null) {24       logger.debug("[------------ ApplicationContext in the ServiceLocator " +25                 "is covered, as the original ApplicationContext is:"26                       + ServiceLocator.context + " ------------]");27     }28     ServiceLocator.context = context; 29   }30   31   /**32    * 实现DisposableBean接口,在Context关闭时清理静态变量.33   */34   @Override35   public void destroy() throws Exception {36     ServiceLocator.clear();37   }38   39   /**40    * 取得存储在静态变量中的ApplicationContext.41    * @return42   */43   public static ApplicationContext getApplicationContext() {44     assertContextInjected();45     return context;46   }47   48   /**49    * 从Spring的应用场景中取得Bean, 自动转型为所赋值对象的类型.50    * @param name bean名称51    * @return bean对象52   */53   @SuppressWarnings("unchecked")54   public static <T> T getService(String name) {55     assertContextInjected();56     return (T) context.getBean(name);57   }58   59   /**60    * 从Spring的应用场景中取得Bean, 自动转型为所赋值对象的类型.61    * @param requiredType bean类62    * @return bean对象63   */64   public static <T> T getService(Class<T> requiredType) {65     assertContextInjected();66     return context.getBean(requiredType);67   }68   69   /**70    * 清除ServiceLocator中的ApplicationContext71   */72   public static void clear() {73     logger.debug("Clear ApplicationContext in ServiceLocator :" + context);74     context = null;75   }76   77   /**78    * 检查ApplicationContext不为空.79   */80   private static void assertContextInjected() {81     if (context == null) {82       throw new IllegalStateException("ApplicaitonContext not injected, " +83               "as defined in the context.);84     }85   }86 }

View Code

 

调用getService函数,分为按名称和类型获取。

 1   /** 2    * 从Spring的应用场景中取得Bean, 自动转型为所赋值对象的类型. 3    * @param name bean名称 4    * @return bean对象 5   */ 6   @SuppressWarnings("unchecked") 7   public static <T> T getService(String name) { 8     assertContextInjected(); 9     return (T) context.getBean(name);10   }11   12   /**13    * 从Spring的应用场景中取得Bean, 自动转型为所赋值对象的类型.14    * @param requiredType bean类15    * @return bean对象16   */17   public static <T> T getService(Class<T> requiredType) {18     assertContextInjected();19     return context.getBean(requiredType);20   }