diff --git a/zheng-common/src/main/java/com/zheng/common/util/SpringContextUtil.java b/zheng-common/src/main/java/com/zheng/common/util/SpringContextUtil.java index 276ebaa7..143c07d3 100644 --- a/zheng-common/src/main/java/com/zheng/common/util/SpringContextUtil.java +++ b/zheng-common/src/main/java/com/zheng/common/util/SpringContextUtil.java @@ -4,6 +4,8 @@ import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import java.util.Map; + /** * 资源文件读取工具 * @@ -29,8 +31,27 @@ public class SpringContextUtil implements ApplicationContextAware { 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 getBean(Class clazz) { + T t = null; + Map map = context.getBeansOfType(clazz); + for (Map.Entry entry : map.entrySet()) { + t = entry.getValue(); + } + return t; } }