From 44b9a6c8bcf750eb21723684607b0abd7f0e3ed4 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Tue, 21 Jan 2025 16:23:41 +0800 Subject: [PATCH] Reuse `EnvironmentPostProcessorsFactory` provided by Spring Boot Signed-off-by: Yanming Zhou --- .../refresh/ConfigDataContextRefresher.java | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresher.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresher.java index 4a5d813e5..ce7fa904c 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresher.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresher.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,14 +22,12 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.boot.BootstrapContext; -import org.springframework.boot.BootstrapRegistry; import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.DefaultBootstrapContext; import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.boot.env.EnvironmentPostProcessorsFactory; import org.springframework.boot.logging.DeferredLogFactory; -import org.springframework.boot.util.Instantiator; import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; import org.springframework.cloud.context.config.ContextRefreshedWithApplicationEvent; import org.springframework.cloud.context.scope.refresh.RefreshScope; @@ -38,11 +36,11 @@ import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; -import org.springframework.core.io.support.SpringFactoriesLoader; /** * @author Dave Syer * @author Venil Noronha + * @author Yanming Zhou */ public class ConfigDataContextRefresher extends ContextRefresher implements ApplicationListener { @@ -77,17 +75,10 @@ protected void updateEnvironment() { // decrypt happen after refresh. The hard coded call to // ConfigDataEnvironmentPostProcessor.applyTo() is now automated as well. DeferredLogFactory logFactory = new PassthruDeferredLogFactory(); - List classNames = SpringFactoriesLoader.loadFactoryNames(EnvironmentPostProcessor.class, - getClass().getClassLoader()); - Instantiator instantiator = new Instantiator<>(EnvironmentPostProcessor.class, - (parameters) -> { - parameters.add(DeferredLogFactory.class, logFactory); - parameters.add(Log.class, logFactory::getLog); - parameters.add(ConfigurableBootstrapContext.class, bootstrapContext); - parameters.add(BootstrapContext.class, bootstrapContext); - parameters.add(BootstrapRegistry.class, bootstrapContext); - }); - List postProcessors = instantiator.instantiate(classNames); + EnvironmentPostProcessorsFactory postProcessorsFactory = EnvironmentPostProcessorsFactory + .fromSpringFactories(getClass().getClassLoader()); + List postProcessors = postProcessorsFactory.getEnvironmentPostProcessors(logFactory, + bootstrapContext); for (EnvironmentPostProcessor postProcessor : postProcessors) { postProcessor.postProcessEnvironment(environment, application); }