码迷,mamicode.com
首页 > 编程语言 > 详细

spring服务定位器,可在任何地方获取bean

时间:2016-06-05 13:45:36      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

通过持有的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 context
19      */
20     @Override
21     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     @Override
35     public void destroy() throws Exception {
36         ServiceLocator.clear();
37     }
38     
39     /**
40      * 取得存储在静态变量中的ApplicationContext.
41      * @return
42      */
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中的ApplicationContext
71      */
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.xml ServiceLocator");
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     }

 

spring服务定位器,可在任何地方获取bean

标签:

原文地址:http://www.cnblogs.com/Mr-kevin/p/5560470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!