springboot的EnvironmentPostProcessor接口方法源码解析

 更新时间:2023年08月24日 09:45:54   作者:codecraft  
这篇文章主要介绍了springboot的EnvironmentPostProcessor接口方法源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下springboot的EnvironmentPostProcessor

EnvironmentPostProcessor

org/springframework/boot/env/EnvironmentPostProcessor.java

@FunctionalInterface
public interface EnvironmentPostProcessor {

    /**
     * Post-process the given {@code environment}.
     * @param environment the environment to post-process
     * @param application the application to which the environment belongs
     */
    void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application);

}

 springboot提供了EnvironmentPostProcessor接口,该接口有postProcessEnvironment方法,其中envrionment参数类型为ConfigurableEnvironment,即应用可以通过实现这个接口进行env环境变量的操作

EnvironmentPostProcessorApplicationListener

org/springframework/boot/env/EnvironmentPostProcessorApplicationListener.java

/**
 * {@link SmartApplicationListener} used to trigger {@link EnvironmentPostProcessor
 * EnvironmentPostProcessors} registered in the {@code spring.factories} file.
 *
 * @author Phillip Webb
 * @since 2.4.0
 */
public class EnvironmentPostProcessorApplicationListener implements SmartApplicationListener, Ordered {
    /**
     * The default order for the processor.
     */
    public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
    private final DeferredLogs deferredLogs;
    private int order = DEFAULT_ORDER;
    private final EnvironmentPostProcessorsFactory postProcessorsFactory;
    /**
     * Create a new {@link EnvironmentPostProcessorApplicationListener} with
     * {@link EnvironmentPostProcessor} classes loaded via {@code spring.factories}.
     */
    public EnvironmentPostProcessorApplicationListener() {
        this(EnvironmentPostProcessorsFactory
                .fromSpringFactories(EnvironmentPostProcessorApplicationListener.class.getClassLoader()));
    }
    /**
     * Create a new {@link EnvironmentPostProcessorApplicationListener} with post
     * processors created by the given factory.
     * @param postProcessorsFactory the post processors factory
     */
    public EnvironmentPostProcessorApplicationListener(EnvironmentPostProcessorsFactory postProcessorsFactory) {
        this(postProcessorsFactory, new DeferredLogs());
    }
    EnvironmentPostProcessorApplicationListener(EnvironmentPostProcessorsFactory postProcessorsFactory,
            DeferredLogs deferredLogs) {
        this.postProcessorsFactory = postProcessorsFactory;
        this.deferredLogs = deferredLogs;
    }
    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
        return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(eventType)
                || ApplicationPreparedEvent.class.isAssignableFrom(eventType)
                || ApplicationFailedEvent.class.isAssignableFrom(eventType);
    }
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationEnvironmentPreparedEvent) {
            onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
        }
        if (event instanceof ApplicationPreparedEvent) {
            onApplicationPreparedEvent((ApplicationPreparedEvent) event);
        }
        if (event instanceof ApplicationFailedEvent) {
            onApplicationFailedEvent((ApplicationFailedEvent) event);
        }
    }
    private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
        ConfigurableEnvironment environment = event.getEnvironment();
        SpringApplication application = event.getSpringApplication();
        for (EnvironmentPostProcessor postProcessor : getEnvironmentPostProcessors(event.getBootstrapContext())) {
            postProcessor.postProcessEnvironment(environment, application);
        }
    }
    private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
        finish();
    }
    private void onApplicationFailedEvent(ApplicationFailedEvent event) {
        finish();
    }
    private void finish() {
        this.deferredLogs.switchOverAll();
    }
    List<EnvironmentPostProcessor> getEnvironmentPostProcessors(ConfigurableBootstrapContext bootstrapContext) {
        return this.postProcessorsFactory.getEnvironmentPostProcessors(this.deferredLogs, bootstrapContext);
    }
    @Override
    public int getOrder() {
        return this.order;
    }
    public void setOrder(int order) {
        this.order = order;
    }
}

