|
@@ -1,117 +1,117 @@
|
|
|
-package com.leanwo.gateway.util;
|
|
|
|
|
-
|
|
|
|
|
-import java.lang.annotation.Annotation;
|
|
|
|
|
-import java.util.Collection;
|
|
|
|
|
-import java.util.Iterator;
|
|
|
|
|
-import java.util.Map;
|
|
|
|
|
-
|
|
|
|
|
-import org.apache.commons.logging.Log;
|
|
|
|
|
-import org.apache.commons.logging.LogFactory;
|
|
|
|
|
-import org.springframework.beans.BeansException;
|
|
|
|
|
-import org.springframework.context.ApplicationContext;
|
|
|
|
|
-import org.springframework.context.ApplicationContextAware;
|
|
|
|
|
-
|
|
|
|
|
-import com.leanwo.gateway.exception.BeanNotInjectException;
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-public class SpringUtil implements ApplicationContextAware {
|
|
|
|
|
- private static Log logger = LogFactory.getLog(SpringUtil.class);
|
|
|
|
|
- private static ApplicationContext applicationContext;
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public void setApplicationContext(ApplicationContext arg0) throws BeansException {
|
|
|
|
|
- applicationContext = arg0;
|
|
|
|
|
- String[] names = beanNames();
|
|
|
|
|
- logger.info("IOC容器共管理:" + names.length + "个对象。");
|
|
|
|
|
- logger.info("Spring容器初始化成功。");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static ApplicationContext getApplicationContext() {
|
|
|
|
|
- checkApplicationContext();
|
|
|
|
|
- return applicationContext;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
|
|
|
|
- */
|
|
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
|
|
- public static <T> T getBean(String name) {
|
|
|
|
|
- checkApplicationContext();
|
|
|
|
|
- return (T) applicationContext.getBean(name);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
|
|
|
|
- */
|
|
|
|
|
- public static <T> T getBean(Class<T> clazz) {
|
|
|
|
|
- checkApplicationContext();
|
|
|
|
|
- return (T) applicationContext.getBeansOfType(clazz);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
|
|
|
|
- */
|
|
|
|
|
- public static Object getSingleBean(Class clazz) {
|
|
|
|
|
- checkApplicationContext();
|
|
|
|
|
- Map<String, Object> map = applicationContext.getBeansOfType(clazz);
|
|
|
|
|
- if (map == null || map.size() == 0) {
|
|
|
|
|
- throw new BeanNotInjectException(clazz.getName() + "未注入。");
|
|
|
|
|
- } else if (map.size() > 1) {
|
|
|
|
|
- throw new IllegalStateException(clazz.getName() + "存在" + map.size() + "个对象。");
|
|
|
|
|
- } else {
|
|
|
|
|
- Iterator iterator = map.entrySet().iterator();
|
|
|
|
|
- Map.Entry entry = (Map.Entry) iterator.next();
|
|
|
|
|
- return entry.getValue();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 从当前IOC获取标识了annotation注释的对象
|
|
|
|
|
- *
|
|
|
|
|
- * @param annotation
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- public static Collection<Object> beansByAnnotation(Class<? extends Annotation> annotation) {
|
|
|
|
|
- checkApplicationContext();
|
|
|
|
|
- return applicationContext.getBeansWithAnnotation(annotation).values();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取当前IOC所有Bean的名称
|
|
|
|
|
- *
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- public static String[] beanNames() {
|
|
|
|
|
- checkApplicationContext();
|
|
|
|
|
- return applicationContext.getBeanDefinitionNames();
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取当前IOC中beanName对应的类
|
|
|
|
|
- *
|
|
|
|
|
- * @param beanName
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- public static Class getType(String beanName) {
|
|
|
|
|
- checkApplicationContext();
|
|
|
|
|
- return applicationContext.getType(beanName);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 从当前IOC获取标识了annotation注释的对象
|
|
|
|
|
- *
|
|
|
|
|
- * @param id
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- public static Object getObject(String id) {
|
|
|
|
|
- checkApplicationContext();
|
|
|
|
|
- return applicationContext.getBean(id);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static void checkApplicationContext() {
|
|
|
|
|
- if (applicationContext == null) {
|
|
|
|
|
- throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+package com.leanwo.gateway.util;
|
|
|
|
|
+
|
|
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
|
|
+import java.util.Collection;
|
|
|
|
|
+import java.util.Iterator;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+import org.apache.commons.logging.Log;
|
|
|
|
|
+import org.apache.commons.logging.LogFactory;
|
|
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
|
|
+import org.springframework.context.ApplicationContextAware;
|
|
|
|
|
+
|
|
|
|
|
+import com.leanwo.gateway.exception.BeanNotInjectException;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+public class SpringUtil implements ApplicationContextAware {
|
|
|
|
|
+ private static Log logger = LogFactory.getLog(SpringUtil.class);
|
|
|
|
|
+ private static ApplicationContext applicationContext;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void setApplicationContext(ApplicationContext arg0) throws BeansException {
|
|
|
|
|
+ applicationContext = arg0;
|
|
|
|
|
+ String[] names = beanNames();
|
|
|
|
|
+ logger.info("IOC容器共管理:" + names.length + "个对象。");
|
|
|
|
|
+ logger.info("Spring容器初始化成功。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static ApplicationContext getApplicationContext() {
|
|
|
|
|
+ checkApplicationContext();
|
|
|
|
|
+ return applicationContext;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
|
|
|
|
+ */
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ public static <T> T getBean(String name) {
|
|
|
|
|
+ checkApplicationContext();
|
|
|
|
|
+ return (T) applicationContext.getBean(name);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
|
|
|
|
+ */
|
|
|
|
|
+ public static <T> T getBean(Class<T> clazz) {
|
|
|
|
|
+ checkApplicationContext();
|
|
|
|
|
+ return (T) applicationContext.getBeansOfType(clazz);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Object getSingleBean(Class clazz) {
|
|
|
|
|
+ checkApplicationContext();
|
|
|
|
|
+ Map<String, Object> map = applicationContext.getBeansOfType(clazz);
|
|
|
|
|
+ if (map == null || map.size() == 0) {
|
|
|
|
|
+ throw new BeanNotInjectException(clazz.getName() + "未注入。");
|
|
|
|
|
+ } else if (map.size() > 1) {
|
|
|
|
|
+ throw new IllegalStateException(clazz.getName() + "存在" + map.size() + "个对象。");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Iterator iterator = map.entrySet().iterator();
|
|
|
|
|
+ Map.Entry entry = (Map.Entry) iterator.next();
|
|
|
|
|
+ return entry.getValue();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 从当前IOC获取标识了annotation注释的对象
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param annotation
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Collection<Object> beansByAnnotation(Class<? extends Annotation> annotation) {
|
|
|
|
|
+ checkApplicationContext();
|
|
|
|
|
+ return applicationContext.getBeansWithAnnotation(annotation).values();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前IOC所有Bean的名称
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String[] beanNames() {
|
|
|
|
|
+ checkApplicationContext();
|
|
|
|
|
+ return applicationContext.getBeanDefinitionNames();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前IOC中beanName对应的类
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param beanName
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Class getType(String beanName) {
|
|
|
|
|
+ checkApplicationContext();
|
|
|
|
|
+ return applicationContext.getType(beanName);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 从当前IOC获取标识了annotation注释的对象
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param id
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Object getObject(String id) {
|
|
|
|
|
+ checkApplicationContext();
|
|
|
|
|
+ return applicationContext.getBean(id);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void checkApplicationContext() {
|
|
|
|
|
+ if (applicationContext == null) {
|
|
|
|
|
+ throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|