亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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)文章

  • maven的pom文件與打包詳解

    maven的pom文件與打包詳解

    pom文件定于了一個(gè)maven項(xiàng)目的maven配置,一般pom文件的放在項(xiàng)目或者模塊的根目錄下。本文詳細(xì)的介紹了pom文件配置,感興趣的可以了解一下
    2021-08-08
  • Java中Arrays.asList()需要注意的坑

    Java中Arrays.asList()需要注意的坑

    在Java中,我們經(jīng)常需要將數(shù)組轉(zhuǎn)換為L(zhǎng)ist來方便地進(jìn)行操作,Arrays.asList()方法是一種常見的方式,本文主要介紹了Java中Arrays.asList()需要注意的坑,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • springboot yml配置文件定義list集合、數(shù)組和map以及使用中的錯(cuò)誤

    springboot yml配置文件定義list集合、數(shù)組和map以及使用中的錯(cuò)誤

    這篇文章主要介紹了springboot yml配置文件定義list集合、數(shù)組和map以及使用中遇到的錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 新手初學(xué)Java常見排序算法

    新手初學(xué)Java常見排序算法

    排序(Sorting) 是計(jì)算機(jī)程序設(shè)計(jì)中的一種重要操作,它的功能是將一個(gè)數(shù)據(jù)元素(或記錄)的任意序列,重新排列成一個(gè)關(guān)鍵字有序的序列
    2021-07-07
  • 新手了解java基礎(chǔ)知識(shí)(一)

    新手了解java基礎(chǔ)知識(shí)(一)

    這篇文章主要介紹了Java基礎(chǔ)知識(shí),本文介紹了Java語言相關(guān)的基礎(chǔ)知識(shí)、歷史介紹、主要應(yīng)用方向等內(nèi)容,需要的朋友可以參考下,希望對(duì)你有所幫助
    2021-07-07
  • 分布式框架Zookeeper?api的使用介紹

    分布式框架Zookeeper?api的使用介紹

    Zookeeper作為?個(gè)分布式框架,主要用來解決分布式?致性問題,它提供了簡(jiǎn)單的分布式原語,并且對(duì)多種編程語?提供了API,所以接下來重點(diǎn)來看下Zookeeper的java客戶端API使用方式
    2022-09-09
  • request.getParameter()方法的簡(jiǎn)單理解與運(yùn)用方式

    request.getParameter()方法的簡(jiǎn)單理解與運(yùn)用方式

    在JavaWeb開發(fā)中,request對(duì)象扮演著至關(guān)重要的角色,它是HTTP請(qǐng)求的封裝,request.getParameter()用于獲取客戶端通過GET或POST方式發(fā)送的參數(shù),與之相對(duì),request.setAttribute()用于在服務(wù)器端設(shè)置屬性,這些屬性只在一次請(qǐng)求中有效
    2024-10-10
  • Mybatis-Plus自動(dòng)生成的數(shù)據(jù)庫(kù)id過長(zhǎng)的解決

    Mybatis-Plus自動(dòng)生成的數(shù)據(jù)庫(kù)id過長(zhǎng)的解決

    這篇文章主要介紹了Mybatis-Plus自動(dòng)生成的數(shù)據(jù)庫(kù)id過長(zhǎng)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • SpringMVC數(shù)據(jù)響應(yīng)詳細(xì)介紹

    SpringMVC數(shù)據(jù)響應(yīng)詳細(xì)介紹

    Spring MVC 是 Spring 提供的一個(gè)基于 MVC 設(shè)計(jì)模式的輕量級(jí) Web 開發(fā)框架,本質(zhì)上相當(dāng)于 Servlet,Spring MVC 角色劃分清晰,分工明細(xì),本章來講解SpringMVC數(shù)據(jù)響應(yīng)
    2023-02-02
  • Maven通過filtering標(biāo)簽讀取變量配置的三種方法

    Maven通過filtering標(biāo)簽讀取變量配置的三種方法

    在日常開發(fā)中,我們大多都會(huì)有開發(fā)環(huán)境、測(cè)試環(huán)境、生產(chǎn)環(huán)境等,不同環(huán)境的參數(shù)肯定不一樣,本文主要介紹了Maven通過filtering標(biāo)簽讀取變量配置的三種方法,感興趣的可以了解一下
    2024-03-03

最新評(píng)論