EnvironmentPostProcessorApplicationListener用于在接收到ApplicationEnvironmentPreparedEvent事件时触发执行EnvironmentPostProcessor的postProcessEnvironment方法

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.ClearCachesApplicationListener,\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.context.logging.LoggingApplicationListener,\
org.springframework.boot.env.EnvironmentPostProcessorApplicationListener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener

示例

public class EnvironmentPostProcessorExample implements EnvironmentPostProcessor {
    private final YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        Resource path = new ClassPathResource("com/example/myapp/config.yml");
        PropertySource<?> propertySource = loadYaml(path);
        environment.getPropertySources().addLast(propertySource);
    }
    private PropertySource<?> loadYaml(Resource path) {
        if (!path.exists()) {
            throw new IllegalArgumentException("Resource " + path + " does not exist");
        }
        try {
            return this.loader.load("custom-resource", path).get(0);
        }
        catch (IOException ex) {
            throw new IllegalStateException("Failed to load yaml configuration from " + path, ex);
        }
    }
}

 EnvironmentPostProcessorExample实现了postProcessEnvironment方法,它额外加载com/example/myapp/config.yml里头的配置最为最后的propertySource

小结

springboot的EnvironmentPostProcessor提供了一个environment的扩展接口,方便应用去做environment的扩展,比如扩展propertySource等

以上就是springboot的EnvironmentPostProcessor接口方法源码解析的详细内容,更多关于springboot EnvironmentPostProcessor的资料请关注脚本之家其它相关文章!

相关文章

  • 解决idea中servlet报红问题

    解决idea中servlet报红问题

    这篇文章主要介绍了解决idea中servlet报红问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • SpringBoot 定时任务遇到的坑

    SpringBoot 定时任务遇到的坑

    这篇文章主要介绍了SpringBoot 定时任务遇到的坑,今天踩的这个坑和 cron 表达式有关,文中给大家介绍了cron 表达式的解释,需要的朋友一起看看吧
    2017-11-11
  • Servlet简单实现登录功能

    Servlet简单实现登录功能

    这篇文章主要为大家详细介绍了Servlet简单实现登录功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-03-03
  • Spring Boot 教程之创建项目的三种方式

    Spring Boot 教程之创建项目的三种方式

    这篇文章主要分享了Spring Boot 教程之创建项目的三种方式,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-05-05
  • 使用Java代码实现Redis和数据库数据同步

    使用Java代码实现Redis和数据库数据同步

    这篇文章主要介绍了使用Java代码实现Redis和数据库数据同步问题,文中通过代码示例给大家讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-06-06
  • 解决spring boot 配置文件后缀的一个坑

    解决spring boot 配置文件后缀的一个坑

    这篇文章主要介绍了spring boot 配置文件后缀的一个坑,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • java实现短地址服务的方法(附代码)

    java实现短地址服务的方法(附代码)

    大多数情况下URL太长,字符多,不便于发布复制和存储,本文就介绍了通过java实现短地址服务,减少了许多使用太长URL带来的不便,需要的朋友可以参考下
    2015-07-07
  • JavaSE经典小练习项目之拷贝文件夹

    JavaSE经典小练习项目之拷贝文件夹

    文件拷贝是一个常见的任务,无论是备份文件,还是将文件从一个位置复制到另一个位置,文件拷贝都是必不可少的,这篇文章主要给大家介绍了关于JavaSE经典小练习项目之拷贝文件夹的相关资料,需要的朋友可以参考下
    2023-10-10
  • Java8 LocalDateTime时间日期类使用实例详解

    Java8 LocalDateTime时间日期类使用实例详解

    本文从 LocalDateTime 类的创建、转换、格式化与解析、计算与比较以及其他操作几个方面详细介绍了 LocalDateTime 类在 Java 8 中的使用,感兴趣的朋友跟随小编一起看看吧
    2024-03-03
  • 详解Java中JSON数据的生成与解析

    详解Java中JSON数据的生成与解析

    今天给大家带来的是关于Java的相关知识,文章围绕着Java中JSON数据的生成与解析展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06

最新评论