fhx
2023-11-06 8812516d440c38ee4691daeb2e1685d1eabe85dd
提交 | 用户 | age
881251 1 package com.hx.common;
220da5 2
F 3 import org.springframework.aop.framework.AopContext;
4 import org.springframework.beans.BeansException;
5 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
6 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
7 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
8 import org.springframework.stereotype.Component;
9
10 /**
11  * spring工具类 方便在非spring管理环境中获取bean
12  * 
13  * @author
14  */
15 @Component
16 public final class SpringUtils implements BeanFactoryPostProcessor
17 {
18     /** Spring应用上下文环境 */
19     private static ConfigurableListableBeanFactory beanFactory;
20
21     @Override
22     public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
23     {
24         SpringUtils.beanFactory = beanFactory;
25     }
26
27     /**
28      * 获取对象
29      *
30      * @param name
31      * @return Object 一个以所给名字注册的bean的实例
32      * @throws BeansException
33      *
34      */
35     @SuppressWarnings("unchecked")
36     public static <T> T getBean(String name) throws BeansException
37     {
38         return (T) beanFactory.getBean(name);
39     }
40
41     /**
42      * 获取类型为requiredType的对象
43      *
44      * @param clz
45      * @return
46      * @throws BeansException
47      *
48      */
49     public static <T> T getBean(Class<T> clz) throws BeansException
50     {
51         T result = (T) beanFactory.getBean(clz);
52         return result;
53     }
54
55     /**
56      * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
57      *
58      * @param name
59      * @return boolean
60      */
61     public static boolean containsBean(String name)
62     {
63         return beanFactory.containsBean(name);
64     }
65
66     /**
67      * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
68      *
69      * @param name
70      * @return boolean
71      * @throws NoSuchBeanDefinitionException
72      *
73      */
74     public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException
75     {
76         return beanFactory.isSingleton(name);
77     }
78
79     /**
80      * @param name
81      * @return Class 注册对象的类型
82      * @throws NoSuchBeanDefinitionException
83      *
84      */
85     public static Class<?> getType(String name) throws NoSuchBeanDefinitionException
86     {
87         return beanFactory.getType(name);
88     }
89
90     /**
91      * 如果给定的bean名字在bean定义中有别名,则返回这些别名
92      *
93      * @param name
94      * @return
95      * @throws NoSuchBeanDefinitionException
96      *
97      */
98     public static String[] getAliases(String name) throws NoSuchBeanDefinitionException
99     {
100         return beanFactory.getAliases(name);
101     }
102
103     /**
104      * 获取aop代理对象
105      * 
106      * @param invoker
107      * @return
108      */
109     @SuppressWarnings("unchecked")
110     public static <T> T getAopProxy(T invoker)
111     {
112         return (T) AopContext.currentProxy();
113     }
114 }