From 0cb9e44307fbcea8b2e9c0158224c3e0e072ca55 Mon Sep 17 00:00:00 2001 From: liaozan <378024053@qq.com> Date: Sun, 2 Jul 2023 20:17:02 +0800 Subject: [PATCH] Add more member to ConfigLoadedEvent --- ...onInitializerEnvironmentPostProcessor.java | 2 +- .../apollo/ConfigurablePropertiesLoader.java | 8 +++--- .../apollo/event/ConfigLoadedEvent.java | 9 ++++--- .../DubboConfigInitEventListener.java | 7 +++-- .../DubboConfigLoadedEventListener.java | 11 ++------ .../BootstrapContextClosedEventListener.java | 27 +++++++++++++++++++ .../LoggerAwareEnvironmentPostProcessor.java | 14 ++++++---- 7 files changed, 54 insertions(+), 24 deletions(-) create mode 100644 support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/BootstrapContextClosedEventListener.java diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloConfigurationInitializerEnvironmentPostProcessor.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloConfigurationInitializerEnvironmentPostProcessor.java index 764ea03..1b3e488 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloConfigurationInitializerEnvironmentPostProcessor.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloConfigurationInitializerEnvironmentPostProcessor.java @@ -38,7 +38,7 @@ public class ApolloConfigurationInitializerEnvironmentPostProcessor extends Logg public ApolloConfigurationInitializerEnvironmentPostProcessor(DeferredLogFactory deferredLogFactory, ConfigurableBootstrapContext bootstrapContext) { super(deferredLogFactory, bootstrapContext); - this.configurablePropertiesLoader = new ConfigurablePropertiesLoader(deferredLogFactory); + this.configurablePropertiesLoader = new ConfigurablePropertiesLoader(deferredLogFactory, bootstrapContext); } @Override diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java index 8a3eaab..ed48309 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java @@ -10,6 +10,7 @@ import com.schbrain.framework.autoconfigure.apollo.properties.ApolloProperties; import com.schbrain.framework.autoconfigure.apollo.util.ConfigUtils; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.logging.Log; +import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.SpringApplication; import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.context.ApplicationListener; @@ -31,12 +32,13 @@ import static org.springframework.core.io.support.SpringFactoriesLoader.loadFact class ConfigurablePropertiesLoader { private final Log log; - private final DeferredLogFactory deferredLogFactory; + private final ConfigurableBootstrapContext bootstrapContext; - ConfigurablePropertiesLoader(DeferredLogFactory deferredLogFactory) { + ConfigurablePropertiesLoader(DeferredLogFactory deferredLogFactory, ConfigurableBootstrapContext bootstrapContext) { this.deferredLogFactory = deferredLogFactory; this.log = deferredLogFactory.getLog(ConfigurablePropertiesLoader.class); + this.bootstrapContext = bootstrapContext; } /** @@ -83,7 +85,7 @@ class ConfigurablePropertiesLoader { private ConfigLoadedEvent createEvent(ConfigurableEnvironment environment, SpringApplication application, OrderedMapPropertySource propertySource, ConfigurableProperties properties) { ConfigurableProperties boundProperties = properties.bind(environment); - return new ConfigLoadedEvent(environment, deferredLogFactory, propertySource, boundProperties, application); + return new ConfigLoadedEvent(environment, deferredLogFactory, propertySource, boundProperties, application, bootstrapContext); } private ApplicationEventMulticaster createEventMulticaster(Set> listeners) { diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/event/ConfigLoadedEvent.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/event/ConfigLoadedEvent.java index 62a2314..673d2d8 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/event/ConfigLoadedEvent.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/event/ConfigLoadedEvent.java @@ -3,6 +3,7 @@ package com.schbrain.framework.autoconfigure.apollo.event; import com.schbrain.common.util.properties.OrderedMapPropertySource; import com.schbrain.common.util.support.ConfigurableProperties; import lombok.Getter; +import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.SpringApplication; import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.context.ApplicationEvent; @@ -18,23 +19,23 @@ public class ConfigLoadedEvent extends ApplicationEvent { private static final long serialVersionUID = 2567291189881702459L; private final ConfigurableEnvironment environment; - private final DeferredLogFactory deferredLogFactory; - private final OrderedMapPropertySource propertySource; - private final SpringApplication springApplication; + private final ConfigurableBootstrapContext bootstrapContext; public ConfigLoadedEvent(ConfigurableEnvironment environment, DeferredLogFactory deferredLogFactory, OrderedMapPropertySource propertySource, ConfigurableProperties properties, - SpringApplication springApplication) { + SpringApplication springApplication, + ConfigurableBootstrapContext bootstrapContext) { super(properties); this.environment = environment; this.propertySource = propertySource; this.deferredLogFactory = deferredLogFactory; this.springApplication = springApplication; + this.bootstrapContext = bootstrapContext; } public ConfigurableProperties getConfigurableProperties() { diff --git a/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/listener/DubboConfigInitEventListener.java b/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/listener/DubboConfigInitEventListener.java index 0a2b105..d396ae0 100644 --- a/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/listener/DubboConfigInitEventListener.java +++ b/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/listener/DubboConfigInitEventListener.java @@ -2,9 +2,11 @@ package com.schbrain.framework.autoconfigure.dubbo.listener; import org.apache.dubbo.config.ConfigCenterConfig; import org.apache.dubbo.config.context.ConfigManager; +import org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor; import org.apache.dubbo.config.spring.context.event.DubboConfigInitEvent; import org.apache.dubbo.config.spring.util.DubboBeanUtils; import org.apache.dubbo.rpc.model.ApplicationModel; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.PriorityOrdered; @@ -15,6 +17,7 @@ import static org.apache.dubbo.config.spring.util.EnvironmentUtils.filterDubboPr /** * @author liaozan + * @see ReferenceAnnotationBeanPostProcessor#postProcessBeanFactory(ConfigurableListableBeanFactory) * @since 2023-05-08 */ class DubboConfigInitEventListener implements ApplicationListener, PriorityOrdered { @@ -42,8 +45,8 @@ class DubboConfigInitEventListener implements ApplicationListener externalConfiguration = filterDubboProperties(applicationContext.getEnvironment()); ConfigCenterConfig configCenterConfig = new ConfigCenterConfig(); - configCenterConfig.setExternalConfig(externalConfiguration); + configCenterConfig.setAppExternalConfig(externalConfiguration); return configCenterConfig; } -} +} \ No newline at end of file diff --git a/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/listener/DubboConfigLoadedEventListener.java b/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/listener/DubboConfigLoadedEventListener.java index 321d0e4..5aefdc3 100644 --- a/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/listener/DubboConfigLoadedEventListener.java +++ b/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/listener/DubboConfigLoadedEventListener.java @@ -1,6 +1,5 @@ package com.schbrain.framework.autoconfigure.dubbo.listener; -import com.schbrain.common.util.ApplicationName; import com.schbrain.common.util.properties.OrderedMapPropertySource; import com.schbrain.framework.autoconfigure.apollo.event.ConfigLoadedEvent; import com.schbrain.framework.autoconfigure.apollo.event.listener.GenericConfigLoadedEventListener; @@ -8,7 +7,6 @@ import com.schbrain.framework.autoconfigure.dubbo.properties.DubboProperties; import org.springframework.boot.SpringApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.Ordered; -import org.springframework.core.env.ConfigurableEnvironment; import static org.apache.dubbo.config.ConfigKeys.DUBBO_SCAN_BASE_PACKAGES; @@ -18,8 +16,6 @@ import static org.apache.dubbo.config.ConfigKeys.DUBBO_SCAN_BASE_PACKAGES; */ public class DubboConfigLoadedEventListener extends GenericConfigLoadedEventListener { - public static final String DUBBO_APPLICATION_NAME = "dubbo.application.name"; - @Override public int getOrder() { return Ordered.HIGHEST_PRECEDENCE; @@ -32,16 +28,13 @@ public class DubboConfigLoadedEventListener extends GenericConfigLoadedEventList @Override protected void onConfigLoaded(ConfigLoadedEvent event, DubboProperties properties) { - addRequiredProperties(event.getEnvironment(), event.getSpringApplication(), event.getPropertySource()); + addRequiredProperties(event.getSpringApplication(), event.getPropertySource()); } - private void addRequiredProperties(ConfigurableEnvironment environment, SpringApplication application, OrderedMapPropertySource propertySource) { + private void addRequiredProperties(SpringApplication application, OrderedMapPropertySource propertySource) { if (!propertySource.containsProperty(DUBBO_SCAN_BASE_PACKAGES)) { propertySource.addProperty(DUBBO_SCAN_BASE_PACKAGES, getBasePackage(application)); } - if (!propertySource.containsProperty(DUBBO_APPLICATION_NAME)) { - propertySource.addProperty(DUBBO_APPLICATION_NAME, ApplicationName.get(environment)); - } } private String getBasePackage(SpringApplication application) { diff --git a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/BootstrapContextClosedEventListener.java b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/BootstrapContextClosedEventListener.java new file mode 100644 index 0000000..e71224c --- /dev/null +++ b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/BootstrapContextClosedEventListener.java @@ -0,0 +1,27 @@ +package com.schbrain.framework.support.spring; + +import org.springframework.boot.BootstrapContextClosedEvent; +import org.springframework.boot.ConfigurableBootstrapContext; +import org.springframework.context.ApplicationListener; +import org.springframework.context.ConfigurableApplicationContext; + +/** + * @author liaozan + * @since 2023-07-02 + */ +class BootstrapContextClosedEventListener implements ApplicationListener { + + private final LoggerAwareEnvironmentPostProcessor delegate; + + BootstrapContextClosedEventListener(LoggerAwareEnvironmentPostProcessor delegate) { + this.delegate = delegate; + } + + @Override + public void onApplicationEvent(BootstrapContextClosedEvent event) { + ConfigurableBootstrapContext bootstrapContext = (ConfigurableBootstrapContext) event.getBootstrapContext(); + ConfigurableApplicationContext applicationContext = event.getApplicationContext(); + delegate.onBootstrapContextClose(bootstrapContext, applicationContext); + } + +} \ No newline at end of file diff --git a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/LoggerAwareEnvironmentPostProcessor.java b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/LoggerAwareEnvironmentPostProcessor.java index 078f7cf..b22e94a 100644 --- a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/LoggerAwareEnvironmentPostProcessor.java +++ b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/LoggerAwareEnvironmentPostProcessor.java @@ -2,10 +2,11 @@ package com.schbrain.framework.support.spring; import lombok.Getter; import org.apache.commons.logging.Log; -import org.springframework.boot.BootstrapContextClosedEvent; import org.springframework.boot.ConfigurableBootstrapContext; +import org.springframework.boot.DefaultBootstrapContext; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.logging.DeferredLogFactory; +import org.springframework.context.ConfigurableApplicationContext; /** * @author liaozan @@ -15,19 +16,22 @@ import org.springframework.boot.logging.DeferredLogFactory; public abstract class LoggerAwareEnvironmentPostProcessor implements EnvironmentPostProcessor { protected final Log log; - protected final DeferredLogFactory deferredLogFactory; - protected final ConfigurableBootstrapContext bootstrapContext; public LoggerAwareEnvironmentPostProcessor(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext) { this.log = logFactory.getLog(getClass()); this.bootstrapContext = bootstrapContext; this.deferredLogFactory = logFactory; - this.bootstrapContext.addCloseListener(this::onBootstrapContextClose); + this.bootstrapContext.addCloseListener(new BootstrapContextClosedEventListener(this)); } - protected void onBootstrapContextClose(BootstrapContextClosedEvent event) { + /** + * This event is triggered after ApplicationContextInitializedEvent + * + * @see DefaultBootstrapContext#close(ConfigurableApplicationContext) + */ + protected void onBootstrapContextClose(ConfigurableBootstrapContext bootstrapContext, ConfigurableApplicationContext applicationContext) { } -- GitLab