You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2023/07/17 06:20:56 UTC

[dubbo] branch 3.2 updated: Fix compact with spring 3.2.x (#12741)

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 cec66fa263 Fix compact with spring 3.2.x (#12741)
cec66fa263 is described below

commit cec66fa2639ffaa68a7fd8108113deaa17ddc419
Author: Albumen Kevin <jh...@gmail.com>
AuthorDate: Mon Jul 17 14:20:51 2023 +0800

    Fix compact with spring 3.2.x (#12741)
---
 .../apache/dubbo/config/spring/ReferenceBean.java  |  4 +-
 .../context/DubboDeployApplicationListener.java    |  4 +-
 .../apache/dubbo/config/spring/util/LockUtils.java | 50 ++++++++++++++++++++++
 3 files changed, 54 insertions(+), 4 deletions(-)

diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java
index e483455d03..46e91f61f3 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java
@@ -31,6 +31,7 @@ import org.apache.dubbo.config.spring.reference.ReferenceBeanSupport;
 import org.apache.dubbo.config.spring.schema.DubboBeanDefinitionParser;
 import org.apache.dubbo.config.spring.util.LazyTargetInvocationHandler;
 import org.apache.dubbo.config.spring.util.LazyTargetSource;
+import org.apache.dubbo.config.spring.util.LockUtils;
 import org.apache.dubbo.config.support.Parameter;
 import org.apache.dubbo.rpc.proxy.AbstractProxyFactory;
 
@@ -43,7 +44,6 @@ import org.springframework.beans.factory.InitializingBean;
 import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
 import org.springframework.beans.factory.config.BeanDefinition;
 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
-import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
 
@@ -396,7 +396,7 @@ public class ReferenceBean<T> implements FactoryBean<T>,
         if (referenceConfig.configInitialized()) {
             return referenceConfig.get();
         }
-        synchronized (((DefaultSingletonBeanRegistry)getBeanFactory()).getSingletonMutex()) {
+        synchronized (LockUtils.getSingletonMutex(applicationContext)) {
             return referenceConfig.get();
         }
     }
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboDeployApplicationListener.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboDeployApplicationListener.java
index 85bb0b6708..2f61fa22d6 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboDeployApplicationListener.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboDeployApplicationListener.java
@@ -25,12 +25,12 @@ import org.apache.dubbo.common.utils.Assert;
 import org.apache.dubbo.config.spring.context.event.DubboApplicationStateEvent;
 import org.apache.dubbo.config.spring.context.event.DubboModuleStateEvent;
 import org.apache.dubbo.config.spring.util.DubboBeanUtils;
+import org.apache.dubbo.config.spring.util.LockUtils;
 import org.apache.dubbo.rpc.model.ApplicationModel;
 import org.apache.dubbo.rpc.model.ModelConstants;
 import org.apache.dubbo.rpc.model.ModuleModel;
 
 import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
 import org.springframework.context.ApplicationListener;
@@ -147,7 +147,7 @@ public class DubboDeployApplicationListener implements ApplicationListener<Appli
     private void onContextRefreshedEvent(ContextRefreshedEvent event) {
         ModuleDeployer deployer = moduleModel.getDeployer();
         Assert.notNull(deployer, "Module deployer is null");
-        Object singletonMutex = ((DefaultSingletonBeanRegistry) applicationContext.getAutowireCapableBeanFactory()).getSingletonMutex();
+        Object singletonMutex = LockUtils.getSingletonMutex(applicationContext);
         // start module
         Future future = null;
         synchronized (singletonMutex) {
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/LockUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/LockUtils.java
new file mode 100644
index 0000000000..7b469c5c55
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/LockUtils.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.config.spring.util;
+
+import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
+import org.springframework.context.ApplicationContext;
+
+import java.lang.reflect.Method;
+
+public class LockUtils {
+
+    private static final String DUBBO_SINGLETON_MUTEX_KEY = "DUBBO_SINGLETON_MUTEX";
+
+    /**
+     * Get the mutex to lock the singleton in the specified {@link ApplicationContext}
+     */
+    public static synchronized Object getSingletonMutex(ApplicationContext applicationContext) {
+        DefaultSingletonBeanRegistry autowireCapableBeanFactory = (DefaultSingletonBeanRegistry) applicationContext.getAutowireCapableBeanFactory();
+        try {
+            return autowireCapableBeanFactory.getSingletonMutex();
+        } catch (Throwable t1) {
+            try {
+                // try protected
+                Method method = DefaultSingletonBeanRegistry.class.getDeclaredMethod("getSingletonMutex");
+                method.setAccessible(true);
+                return method.invoke(autowireCapableBeanFactory);
+            } catch (Throwable t2) {
+                // Before Spring 4.2, there is no getSingletonMutex method
+                if (!autowireCapableBeanFactory.containsSingleton(DUBBO_SINGLETON_MUTEX_KEY)) {
+                    autowireCapableBeanFactory.registerSingleton(DUBBO_SINGLETON_MUTEX_KEY, new Object());
+                }
+                return autowireCapableBeanFactory.getSingleton(DUBBO_SINGLETON_MUTEX_KEY);
+            }
+        }
+    }
+}