Commit 0cb9e443 authored by liaozan's avatar liaozan 🏀

Add more member to ConfigLoadedEvent

parent 14e8d0e8
...@@ -38,7 +38,7 @@ public class ApolloConfigurationInitializerEnvironmentPostProcessor extends Logg ...@@ -38,7 +38,7 @@ public class ApolloConfigurationInitializerEnvironmentPostProcessor extends Logg
public ApolloConfigurationInitializerEnvironmentPostProcessor(DeferredLogFactory deferredLogFactory, ConfigurableBootstrapContext bootstrapContext) { public ApolloConfigurationInitializerEnvironmentPostProcessor(DeferredLogFactory deferredLogFactory, ConfigurableBootstrapContext bootstrapContext) {
super(deferredLogFactory, bootstrapContext); super(deferredLogFactory, bootstrapContext);
this.configurablePropertiesLoader = new ConfigurablePropertiesLoader(deferredLogFactory); this.configurablePropertiesLoader = new ConfigurablePropertiesLoader(deferredLogFactory, bootstrapContext);
} }
@Override @Override
......
...@@ -10,6 +10,7 @@ import com.schbrain.framework.autoconfigure.apollo.properties.ApolloProperties; ...@@ -10,6 +10,7 @@ import com.schbrain.framework.autoconfigure.apollo.properties.ApolloProperties;
import com.schbrain.framework.autoconfigure.apollo.util.ConfigUtils; import com.schbrain.framework.autoconfigure.apollo.util.ConfigUtils;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
...@@ -31,12 +32,13 @@ import static org.springframework.core.io.support.SpringFactoriesLoader.loadFact ...@@ -31,12 +32,13 @@ import static org.springframework.core.io.support.SpringFactoriesLoader.loadFact
class ConfigurablePropertiesLoader { class ConfigurablePropertiesLoader {
private final Log log; private final Log log;
private final DeferredLogFactory deferredLogFactory; private final DeferredLogFactory deferredLogFactory;
private final ConfigurableBootstrapContext bootstrapContext;
ConfigurablePropertiesLoader(DeferredLogFactory deferredLogFactory) { ConfigurablePropertiesLoader(DeferredLogFactory deferredLogFactory, ConfigurableBootstrapContext bootstrapContext) {
this.deferredLogFactory = deferredLogFactory; this.deferredLogFactory = deferredLogFactory;
this.log = deferredLogFactory.getLog(ConfigurablePropertiesLoader.class); this.log = deferredLogFactory.getLog(ConfigurablePropertiesLoader.class);
this.bootstrapContext = bootstrapContext;
} }
/** /**
...@@ -83,7 +85,7 @@ class ConfigurablePropertiesLoader { ...@@ -83,7 +85,7 @@ class ConfigurablePropertiesLoader {
private ConfigLoadedEvent createEvent(ConfigurableEnvironment environment, SpringApplication application, private ConfigLoadedEvent createEvent(ConfigurableEnvironment environment, SpringApplication application,
OrderedMapPropertySource propertySource, ConfigurableProperties properties) { OrderedMapPropertySource propertySource, ConfigurableProperties properties) {
ConfigurableProperties boundProperties = properties.bind(environment); 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<ApplicationListener<?>> listeners) { private ApplicationEventMulticaster createEventMulticaster(Set<ApplicationListener<?>> listeners) {
......
...@@ -3,6 +3,7 @@ package com.schbrain.framework.autoconfigure.apollo.event; ...@@ -3,6 +3,7 @@ package com.schbrain.framework.autoconfigure.apollo.event;
import com.schbrain.common.util.properties.OrderedMapPropertySource; import com.schbrain.common.util.properties.OrderedMapPropertySource;
import com.schbrain.common.util.support.ConfigurableProperties; import com.schbrain.common.util.support.ConfigurableProperties;
import lombok.Getter; import lombok.Getter;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
...@@ -18,23 +19,23 @@ public class ConfigLoadedEvent extends ApplicationEvent { ...@@ -18,23 +19,23 @@ public class ConfigLoadedEvent extends ApplicationEvent {
private static final long serialVersionUID = 2567291189881702459L; private static final long serialVersionUID = 2567291189881702459L;
private final ConfigurableEnvironment environment; private final ConfigurableEnvironment environment;
private final DeferredLogFactory deferredLogFactory; private final DeferredLogFactory deferredLogFactory;
private final OrderedMapPropertySource propertySource; private final OrderedMapPropertySource propertySource;
private final SpringApplication springApplication; private final SpringApplication springApplication;
private final ConfigurableBootstrapContext bootstrapContext;
public ConfigLoadedEvent(ConfigurableEnvironment environment, public ConfigLoadedEvent(ConfigurableEnvironment environment,
DeferredLogFactory deferredLogFactory, DeferredLogFactory deferredLogFactory,
OrderedMapPropertySource propertySource, OrderedMapPropertySource propertySource,
ConfigurableProperties properties, ConfigurableProperties properties,
SpringApplication springApplication) { SpringApplication springApplication,
ConfigurableBootstrapContext bootstrapContext) {
super(properties); super(properties);
this.environment = environment; this.environment = environment;
this.propertySource = propertySource; this.propertySource = propertySource;
this.deferredLogFactory = deferredLogFactory; this.deferredLogFactory = deferredLogFactory;
this.springApplication = springApplication; this.springApplication = springApplication;
this.bootstrapContext = bootstrapContext;
} }
public ConfigurableProperties getConfigurableProperties() { public ConfigurableProperties getConfigurableProperties() {
......
...@@ -2,9 +2,11 @@ package com.schbrain.framework.autoconfigure.dubbo.listener; ...@@ -2,9 +2,11 @@ package com.schbrain.framework.autoconfigure.dubbo.listener;
import org.apache.dubbo.config.ConfigCenterConfig; import org.apache.dubbo.config.ConfigCenterConfig;
import org.apache.dubbo.config.context.ConfigManager; 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.context.event.DubboConfigInitEvent;
import org.apache.dubbo.config.spring.util.DubboBeanUtils; import org.apache.dubbo.config.spring.util.DubboBeanUtils;
import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.ApplicationModel;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.PriorityOrdered; import org.springframework.core.PriorityOrdered;
...@@ -15,6 +17,7 @@ import static org.apache.dubbo.config.spring.util.EnvironmentUtils.filterDubboPr ...@@ -15,6 +17,7 @@ import static org.apache.dubbo.config.spring.util.EnvironmentUtils.filterDubboPr
/** /**
* @author liaozan * @author liaozan
* @see ReferenceAnnotationBeanPostProcessor#postProcessBeanFactory(ConfigurableListableBeanFactory)
* @since 2023-05-08 * @since 2023-05-08
*/ */
class DubboConfigInitEventListener implements ApplicationListener<DubboConfigInitEvent>, PriorityOrdered { class DubboConfigInitEventListener implements ApplicationListener<DubboConfigInitEvent>, PriorityOrdered {
...@@ -42,8 +45,8 @@ class DubboConfigInitEventListener implements ApplicationListener<DubboConfigIni ...@@ -42,8 +45,8 @@ class DubboConfigInitEventListener implements ApplicationListener<DubboConfigIni
private ConfigCenterConfig buildConfigCenterConfig() { private ConfigCenterConfig buildConfigCenterConfig() {
Map<String, String> externalConfiguration = filterDubboProperties(applicationContext.getEnvironment()); Map<String, String> externalConfiguration = filterDubboProperties(applicationContext.getEnvironment());
ConfigCenterConfig configCenterConfig = new ConfigCenterConfig(); ConfigCenterConfig configCenterConfig = new ConfigCenterConfig();
configCenterConfig.setExternalConfig(externalConfiguration); configCenterConfig.setAppExternalConfig(externalConfiguration);
return configCenterConfig; return configCenterConfig;
} }
} }
\ No newline at end of file
package com.schbrain.framework.autoconfigure.dubbo.listener; package com.schbrain.framework.autoconfigure.dubbo.listener;
import com.schbrain.common.util.ApplicationName;
import com.schbrain.common.util.properties.OrderedMapPropertySource; import com.schbrain.common.util.properties.OrderedMapPropertySource;
import com.schbrain.framework.autoconfigure.apollo.event.ConfigLoadedEvent; import com.schbrain.framework.autoconfigure.apollo.event.ConfigLoadedEvent;
import com.schbrain.framework.autoconfigure.apollo.event.listener.GenericConfigLoadedEventListener; import com.schbrain.framework.autoconfigure.apollo.event.listener.GenericConfigLoadedEventListener;
...@@ -8,7 +7,6 @@ import com.schbrain.framework.autoconfigure.dubbo.properties.DubboProperties; ...@@ -8,7 +7,6 @@ import com.schbrain.framework.autoconfigure.dubbo.properties.DubboProperties;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import static org.apache.dubbo.config.ConfigKeys.DUBBO_SCAN_BASE_PACKAGES; 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; ...@@ -18,8 +16,6 @@ import static org.apache.dubbo.config.ConfigKeys.DUBBO_SCAN_BASE_PACKAGES;
*/ */
public class DubboConfigLoadedEventListener extends GenericConfigLoadedEventListener<DubboProperties> { public class DubboConfigLoadedEventListener extends GenericConfigLoadedEventListener<DubboProperties> {
public static final String DUBBO_APPLICATION_NAME = "dubbo.application.name";
@Override @Override
public int getOrder() { public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE; return Ordered.HIGHEST_PRECEDENCE;
...@@ -32,16 +28,13 @@ public class DubboConfigLoadedEventListener extends GenericConfigLoadedEventList ...@@ -32,16 +28,13 @@ public class DubboConfigLoadedEventListener extends GenericConfigLoadedEventList
@Override @Override
protected void onConfigLoaded(ConfigLoadedEvent event, DubboProperties properties) { 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)) { if (!propertySource.containsProperty(DUBBO_SCAN_BASE_PACKAGES)) {
propertySource.addProperty(DUBBO_SCAN_BASE_PACKAGES, getBasePackage(application)); 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) { private String getBasePackage(SpringApplication application) {
......
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<BootstrapContextClosedEvent> {
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
...@@ -2,10 +2,11 @@ package com.schbrain.framework.support.spring; ...@@ -2,10 +2,11 @@ package com.schbrain.framework.support.spring;
import lombok.Getter; import lombok.Getter;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.springframework.boot.BootstrapContextClosedEvent;
import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.DefaultBootstrapContext;
import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.context.ConfigurableApplicationContext;
/** /**
* @author liaozan * @author liaozan
...@@ -15,19 +16,22 @@ import org.springframework.boot.logging.DeferredLogFactory; ...@@ -15,19 +16,22 @@ import org.springframework.boot.logging.DeferredLogFactory;
public abstract class LoggerAwareEnvironmentPostProcessor implements EnvironmentPostProcessor { public abstract class LoggerAwareEnvironmentPostProcessor implements EnvironmentPostProcessor {
protected final Log log; protected final Log log;
protected final DeferredLogFactory deferredLogFactory; protected final DeferredLogFactory deferredLogFactory;
protected final ConfigurableBootstrapContext bootstrapContext; protected final ConfigurableBootstrapContext bootstrapContext;
public LoggerAwareEnvironmentPostProcessor(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext) { public LoggerAwareEnvironmentPostProcessor(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext) {
this.log = logFactory.getLog(getClass()); this.log = logFactory.getLog(getClass());
this.bootstrapContext = bootstrapContext; this.bootstrapContext = bootstrapContext;
this.deferredLogFactory = logFactory; 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) {
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment