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相关推荐

  1. Spring - BeanDefinitionRegistryPostProcessor 扩展接口 动态注册bean

    文章目录 Pre org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor 接口的继承关系 BeanD ...

  2. 有什么办法动态更改yml的值吗_基于Redis实现Spring Cloud Gateway的动态管理

    转载本文需注明出处:微信公众号EAWorld,违者必究. 引言: Spring Cloud Gateway是当前使用非常广泛的一种API网关.它本身能力并不能完全满足企业对网关的期望,人们希望它可以提 ...

  3. 对xxl-job进行simpleTrigger并动态创建任务扩展

    2019独角兽企业重金招聘Python工程师标准>>> 博文原址:对xxl-job进行simpleTrigger并动态创建任务扩展 业务场景 需求上要求能实现quartz的simpl ...

  4. quartz 动态添加job_SpringBoot+Quartz实现动态管理定时任务

    前言: 最近在做Java开发对接微信公众平台之类的工作,在开发过程中遇到一个需求是定时任务这块的,但是普通的定时任务还远远不能满足:最后要实现的效果是每个任务都是不同的定时时间来执行而且是在前台页面上 ...

  5. JEE6 CDI 扩展实现 MVC (四) 实现多模板引擎支持,并提供扩展接口

    为什么80%的码农都做不了架构师?>>>    在上一篇中,我们添加了对 Freemarker的支持,这里我们再添加多 Velocity 的支持,同时留出扩展接口.方便用户自己添加模 ...

  6. SpringCloud的Archaius - 动态管理属性配置

    参考链接:http://www.th7.cn/Program/java/201608/919853.shtml 一.Archaius是什么? Archaius用于动态管理属性配置文件. 参考自Gett ...

  7. dmv io读写高的sql_使用内置的动态管理视图(DMV)发现更多SQL Server信息

    dmv io读写高的sql 介绍 (Introduction) This is the second article in a continuing series on the many system ...

  8. ThreadPoolTaskScheduler实现动态管理定时任务

    最近,有个项目有需要用到定时任务,所以做了一个动态管理定时任务的模块.本文将从项目背景.需求.选型.思路.具体实现等方面展开介绍. 背景:有个支付类的项目,中间会产生一些中间态的订单,需要有个定时任务 ...

  9. Springboot调度任务:动态管理

    前言 现在智能手表.手环,相信很多人都使用过,其中有一个功能,就是会有各种的提醒,如喝水提醒.运动提醒.远眺提醒,本质上根据用户的设置,间隔一定时间执行一个调度任务,提醒用户做某件事情.这篇文章将以这 ...

最新文章

  1. TVM自定义数据类型
  2. 禁用和删除Exchange邮箱深入探讨
  3. 品优购dubbox文档bug连环计,还是自己敲最实在!!!!!!
  4. afudos备份bios不动_AMI BIOS刷新程序AFUDOS操作说明及介绍-BIOS维修网站www.biosrepair.com...
  5. ABAP和XML数据格式互相转换的两种方式
  6. Java IO: Reader And Writer
  7. 七夕新浪漫,让AI黑科技带你们提前看看爱情的结晶
  8. 如何以出售开源软件为生?
  9. Nature最新封面:机器人进军考古界,破解3亿年前生物谜团 | 附Demo
  10. linux yum 安装播放器,centos6.5 常用影音播放器安装
  11. 京东价格监控软件开发技术探讨八:如何获取京东商品分类数据
  12. 最大规模开源中文语音数据集 — aidatatang_1505zh及其语音识别基准实验详解
  13. SqlHelper——只因为在人群中多看了你一眼
  14. Computer Science 领域文献检索 SCI、CPCI-S 和 EI
  15. 建立TCP连接的时候,syn包什么情况下会被对端rst?
  16. ios重签名工具ios-app-signer的使用
  17. 有哪些资本运作的经典案例?
  18. ipad协议临时号828版
  19. cg of spears storm_Steam 上的 Storm Of Spears RPG
  20. Google登录强制启用二次身份验证与FIDO解决方案

热门文章

  1. Open ROADS Community首次正式会议在新加坡顺利召开
  2. angularJS——模块
  3. OCR磁盘的导出和导入、备份和恢复以及移动(ocrconfig命令的应用)
  4. maven 工程启动找不到 Spring ContextLoaderListener 的解决办法
  5. linux Makefile编写的整理
  6. nginx 及 php 配置
  7. ob_start ob_end_clean的用法 fetch
  8. linux查看文件夹目录大小
  9. LINUX ulimit命令
  10. 设计模式-结构型-装饰