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 764ea03032a01171ac39b294a7ee68e63e28692d..1b3e488e9786432a938e39307aa5874bbd0f621f 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 8a3eaab893ba03892e87d7f63de4416cff40d37e..ed4830924af09cab4408fc46fe2c56881b1e0d00 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 62a2314c0ffa4c50d4553683b5dc34b75ddcfd89..673d2d82492f221cbfad43e27ded1e7dbc3dab22 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 0a2b1052acfb7ce653d2ca35720fd33cf3adad0c..d396ae051b3d645a6c15f7e814cd61911bfb1dc2 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 321d0e4db9301b3ea8bdf915b8a9b0b8af45dabd..5aefdc319c76b203b9e6a9b316ec6bde99cdc37a 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 0000000000000000000000000000000000000000..e71224c98452185c49e1b624fc416eeb62b62f20 --- /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 078f7cf8da0df5187080f72ae4fb965428a93f06..b22e94ae0a7c9d01823c43647c384005a0ccd259 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) { }