通过spring-boot获取Application.properties值操作,整合多数据源(上)

通过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]);
	
}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值