通过spring-boot获取Application.properties值操作,整合多数据源(上)_janury的博客-程序员宝宝

技术标签: spring  application.properties  spring-boot  

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

}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/janury/article/details/88668832

智能推荐

T-SQL程序练习04_会洗碗的CV工程师的博客-程序员宝宝

本文同样是T-SQL的存储过程和游标的练习!!!

VMware Horizon 7 Security Server 安装错误28083的解决方法_weixin_30404405的博客-程序员宝宝

在安装VMware Horizon 7 Security Connection Server时,出现以下错误:Error 28083. IPsec setup failed. Please refer to the C:\users\...\...\vminst.log file for further details. The log reveals "Error: Failed to ...

(摘抄)SQLServer 中发布与订阅_雨水霂的博客-程序员宝宝

 在对数据库做迁移的时候,会有很多方法,用存储过程,job,也可以用开源工具kettle,那么今天这些天变接触到了一种新的方法,就是SqlServer中自带的发布与订阅。  首先说明一下数据复制的流程。如下图A是(192.168.210.170)上的数据库,B是(172.23.100.109)上的数据库。把B当作数据源,然后A从B上获取数据。 发布前准备:首先两个服务器之间要能...

html 标签引用外部文件时//的作用_html中 引用外部链接用//_当作看不见的博客-程序员宝宝

html 标签引用外部文件时//的作用在HTML 文件中时常会用到一系列图片,大多数时候都是使用相对路径或者绝对路径来加载对应的js css,还有img图片. 引用就不需多言 下面看一下引用的方式src="./*" 相对路径引用src="/*" 绝对路劲引用src="http://*" http 引用src="https://*" https 引用src="//*" 兼容模式引用那么兼

Linux监控平台搭建Zabbix(资源)_weixin_34261739的博客-程序员宝宝

2019独角兽企业重金招聘Python工程师标准&gt;&gt;&gt; ...

开始我的博客_weixin_30329623的博客-程序员宝宝

  一个优秀的程序员必须有一个博客,用来记录自己的代码经历。看了其他大牛的博客之后,我突然想开一个自己的博客。  我现在大二,学完c,html,css,java基础,正在学jdbc,数据库,javascript,ajax,jquery。我以后会把我的学习的过程记录在博客里。public class helloblog{ public static void main(Stri...

随便推点

相邻垂直外边距的塌陷_凶鸡的博客-程序员宝宝

垂直外边距的塌陷首先需要明确塌陷只会发生在块级元素的相邻垂直外边距间在使用CSS的垂直外边距样式时,相邻元素和父子元素都有可能发生相邻垂直外边距的塌陷(相邻元素是上方元素的下外边距和下方元素的上外边距之间,父子元素是父元素的上外边距和子元素的上外边距之间),为了在编写静态页面时避免由于垂直外边距的塌陷导致的布局混乱,需要了解塌陷时垂直外边距的表现和产生的原因——知道原因才能更好的解决问题塌陷时外边距的计算方式在《CSS权威指南》一书中对这种塌陷发生的描述非常的生动,书中将包围元素的外边距比作透明的塑

Document-oriented DB:MongoBD(1)_document-oriented 数据库_zealscott的博客-程序员宝宝

Introduce to Document-oriented Database: MongoBD.ConceptionDocument -&amp;gt; Collection -&amp;gt; DatabaseExample: MongoDB (using in Web App) using B-Tree as storage structureObject Oriented Progr...

mosquiito运行命令_mosquitto命令_南沙的星星的博客-程序员宝宝

[email protected]:/home/zone/z2# /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf1652700122: mosquitto version 1.4.15 (build date Tue, 18 Jun 2019 11:42:22 -0300) starting1652700122: Config loaded from /etc/mosquitto/mosquitto.conf.1652700122: Opening ipv4 l

【MySQL学习笔记】事务和MySQL中事务隔离级别修改_mysql 5.7修改事务隔离级别 rc_DivingKitten的博客-程序员宝宝

一、事务的概念事务:一组操作逻辑,使数据从一个状态转变到另一个状态。事务特性:原子性(atomicity):一个不可分割的工作单元,要么全部提交,要么全部回滚。一致性(consistency):事务执行前后,数据从一个合法状态转变到另一个合法状态隔离性(isolation):事务之间相互不干扰。持久性(durablility)事务被提交,则对数据修改时永久性的。事务状态:活动(active):事务正在执行中部分提交(partially committed):数据已提交到内存,

HDU 5478 Can you find it_KeyboardPianist的博客-程序员宝宝

2015 ACM/ICPC Asia Regional Shanghai Online引用一下公式:http://blog.csdn.net/queuelovestack/article/details/48754331#include #define ll long longll mod(ll a, ll b, ll n){ ll ret = 1; while(b

来一场蛋白和小分子的风花雪月_生信宝典的博客-程序员宝宝

分子对接(Molecular Docking)理论所谓分子对接就是两个或多个分子之间通过几何匹配和能量匹配相互识别找到最佳匹配模式的过程。分子对接对酶学研究和药物设计中有重要的应用意义。分子对接计算是在受体活性位点区域通过空间结构互补和能量最小化原则来搜寻配体与受体是否能产生相互作用以及它们之间的最佳结合模式。分子对接的思想起源于Fisher E的”钥匙和锁模型”,主要强调的是空间形状的匹配。...

推荐文章

热门文章

相关标签