|
@@ -4,6 +4,8 @@ import org.springframework.beans.BeansException;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
|
|
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 资源文件读取工具
|
|
* 资源文件读取工具
|
|
|
*
|
|
*
|
|
@@ -29,8 +31,27 @@ public class SpringContextUtil implements ApplicationContextAware {
|
|
|
context = applicationContext;
|
|
context = applicationContext;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static Object getBean(String name) {
|
|
|
|
|
- return context.getBean(name);
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据名称获取bean
|
|
|
|
|
+ * @param beanName
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Object getBean(String beanName) {
|
|
|
|
|
+ return context.getBean(beanName);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据类型获取bean
|
|
|
|
|
+ * @param clazz
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static <T> T getBean(Class<T> clazz) {
|
|
|
|
|
+ T t = null;
|
|
|
|
|
+ Map<String, T> map = context.getBeansOfType(clazz);
|
|
|
|
|
+ for (Map.Entry<String, T> entry : map.entrySet()) {
|
|
|
|
|
+ t = entry.getValue();
|
|
|
|
|
+ }
|
|
|
|
|
+ return t;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|