|
@@ -298,7 +298,7 @@ public class ApplicationContextHolder implements ApplicationContextAware {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取可以为空的对象实例
|
|
|
+ * 获取对象实例
|
|
|
*
|
|
|
* @param type 对象类型
|
|
|
* @param required 对象实例是否必须存在
|
|
@@ -328,7 +328,7 @@ public class ApplicationContextHolder implements ApplicationContextAware {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取可以为空的对象实例
|
|
|
+ * 获取对象实例
|
|
|
*
|
|
|
* @param name 对象名称
|
|
|
* @param required 对象实例是否必须存在
|
|
@@ -347,6 +347,42 @@ public class ApplicationContextHolder implements ApplicationContextAware {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取对象实例
|
|
|
+ *
|
|
|
+ * @param name 对象名称
|
|
|
+ * @param <T> 对象类型泛型
|
|
|
+ * @return 对象实例
|
|
|
+ */
|
|
|
+ public static <T> T getSafeBean(String name) {
|
|
|
+ return getSafeBean(name, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取对象实例
|
|
|
+ *
|
|
|
+ * @param name 对象名称
|
|
|
+ * @param required 对象实例是否必须存在
|
|
|
+ * @param <T> 对象类型泛型
|
|
|
+ * @return 对象实例
|
|
|
+ */
|
|
|
+ public static <T> T getSafeBean(String name, boolean required) {
|
|
|
+ T bean = getBean(name, false);
|
|
|
+ if (bean != null) {
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果名称包含包路径,则尝试获取简单类名获取
|
|
|
+ int separator = name.lastIndexOf('.');
|
|
|
+ if (separator >= 0 && (bean = getBean(StringUtils.hump(name.substring(separator + 1)), false)) != null) {
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+ if (required) {
|
|
|
+ throw new NoSuchBeanDefinitionException(name);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取所有application.yml文件资源
|
|
|
*
|