Skip to content

Commit

Permalink
Optimize code in DubboSpringInitializer (apache#12834)
Browse files Browse the repository at this point in the history
  • Loading branch information
kang-hl committed Aug 9, 2023
1 parent 1dd248a commit 45405ff
Showing 1 changed file with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.dubbo.rpc.model.ModuleModel;
import org.apache.dubbo.rpc.model.ScopeModel;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
Expand All @@ -48,14 +49,14 @@ private DubboSpringInitializer() {

public static void initialize(BeanDefinitionRegistry registry) {

// prepare context and do customize
DubboSpringInitContext context = new DubboSpringInitContext();

// Spring ApplicationContext may not ready at this moment (e.g. load from xml), so use registry as key
if (contextMap.putIfAbsent(registry, new DubboSpringInitContext()) != null) {
if (contextMap.putIfAbsent(registry, context) != null) {
return;
}

// prepare context and do customize
DubboSpringInitContext context = contextMap.get(registry);

// find beanFactory
ConfigurableListableBeanFactory beanFactory = findBeanFactory(registry);

Expand All @@ -68,11 +69,12 @@ public static boolean remove(BeanDefinitionRegistry registry) {
}

public static boolean remove(ApplicationContext springContext) {
AutowireCapableBeanFactory autowireCapableBeanFactory = springContext.getAutowireCapableBeanFactory();
for (Map.Entry<BeanDefinitionRegistry, DubboSpringInitContext> entry : contextMap.entrySet()) {
DubboSpringInitContext initContext = entry.getValue();
if (initContext.getApplicationContext() == springContext ||
initContext.getBeanFactory() == springContext.getAutowireCapableBeanFactory() ||
initContext.getRegistry() == springContext.getAutowireCapableBeanFactory()
initContext.getBeanFactory() == autowireCapableBeanFactory ||
initContext.getRegistry() == autowireCapableBeanFactory
) {
DubboSpringInitContext context = contextMap.remove(entry.getKey());
logger.info("Unbind " + safeGetModelDesc(context.getModuleModel()) + " from spring container: " +
Expand All @@ -88,8 +90,7 @@ static Map<BeanDefinitionRegistry, DubboSpringInitContext> getContextMap() {
}

static DubboSpringInitContext findBySpringContext(ApplicationContext applicationContext) {
for (Map.Entry<BeanDefinitionRegistry, DubboSpringInitContext> entry : contextMap.entrySet()) {
DubboSpringInitContext initContext = entry.getValue();
for (DubboSpringInitContext initContext : contextMap.values()) {
if (initContext.getApplicationContext() == applicationContext) {
return initContext;
}
Expand All @@ -112,25 +113,26 @@ private static void initContext(DubboSpringInitContext context, BeanDefinitionRe
if (findContextForApplication(ApplicationModel.defaultModel()) == null) {
// first spring context use default application instance
applicationModel = ApplicationModel.defaultModel();
logger.info("Use default application: " + safeGetModelDesc(applicationModel));
logger.info("Use default application: " + applicationModel.getDesc());
} else {
// create a new application instance for later spring context
applicationModel = FrameworkModel.defaultModel().newApplication();
logger.info("Create new application: " + safeGetModelDesc(applicationModel));
logger.info("Create new application: " + applicationModel.getDesc());
}

// init ModuleModel
moduleModel = applicationModel.getDefaultModule();
context.setModuleModel(moduleModel);
logger.info("Use default module model of target application: " + safeGetModelDesc(moduleModel));
logger.info("Use default module model of target application: " + moduleModel.getDesc());
} else {
logger.info("Use module model from customizer: " + safeGetModelDesc(moduleModel));
logger.info("Use module model from customizer: " + moduleModel.getDesc());
}
logger.info("Bind " + safeGetModelDesc(moduleModel) + " to spring container: " + ObjectUtils.identityToString(registry));
logger.info("Bind " + moduleModel.getDesc() + " to spring container: " + ObjectUtils.identityToString(registry));

// set module attributes
if (context.getModuleAttributes().size() > 0) {
context.getModuleModel().getAttributes().putAll(context.getModuleAttributes());
Map<String, Object> moduleAttributes = context.getModuleAttributes();
if (moduleAttributes.size() > 0) {
moduleModel.getAttributes().putAll(moduleAttributes);
}

// bind dubbo initialization context to spring context
Expand Down Expand Up @@ -198,7 +200,6 @@ private static void customize(DubboSpringInitContext context) {
customizer.customize(context);
}
customizerHolder.clearCustomizers();

}

}

0 comments on commit 45405ff

Please sign in to comment.