关于java:SpringBoot扩展点EnvironmentPostProcessor

2次阅读

共计 4454 个字符,预计需要花费 12 分钟才能阅读完成。

一、背景

之前我的项目中用到了 Apollo 配置核心,对接 Apollo 配置核心后,配置核心的属性就能够在程序中应用了,那么这个是怎么实现的呢?配置核心的属性又是何时加载到程序中的呢?那么咱们如果找到了这个是怎么实现的是否就能够 从任何中央加载配置属性 配置属性的加解密性能呢

二、需要


从上图中得悉,咱们的需要很简略,即咱们本人定义的属性须要比配置文件中的优先级更高。

三、剖析

1、什么时候向 SpringBoot 中退出咱们本人的配置属性

当咱们想在 Bean 中应用配置属性时,那么咱们的配置属性必须在 Bean 实例化之前就放入到 Spring 到 Environment 中。即咱们的接口须要在 application context refreshed 之前进行调用,而 EnvironmentPostProcessor 正好能够实现这个性能。

2、获取配置属性的优先级

咱们晓得在 Spring 中获取属性是有优先级的。
比方咱们存在如下配置属性 username

├─application.properties
│   >> username=huan
├─application-dev.properties
│   >> username=huan.fu

那么此时 username 的值是什么呢?此处借用 Apollo的一张图来说解释一下这个问题。

参考链接:https://www.apolloconfig.com/#/zh/design/apollo-design

Spring 从 3.1 版本开始减少了 ConfigurableEnvironmentPropertySource

ConfigurableEnvironment

  • Spring 的 ApplicationContext 会蕴含一个 Environment(实现 ConfigurableEnvironment 接口)
  • ConfigurableEnvironment 本身蕴含了很多个 PropertySource

PropertySource

  • 属性源
  • 能够了解为很多个 Key – Value 的属性配置

由上方的原理图可知,key在最开始呈现的 PropertySource 中的优先级更高,下面的例子在 SpringBootusername的值为huan.fu

3、何时退出咱们本人的配置

由第二步 获取配置属性的优先级 可知,PropertySource 越靠前越先执行,那么要咱们配置失效,就必须放在越后面越好。

由上图可知,SpringBoot 加载各种配置是通过 EnvironmentPostProcessor 来实现的,而具体的实现是 ConfigDataEnvironmentPostProcessor 来实现的。那么咱们本人编写一个 EnvironmentPostProcessor 的实现类,而后在 ConfigDataEnvironmentPostProcessor 后执行,并退出到 Environment中的第一位即可。

四、实现

1、引入 SpringBoot 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.huan.springcloud</groupId>
    <artifactId>springboot-extension-point</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-extension-point</name>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

2、在 application.properties 中配置属性

vim application.properties

username=huan

3、编写自定义属性并退出 Spring Environment 中


留神:
1、如果发现程序中日志没有输入,查看是否应用了 slf4j 输入日志,此时因为日志零碎未初始化无奈输入日志。解决办法如下:

SpringBoot 版本
        >= 2.4 能够参考上图中的应用 DeferredLogFactory 来输入日志
        < 2.4
            1、参考如下链接 https://stackoverflow.com/questions/42839798/how-to-log-errors-in-a-environmentpostprocessor-execution
            2、外围代码:@Component
                public class MyEnvironmentPostProcessor implements
                        EnvironmentPostProcessor, ApplicationListener<ApplicationEvent> {private static final DeferredLog log = new DeferredLog();
                    @Override
                    public void postProcessEnvironment(ConfigurableEnvironment env, SpringApplication app) {log.error("This should be printed");
                    }
                    @Override
                    public void onApplicationEvent(ApplicationEvent event) {log.replayTo(MyEnvironmentPostProcessor.class);
                    }
                }

4、通过 SPI 使自定义的配置失效

1、在 src/main/resources下新建 META-INF/spring.factories 文件

2、配置

org.springframework.boot.env.EnvironmentPostProcessor=\
  com.huan.springcloud.extensionpoint.environmentpostprocessor.CustomEnvironmentPostProcessor

5、编写测试类,输入定义的 username 属性的值

@Component
public class PrintCustomizeEnvironmentProperty implements ApplicationRunner {private static final Logger log = LoggerFactory.getLogger(PrintCustomizeEnvironmentProperty.class);

    @Value("${username}")
    private String userName;

    @Override
    public void run(ApplicationArguments args) {log.info("获取到的 username 的属性值为: {}", userName);
    }
}

6、运行后果

五、注意事项

1、日志无奈输入

参考上方的 3、编写自定义属性并退出 Spring Environment 中 提供的解决方案。

2、配置没有失效

  • 查看 EnvironmentPostProcessor 的优先级,看看是否 @Order 或者 Ordered 返回的优先级值不对。
  • 看看别的中央是否实现了 EnvironmentPostProcessorApplicationContextInitializerBeanFactoryPostProcessorBeanDefinitionRegistryPostProcessor 等这些接口,在这个外面批改了 PropertySource的程序。
  • 了解 Spring 获取获取属性的程序 参考 2、获取配置属性的优先级

3、日志零碎如何初始化

如下代码初始化日志零碎

org.springframework.boot.context.logging.LoggingApplicationListener

六、残缺代码

https://gitee.com/huan1993/spring-cloud-parent/tree/master/springboot/springboot-extension-point/src/main/java/com/huan/springcloud/extensionpoint/environmentpostprocessor

七、参考链接

1、https://github.com/apolloconfig/apollo/blob/master/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializer.java
2、https://github.com/apolloconfig/apollo/blob/master/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesProcessor.java

3、https://www.apolloconfig.com/#/zh/design/apollo-design

4、解决 EnvironmentPostProcessor 中无奈输入日志

5、https://docs.spring.io/spring-boot/docs/2.6.6/reference/htmlsingle/#howto.application.customize-the-environment-or-application-context

正文完
 0