Commit 0cb9e443 authored by liaozan's avatar liaozan 🏀

Add more member to ConfigLoadedEvent

parent 14e8d0e8
......@@ -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
......
......@@ -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<ApplicationListener<?>> listeners) {
......
......@@ -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() {
......
......@@ -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<DubboConfigInitEvent>, PriorityOrdered {
......@@ -42,7 +45,7 @@ class DubboConfigInitEventListener implements ApplicationListener<DubboConfigIni
private ConfigCenterConfig buildConfigCenterConfig() {
Map<String, String> externalConfiguration = filterDubboProperties(applicationContext.getEnvironment());
ConfigCenterConfig configCenterConfig = new ConfigCenterConfig();
configCenterConfig.setExternalConfig(externalConfiguration);
configCenterConfig.setAppExternalConfig(externalConfiguration);
return configCenterConfig;
}
......
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<DubboProperties> {
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) {
......
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;
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) {
}
......
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