Spring:利用ApplicationContextAware装配Bean

使用xml、注解的形式装配Bean,因其方便快捷,受到你们喜好。通常形式以下:app

1 @Autowired
2 private BeanObjectInterface beanObject;

然而在某些特别场景下,既须要将对象做为Bean交于Spring管理,又须要在代码中即时地获取Bean,用注解形式进行装配就不太能知足需求。ui

查找资料,Spring提供了 ApplicationContextAware 接口,便于实时地获取Bean。spa

首先,建立一个 Util 类,继承 ApplicationContextAware 接口:prototype

 1 public class SpringContextUtil implements ApplicationContextAware {
 2 
 3     //Spring应用上下文环境
 4     private static ApplicationContext applicationContext;
 5 
 6     /**
 7      * 实现ApplicationContextAware接口的回调方法,设置上下文环境
 8      * @param applicationContext
 9      * @throws BeansException
10      */
11     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
12         SpringContextUtil.applicationContext = applicationContext;
13     }
14 
15     public static ApplicationContext getApplicationContext(){
16         return applicationContext;
17     }
18 
19     /**
20      * 获取对象
21      * @param name
22      * @return Object 一个以类型
23      * @throws BeansException
24      */
25 
26     public static Object getBean(Class requiredType) throws BeansException {
27         return applicationContext.getBean(requiredType);
28     }
29 
30     /**
31      * 获取对象
32      * @param name
33      * @return Object 一个以所给名字注册的bean的实例
34      * @throws BeansException
35      */
36     public static Object getBean(String name) throws BeansException {
37         return applicationContext.getBean(name);
38     }
39 
40     /**
41      * 获取类型为requiredType的对象
42      * 若是bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException)
43      * @param name       bean注册名
44      * @param requiredType 返回对象类型
45      * @return Object 返回requiredType类型对象
46      * @throws BeansException
47      */
48     public static Object getBean(String name, Class requireType) throws BeansException{
49         return applicationContext.getBean(name, requireType);
50     }
51 
52     /**
53      * 若是BeanFactory包含一个与所给名称匹配的bean定义,则返回true
54      * @param name
55      * @return boolean
56      */
57     public static boolean containsBean(String name){
58         return applicationContext.containsBean(name);
59     }
60 
61     /**
62      * 判断以给定名字注册的bean定义是一个singleton仍是一个prototype。
63      * 若是与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
64      * @param name
65      * @return boolean
66      * @throws NoSuchBeanDefinitionException
67      */
68     public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
69         return applicationContext.isSingleton(name);
70     }
71 
72     /**
73      * @param name
74      * @return Class 注册对象的类型
75      * @throws NoSuchBeanDefinitionException
76      */
77     public static Class getType(String name) throws NoSuchBeanDefinitionException {
78         return applicationContext.getType(name);
79     }
80 
81     /**
82      * 若是给定的bean名字在bean定义中有别名,则返回这些别名
83      * @param name
84      * @return
85      * @throws NoSuchBeanDefinitionException
86      */
87     public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
88         return applicationContext.getAliases(name);
89     }
90 
91 }

而后在 appcontext.xml 文件中添加相应的bean配置,使得Spring能够在项目启动时加载 SpringContextUtil ,完成 applicationContext 的初始化。code

若是是使用注解扫描形式加载bean,则须要在 SpringContextUtil 上方标注 @Component:xml

1 @Component
2 public class SpringContextUtil implements ApplicationContextAware {
3     // ... 内容再也不赘述    
4 }

 

References:对象

p.s. 还有一篇简书博客找不到了。其实不少方法是从那里学来的
相关文章
相关标签/搜索