动态管理配置文件扩展接口EnvironmentPostProcessor
SpringBoot支持动态的读取文件,留下的扩展接口org.springframework.boot.env.EnvironmentPostProcessor
。这个接口是spring包下的,使用这个进行配置文件的集中管理,而不需要每个项目都去配置配置文件。这种方法也是springboot框架留下的一个扩展(可以自己去扩展)
demo
在/Users/naeshihiroshi/study/studySummarize/SpringBoot/
(自己测试也可以随机在一个目录下建立一文件),目录下建立一个名为springboot.properties
文件,
springboot.properties
中定义一些配置,配置如下:
jdbc.root.user=zhihao.miao
jdbc.root.password=242312321
定义MyEnvironmentPostProcessor
实现EnvironmentPostProcessor
接口
@Component
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {@Overridepublic void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {try{InputStream inputStream = new FileInputStream("/Users/naeshihiroshi/study/studySummarize/SpringBoot/springboot.properties");Properties properties = new Properties();properties.load(inputStream);PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource("my",properties);environment.getPropertySources().addLast(propertiesPropertySource);}catch (IOException e){e.printStackTrace();}}
}
在classpath定义一个META-INF
文件夹然后在其下面先建spring.factories
文件,在其中指定:
org.springframework.boot.env.EnvironmentPostProcessor=com.zhihao.miao.processor.MyEnvironmentPostProcessor‘
启动类测试:
@SpringBootApplication
public class Application {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(Application.class,args);String username = context.getEnvironment().getProperty("jdbc.root.user");String password = context.getEnvironment().getProperty("jdbc.root.password");System.out.println("username==="+username);System.out.println("password==="+password);context.close();}
}
打印结果:
username===zhihao.miao
password===242312321
EnvironmentPostProcessor接口官网说明
官方文档如下
Allows for customization of the application's {@link Environment} prior to the application context being refreshed.
允许定制应用的上下文的应用环境优于应用的上下文之前被刷新。(意思就是在spring上下文构建之前可以设置一些系统配置)
EnvironmentPostProcessor implementations have to be registered in
META-INF/spring.factories, using the fully qualified name of this class as the key.
EnvironmentPostProcessor的实现类必须要在META-INF/spring.factories
文件中去注册,并且注册的是全类名。
EnvironmentPostProcessor processors are encouraged to detect whether Spring's
org.springframework.core.Ordered
Ordered interface has been implemented or if the@org.springframework.core.annotation.Order
Order annotation is present and to sort instances accordingly if so prior to invocation.
鼓励EnvironmentPostProcessor处理器检测Org.springframework.core.Ordered
注解,这样相应的实例也会按照@Order注解的顺序去被调用。
作者:二月_春风
链接:https://www.jianshu.com/p/be6c818fe6ff
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
动态管理配置文件扩展接口EnvironmentPostProcessor相关推荐
- Spring - BeanDefinitionRegistryPostProcessor 扩展接口 动态注册bean
文章目录 Pre org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor 接口的继承关系 BeanD ...
- 有什么办法动态更改yml的值吗_基于Redis实现Spring Cloud Gateway的动态管理
转载本文需注明出处:微信公众号EAWorld,违者必究. 引言: Spring Cloud Gateway是当前使用非常广泛的一种API网关.它本身能力并不能完全满足企业对网关的期望,人们希望它可以提 ...
- 对xxl-job进行simpleTrigger并动态创建任务扩展
2019独角兽企业重金招聘Python工程师标准>>> 博文原址:对xxl-job进行simpleTrigger并动态创建任务扩展 业务场景 需求上要求能实现quartz的simpl ...
- quartz 动态添加job_SpringBoot+Quartz实现动态管理定时任务
前言: 最近在做Java开发对接微信公众平台之类的工作,在开发过程中遇到一个需求是定时任务这块的,但是普通的定时任务还远远不能满足:最后要实现的效果是每个任务都是不同的定时时间来执行而且是在前台页面上 ...
- JEE6 CDI 扩展实现 MVC (四) 实现多模板引擎支持,并提供扩展接口
为什么80%的码农都做不了架构师?>>> 在上一篇中,我们添加了对 Freemarker的支持,这里我们再添加多 Velocity 的支持,同时留出扩展接口.方便用户自己添加模 ...
- SpringCloud的Archaius - 动态管理属性配置
参考链接:http://www.th7.cn/Program/java/201608/919853.shtml 一.Archaius是什么? Archaius用于动态管理属性配置文件. 参考自Gett ...
- dmv io读写高的sql_使用内置的动态管理视图(DMV)发现更多SQL Server信息
dmv io读写高的sql 介绍 (Introduction) This is the second article in a continuing series on the many system ...
- ThreadPoolTaskScheduler实现动态管理定时任务
最近,有个项目有需要用到定时任务,所以做了一个动态管理定时任务的模块.本文将从项目背景.需求.选型.思路.具体实现等方面展开介绍. 背景:有个支付类的项目,中间会产生一些中间态的订单,需要有个定时任务 ...
- Springboot调度任务:动态管理
前言 现在智能手表.手环,相信很多人都使用过,其中有一个功能,就是会有各种的提醒,如喝水提醒.运动提醒.远眺提醒,本质上根据用户的设置,间隔一定时间执行一个调度任务,提醒用户做某件事情.这篇文章将以这 ...
最新文章
- TVM自定义数据类型
- 禁用和删除Exchange邮箱深入探讨
- 品优购dubbox文档bug连环计,还是自己敲最实在!!!!!!
- afudos备份bios不动_AMI BIOS刷新程序AFUDOS操作说明及介绍-BIOS维修网站www.biosrepair.com...
- ABAP和XML数据格式互相转换的两种方式
- Java IO: Reader And Writer
- 七夕新浪漫,让AI黑科技带你们提前看看爱情的结晶
- 如何以出售开源软件为生?
- Nature最新封面:机器人进军考古界,破解3亿年前生物谜团 | 附Demo
- linux yum 安装播放器,centos6.5 常用影音播放器安装
- 京东价格监控软件开发技术探讨八:如何获取京东商品分类数据
- 最大规模开源中文语音数据集 — aidatatang_1505zh及其语音识别基准实验详解
- SqlHelper——只因为在人群中多看了你一眼
- Computer Science 领域文献检索 SCI、CPCI-S 和 EI
- 建立TCP连接的时候,syn包什么情况下会被对端rst?
- ios重签名工具ios-app-signer的使用
- 有哪些资本运作的经典案例?
- ipad协议临时号828版
- cg of spears storm_Steam 上的 Storm Of Spears RPG
- Google登录强制启用二次身份验证与FIDO解决方案