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,即應用可以通過實現(xià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事件時觸發(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實現(xiàn)了postProcessEnvironment方法,它額外加載com/example/myapp/config.yml里頭的配置最為最后的propertySource
小結
springboot的EnvironmentPostProcessor提供了一個environment的擴展接口,方便應用去做environment的擴展,比如擴展propertySource等
以上就是springboot的EnvironmentPostProcessor接口方法源碼解析的詳細內容,更多關于springboot EnvironmentPostProcessor的資料請關注腳本之家其它相關文章!
相關文章
springboot yml配置文件定義list集合、數(shù)組和map以及使用中的錯誤
這篇文章主要介紹了springboot yml配置文件定義list集合、數(shù)組和map以及使用中遇到的錯誤問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07request.getParameter()方法的簡單理解與運用方式
在JavaWeb開發(fā)中,request對象扮演著至關重要的角色,它是HTTP請求的封裝,request.getParameter()用于獲取客戶端通過GET或POST方式發(fā)送的參數(shù),與之相對,request.setAttribute()用于在服務器端設置屬性,這些屬性只在一次請求中有效2024-10-10Mybatis-Plus自動生成的數(shù)據庫id過長的解決
這篇文章主要介紹了Mybatis-Plus自動生成的數(shù)據庫id過長的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12