最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot的EnvironmentPostProcessor接口方法源碼解析

 更新時(shí)間:2023年08月24日 09:45:54   作者:codecraft  
這篇文章主要介紹了springboot的EnvironmentPostProcessor接口方法源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

本文主要研究一下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參數(shù)類型為ConfigurableEnvironment,即應(yīng)用可以通過實(shí)現(xiàn)這個(gè)接口進(jìn)行env環(huán)境變量的操作

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事件時(shí)觸發(fā)執(zhí)行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實(shí)現(xiàn)了postProcessEnvironment方法,它額外加載com/example/myapp/config.yml里頭的配置最為最后的propertySource

小結(jié)

springboot的EnvironmentPostProcessor提供了一個(gè)environment的擴(kuò)展接口,方便應(yīng)用去做environment的擴(kuò)展,比如擴(kuò)展propertySource等

以上就是springboot的EnvironmentPostProcessor接口方法源碼解析的詳細(xì)內(nèi)容,更多關(guān)于springboot EnvironmentPostProcessor的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 解決idea中servlet報(bào)紅問題

    解決idea中servlet報(bào)紅問題

    這篇文章主要介紹了解決idea中servlet報(bào)紅問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • SpringBoot 定時(shí)任務(wù)遇到的坑

    SpringBoot 定時(shí)任務(wù)遇到的坑

    這篇文章主要介紹了SpringBoot 定時(shí)任務(wù)遇到的坑,今天踩的這個(gè)坑和 cron 表達(dá)式有關(guān),文中給大家介紹了cron 表達(dá)式的解釋,需要的朋友一起看看吧
    2017-11-11
  • Servlet簡(jiǎn)單實(shí)現(xiàn)登錄功能

    Servlet簡(jiǎn)單實(shí)現(xiàn)登錄功能

    這篇文章主要為大家詳細(xì)介紹了Servlet簡(jiǎn)單實(shí)現(xiàn)登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • Spring?Boot?教程之創(chuàng)建項(xiàng)目的三種方式

    Spring?Boot?教程之創(chuàng)建項(xiàng)目的三種方式

    這篇文章主要分享了Spring?Boot?教程之創(chuàng)建項(xiàng)目的三種方式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05
  • 使用Java代碼實(shí)現(xiàn)Redis和數(shù)據(jù)庫數(shù)據(jù)同步

    使用Java代碼實(shí)現(xiàn)Redis和數(shù)據(jù)庫數(shù)據(jù)同步

    這篇文章主要介紹了使用Java代碼實(shí)現(xiàn)Redis和數(shù)據(jù)庫數(shù)據(jù)同步問題,文中通過代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-06-06
  • 解決spring boot 配置文件后綴的一個(gè)坑

    解決spring boot 配置文件后綴的一個(gè)坑

    這篇文章主要介紹了spring boot 配置文件后綴的一個(gè)坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java實(shí)現(xiàn)短地址服務(wù)的方法(附代碼)

    java實(shí)現(xiàn)短地址服務(wù)的方法(附代碼)

    大多數(shù)情況下URL太長(zhǎng),字符多,不便于發(fā)布復(fù)制和存儲(chǔ),本文就介紹了通過java實(shí)現(xiàn)短地址服務(wù),減少了許多使用太長(zhǎng)URL帶來的不便,需要的朋友可以參考下
    2015-07-07
  • JavaSE經(jīng)典小練習(xí)項(xiàng)目之拷貝文件夾

    JavaSE經(jīng)典小練習(xí)項(xiàng)目之拷貝文件夾

    文件拷貝是一個(gè)常見的任務(wù),無論是備份文件,還是將文件從一個(gè)位置復(fù)制到另一個(gè)位置,文件拷貝都是必不可少的,這篇文章主要給大家介紹了關(guān)于JavaSE經(jīng)典小練習(xí)項(xiàng)目之拷貝文件夾的相關(guān)資料,需要的朋友可以參考下
    2023-10-10
  • Java8?LocalDateTime時(shí)間日期類使用實(shí)例詳解

    Java8?LocalDateTime時(shí)間日期類使用實(shí)例詳解

    本文從 LocalDateTime 類的創(chuàng)建、轉(zhuǎn)換、格式化與解析、計(jì)算與比較以及其他操作幾個(gè)方面詳細(xì)介紹了 LocalDateTime 類在 Java 8 中的使用,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • 詳解Java中JSON數(shù)據(jù)的生成與解析

    詳解Java中JSON數(shù)據(jù)的生成與解析

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著Java中JSON數(shù)據(jù)的生成與解析展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06

最新評(píng)論

灵石县| 大埔区| 柘城县| 淅川县| 顺义区| 新安县| 云安县| 清丰县| 礼泉县| 南溪县| 古田县| 辽阳县| 黔西| 石景山区| 广平县| 定州市| 广元市| 扬中市| 塔城市| 辽阳市| 新源县| 紫云| 县级市| 泰州市| 开远市| 汕头市| 垣曲县| 古丈县| 手机| 江永县| 甘谷县| 徐州市| 常宁市| 拜城县| 正镶白旗| 宝鸡市| 镇宁| 河池市| 玉屏| 柞水县| 沂水县|