惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 聂微东
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
罗磊的独立博客
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
博客园 - 司徒正美
博客园 - Franky
爱范儿
爱范儿
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
量子位
博客园 - 叶小钗
博客园_首页
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - 如.若

Linux系统负载过高问题排查 Git 配置 ssh-key 免密登录 cron表达式详解 AdoptOpenJDK(JAVA环境)安装 JVM优化之 -Xss -Xms -Xmx -Xmn 参数设置 JAVA 多线程基础 SQL 存储过程游标应用 SQL 语句实现查询子父节点 Spring ClassPathResource加载资源文件详解 浅谈web项目读取classpath路径下面的文件 SpringBoot+SpringSecurity+SpringSession实现一个前后端分离的权限管理系统 Spring Security 中 CSRF 防御源码解析 Redis 持久化 SpringBoot 如何防御 CSRF 攻击 SimpleDateFormat线程不安全的5种解决方案 数据库建模-规范 接口的幂等性怎么设计? TOP命令结果分析 Redis配置参数详解
Spring Boot 使用 EnvironmentPostProcessor 接口实现加载外...
如.若 · 2021-08-18 · via 博客园 - 如.若

简介

从Spring Boot 1.3开始,我们可以在应用程序上下文刷新之前使用EnvironmentPostProcessor来自定义应用程序的EnvironmentEnvironment表示当前应用程序运行的环境,它可以统一访问各种属性源中的属性,如属性文件、JVM系统属性、系统环境变量和Servlet上下文参数。使用EnvironmentPostProcessor可以在bean初始化之前对Environment进行修改。

示例

在 custom.properties 中定义一些配置,配置如下:

jdbc.root.user=mysql
jdbc.root.password=123456

1.实现EnvironmentPostProcessor

定义MyEnvironmentPostProcessor实现EnvironmentPostProcessor接口

public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        try{
            InputStream inputStream = new FileInputStream("/Users/SpringBoot/custom.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource("my",properties);
            environment.getPropertySources().addLast(propertiesPropertySource);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

}

如果你希望EnvironmentPostProcessor按照特定的顺序被调用,可以实现Ordered接口,或者使用@Order注解。

2.注册实现类

想要在Spring Boot启动过程中调用这个实现类,我们还需要在项目的resources目录中定义一个META-INF文件夹,然后在其下面先建spring.factories文件,在其中指定注册这个实现类:

org.springframework.boot.env.EnvironmentPostProcessor=com.test.springboot.processor.MyEnvironmentPostProcessor

3.测试

@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);
    }
}

 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注解的顺序去被调用。