通过spring-boot自带获取Application.properties等数据操作,获取其相应对象 从而获取 其 整合的 数据
思路:(只是其中一种方法)
1.准备 spring-boot start jar
2.配置文件
3.启动类
4.相关代码
2.
3.
4.
运行截图
代码一览:
@SpringBootApplication
@EnableConfigurationProperties
public class SBS {
@Autowired
static ApplicationContext app;
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext run = SpringApplication.run(SBS.class, args);
Map<String, String> prefixProperties = PropUtils.getPrefixProperties(run, "mysqlMy.datasource");
System.out.println("-----------查找------------");
showMap(prefixProperties);
}
public static void showMap(Map<String, String> prefixProperties) {
for(Entry<String, String> en : prefixProperties.entrySet()) {
System.out.println(en.getKey()+"value:"+en.getValue());
}
}
}
public class PropUtils {
// 保存所有 properties
static Map<String,String> map = new HashMap<>();
//对外抛出
public static Map<String,String> getPrefixProperties(ApplicationContext run,String strPrefix) {
try {
// 初始化 k-v
init(run);
} catch (ReflectiveOperationException e) {
System.out.println("失败,将返回 new Map for null!");
}
//获取指定前缀数据
return conpareKeyPrefix(strPrefix,map);
}
private static Map<String, String> conpareKeyPrefix(String strPrefix, Map<String, String> map2) {
Map<String, String> m = new HashMap<>();
for (Entry<String, String> en : map2.entrySet()) {
if (en.getKey().startsWith(strPrefix)) {
try {
m.put(en.getKey().toString(), en.getValue());
} catch (Exception e) {
// OriginTrackedCharSequence 无法 toString 这是个问题
addMapEnery(m, en.toString());
}
}
}
return m;
}
/**
* 流程:
* 第一步 通过StandardServletEnvironment对象获取 AbstractEnvironment的 'propertyResolver' 成员变量的值 (别名a)
* 第二部 将 a 强转为 PropertySourcesPropertyResolver 类 (别名b)
* 第三部 获取 b 的 'propertySources' 成员变量值 (别名c)
* 第四步 将 c 转化为 PropertySources (二者类型匹配)
* 最后一步 便利调用 getSource()获取 数据 (Map)
*
* @param run
* @throws ReflectiveOperationException
*/
static void init(ApplicationContext run) throws ReflectiveOperationException {
//获取Environment子类
StandardServletEnvironment bean3 =
run.getBean(StandardServletEnvironment.class);
//获取类对象 AbstractEnvironment
Class<?> forName = Class.forName(AbstractEnvironment.class.getName());
//获取对应字段
Field declaredField = forName.getDeclaredField("propertyResolver");
declaredField.setAccessible(true);
//获取字段值 获取 StandardServletEnvironment 的 父类的父类 AbstractEnvironment
//的 ( ConfigurablePropertyResolver ) propertyResolver 成员变量 的 字段值
//详情 : ConfigurablePropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
Object object = declaredField.get(bean3);
//字段值 --> 类 向下强转 ,ConfigurablePropertyResolver 转 PropertySourcesPropertyResolver
PropertySourcesPropertyResolver p = (PropertySourcesPropertyResolver)object;
//获取类对象
Class<? extends PropertySourcesPropertyResolver> class2 = p.getClass();
//获取对应字段
Field declaredField2 = class2.getDeclaredField("propertySources");
declaredField2.setAccessible(true);
//获取字段值 PropertySources propertySources;
Object object2 = declaredField2.get(p);
//字段值 --> 类 配置集合 k(系统配置)- v(系统属性),k(本地配置)-v(本地属性)
PropertySources ss = (PropertySources)object2;
//得到全部数据 所有配置k-v
for (PropertySource<?> propertySource : ss) {
// 关键点 获取对应配置集合 k 的 属性集合
Object source = propertySource.getSource();
System.out.println(source);
if (source instanceof Map) {
map.putAll((Map)source);
}
}
}
private static void addMapEnery(Map<String, String> map2, String source) {
source = source.substring(1, source.length()-1);
String[] split = source.split("=");
map2.put(split[0], split[1]);
}