This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.2 by this push:
     new 45405ff340 Optimize code in DubboSpringInitializer (#12834)
45405ff340 is described below

commit 45405ff3408bae0df0976e7c1350939c87f52ac7
Author: kanghailin <[email protected]>
AuthorDate: Wed Aug 9 19:15:27 2023 +0800

    Optimize code in DubboSpringInitializer (#12834)
---
 .../spring/context/DubboSpringInitializer.java     | 33 +++++++++++-----------
 1 file changed, 17 insertions(+), 16 deletions(-)

diff --git 
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboSpringInitializer.java
 
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboSpringInitializer.java
index 774cfc1e8e..2c188af0a7 100644
--- 
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboSpringInitializer.java
+++ 
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboSpringInitializer.java
@@ -24,6 +24,7 @@ import org.apache.dubbo.rpc.model.FrameworkModel;
 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;
@@ -48,14 +49,14 @@ public class 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);
 
@@ -68,11 +69,12 @@ public class DubboSpringInitializer {
     }
 
     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: " +
@@ -88,8 +90,7 @@ public class DubboSpringInitializer {
     }
 
     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;
             }
@@ -112,25 +113,26 @@ public class DubboSpringInitializer {
             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
@@ -198,7 +200,6 @@ public class DubboSpringInitializer {
             customizer.customize(context);
         }
         customizerHolder.clearCustomizers();
-
     }
 
 }

Reply via email to