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參數(shù)類型為ConfigurableEnvironment,即應(yīng)用可以通過(guò)實(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)文章
springboot yml配置文件定義list集合、數(shù)組和map以及使用中的錯(cuò)誤
這篇文章主要介紹了springboot yml配置文件定義list集合、數(shù)組和map以及使用中遇到的錯(cuò)誤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
request.getParameter()方法的簡(jiǎn)單理解與運(yùn)用方式
在JavaWeb開(kāi)發(fā)中,request對(duì)象扮演著至關(guān)重要的角色,它是HTTP請(qǐng)求的封裝,request.getParameter()用于獲取客戶端通過(guò)GET或POST方式發(fā)送的參數(shù),與之相對(duì),request.setAttribute()用于在服務(wù)器端設(shè)置屬性,這些屬性只在一次請(qǐng)求中有效2024-10-10
Mybatis-Plus自動(dòng)生成的數(shù)據(jù)庫(kù)id過(guò)長(zhǎng)的解決
這篇文章主要介紹了Mybatis-Plus自動(dòng)生成的數(shù)據(jù)庫(kù)id過(guò)長(zhǎng)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringMVC數(shù)據(jù)響應(yīng)詳細(xì)介紹
Spring MVC 是 Spring 提供的一個(gè)基于 MVC 設(shè)計(jì)模式的輕量級(jí) Web 開(kāi)發(fā)框架,本質(zhì)上相當(dāng)于 Servlet,Spring MVC 角色劃分清晰,分工明細(xì),本章來(lái)講解SpringMVC數(shù)據(jù)響應(yīng)2023-02-02
Maven通過(guò)filtering標(biāo)簽讀取變量配置的三種方法
在日常開(kāi)發(fā)中,我們大多都會(huì)有開(kāi)發(fā)環(huán)境、測(cè)試環(huán)境、生產(chǎn)環(huán)境等,不同環(huán)境的參數(shù)肯定不一樣,本文主要介紹了Maven通過(guò)filtering標(biāo)簽讀取變量配置的三種方法,感興趣的可以了解一下2024-03-03

