|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+package com.unissoft.utils;
|
|
|
2
|
+
|
|
|
3
|
+import org.springframework.beans.BeansException;
|
|
|
4
|
+import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
|
|
5
|
+import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
|
|
6
|
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
|
7
|
+import org.springframework.stereotype.Repository;
|
|
|
8
|
+
|
|
|
9
|
+@Repository
|
|
|
10
|
+public final class SpringUtils implements BeanFactoryPostProcessor {
|
|
|
11
|
+
|
|
|
12
|
+ private static ConfigurableListableBeanFactory beanFactory; // Spring应用上下文环境
|
|
|
13
|
+
|
|
|
14
|
+ @Override
|
|
|
15
|
+ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
|
|
16
|
+ SpringUtils.beanFactory = beanFactory;
|
|
|
17
|
+ }
|
|
|
18
|
+
|
|
|
19
|
+ public static ConfigurableListableBeanFactory getBeanFactory() {
|
|
|
20
|
+ return beanFactory;
|
|
|
21
|
+ }
|
|
|
22
|
+
|
|
|
23
|
+ /**
|
|
|
24
|
+ * 获取对象
|
|
|
25
|
+ *
|
|
|
26
|
+ * @param name
|
|
|
27
|
+ * @return Object 一个以所给名字注册的bean的实例
|
|
|
28
|
+ * @throws BeansException
|
|
|
29
|
+ *
|
|
|
30
|
+ */
|
|
|
31
|
+ @SuppressWarnings("unchecked")
|
|
|
32
|
+ public static <T> T getBean(String name) throws BeansException {
|
|
|
33
|
+ return (T) getBeanFactory().getBean(name);
|
|
|
34
|
+ }
|
|
|
35
|
+
|
|
|
36
|
+ /**
|
|
|
37
|
+ * 获取类型为requiredType的对象
|
|
|
38
|
+ *
|
|
|
39
|
+ * @param clz
|
|
|
40
|
+ * @return
|
|
|
41
|
+ * @throws BeansException
|
|
|
42
|
+ *
|
|
|
43
|
+ */
|
|
|
44
|
+ public static <T> T getBean(Class<T> clz) throws BeansException {
|
|
|
45
|
+ T result = (T) getBeanFactory().getBean(clz);
|
|
|
46
|
+ return result;
|
|
|
47
|
+ }
|
|
|
48
|
+
|
|
|
49
|
+ /**
|
|
|
50
|
+ * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
|
|
|
51
|
+ *
|
|
|
52
|
+ * @param name
|
|
|
53
|
+ * @return boolean
|
|
|
54
|
+ */
|
|
|
55
|
+ public static boolean containsBean(String name) {
|
|
|
56
|
+ return getBeanFactory().containsBean(name);
|
|
|
57
|
+ }
|
|
|
58
|
+
|
|
|
59
|
+ /**
|
|
|
60
|
+ * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
|
|
|
61
|
+ *
|
|
|
62
|
+ * @param name
|
|
|
63
|
+ * @return boolean
|
|
|
64
|
+ * @throws NoSuchBeanDefinitionException
|
|
|
65
|
+ *
|
|
|
66
|
+ */
|
|
|
67
|
+ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
|
|
|
68
|
+ return getBeanFactory().isSingleton(name);
|
|
|
69
|
+ }
|
|
|
70
|
+
|
|
|
71
|
+ /**
|
|
|
72
|
+ * @param name
|
|
|
73
|
+ * @return Class 注册对象的类型
|
|
|
74
|
+ * @throws NoSuchBeanDefinitionException
|
|
|
75
|
+ *
|
|
|
76
|
+ */
|
|
|
77
|
+ public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
|
|
|
78
|
+ return getBeanFactory().getType(name);
|
|
|
79
|
+ }
|
|
|
80
|
+
|
|
|
81
|
+ /**
|
|
|
82
|
+ * 如果给定的bean名字在bean定义中有别名,则返回这些别名
|
|
|
83
|
+ *
|
|
|
84
|
+ * @param name
|
|
|
85
|
+ * @return
|
|
|
86
|
+ * @throws NoSuchBeanDefinitionException
|
|
|
87
|
+ *
|
|
|
88
|
+ */
|
|
|
89
|
+ public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
|
|
|
90
|
+ return getBeanFactory().getAliases(name);
|
|
|
91
|
+ }
|
|
|
92
|
+
|
|
|
93
|
+}
|