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

Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 370e39e19c fix(sdk): avoid sharing client proxies across contexts 
(#7070)
370e39e19c is described below

commit 370e39e19cd8af638741f4da28a330d42ccf4e7b
Author: Liming Deng <[email protected]>
AuthorDate: Wed Sep 30 09:56:40 2026 +0800

    fix(sdk): avoid sharing client proxies across contexts (#7070)
---
 .../sdk/spring/proxy/ShenyuClientProxyFactory.java | 21 ++---------
 .../shenyu/sdk/spring/proxy/AbstractProxyTest.java |  4 --
 .../spring/proxy/ShenyuClientProxyFactoryTest.java | 44 ++++++++++++++++------
 3 files changed, 35 insertions(+), 34 deletions(-)

diff --git 
a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientProxyFactory.java
 
b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientProxyFactory.java
index c0baf1587a..e9d8d3fcee 100644
--- 
a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientProxyFactory.java
+++ 
b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientProxyFactory.java
@@ -21,8 +21,6 @@ import org.apache.shenyu.sdk.spring.ShenyuClientFactoryBean;
 import org.springframework.context.ApplicationContext;
 
 import java.lang.reflect.Proxy;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
 
 /**
  * ShenyuClientProxyFactory.
@@ -30,11 +28,6 @@ import java.util.concurrent.ConcurrentMap;
  */
 public final class ShenyuClientProxyFactory {
 
-    /**
-     * PROXY_CACHE.
-     */
-    private static final ConcurrentMap<Class<?>, Object> PROXY_CACHE = new 
ConcurrentHashMap<>();
-
     /**
      * createProxy.
      *
@@ -48,17 +41,9 @@ public final class ShenyuClientProxyFactory {
             throw new UnsupportedOperationException("@ShenyuClient please use 
it on the interface. " + apiClass.getName());
         }
 
-        if (PROXY_CACHE.containsKey(apiClass)) {
-            return PROXY_CACHE.get(apiClass);
-        }
-
-        synchronized (apiClass) {
-            Object proxy = Proxy.newProxyInstance(apiClass.getClassLoader(),
-                    new Class<?>[]{apiClass},
-                    new ShenyuClientInvocationHandler(apiClass, 
applicationContext, shenyuClientFactoryBean));
-            PROXY_CACHE.put(apiClass, proxy);
-        }
-        return PROXY_CACHE.get(apiClass);
+        return Proxy.newProxyInstance(apiClass.getClassLoader(),
+                new Class<?>[]{apiClass},
+                new ShenyuClientInvocationHandler(apiClass, 
applicationContext, shenyuClientFactoryBean));
     }
 
 }
diff --git 
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/AbstractProxyTest.java
 
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/AbstractProxyTest.java
index 751a36c1b8..06ec714a1e 100644
--- 
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/AbstractProxyTest.java
+++ 
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/AbstractProxyTest.java
@@ -46,16 +46,12 @@ public abstract class AbstractProxyTest {
 
     protected static final Field REQUEST_TEMPLATE;
 
-    protected static final Field PROXY_CACHE;
-
     static {
         try {
             METHOD_HANDLER_MAP = 
ShenyuClientInvocationHandler.class.getDeclaredField("methodHandlerMap");
             METHOD_HANDLER_MAP.setAccessible(true);
             REQUEST_TEMPLATE = 
ShenyuClientMethodHandler.class.getDeclaredField("requestTemplate");
             REQUEST_TEMPLATE.setAccessible(true);
-            PROXY_CACHE = 
ShenyuClientProxyFactory.class.getDeclaredField("PROXY_CACHE");
-            PROXY_CACHE.setAccessible(true);
         } catch (NoSuchFieldException e) {
             throw new RuntimeException(e);
         }
diff --git 
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientProxyFactoryTest.java
 
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientProxyFactoryTest.java
index 46737aefda..0457890f12 100644
--- 
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientProxyFactoryTest.java
+++ 
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientProxyFactoryTest.java
@@ -17,26 +17,46 @@
 
 package org.apache.shenyu.sdk.spring.proxy;
 
-import java.io.IOException;
-import java.util.concurrent.ConcurrentMap;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Collections;
+import org.apache.shenyu.sdk.core.client.ShenyuSdkClient;
+import org.apache.shenyu.sdk.spring.ShenyuClientFactoryBean;
+import org.apache.shenyu.sdk.spring.factory.Contract;
 import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.condition.DisabledForJreRange;
-import org.junit.jupiter.api.condition.JRE;
+import org.springframework.context.ApplicationContext;
 
 /**
  * {@link ShenyuClientProxyFactory} test.
  */
-public class ShenyuClientProxyFactoryTest extends AbstractProxyTest {
+public class ShenyuClientProxyFactoryTest {
 
     @Test
-    @SuppressWarnings("unchecked")
-    @DisabledForJreRange(min = JRE.JAVA_16)
-    public void factoryTest() throws IllegalAccessException, IOException {
-        init();
+    public void testCreateContextSpecificProxy() {
+        ApplicationContext firstContext = mock(ApplicationContext.class);
+        ApplicationContext secondContext = mock(ApplicationContext.class);
+        prepareContext(firstContext);
+        prepareContext(secondContext);
+
+        Object firstProxy = 
ShenyuClientProxyFactory.createProxy(TestClient.class, firstContext, new 
ShenyuClientFactoryBean());
+        Object secondProxy = 
ShenyuClientProxyFactory.createProxy(TestClient.class, secondContext, new 
ShenyuClientFactoryBean());
+
+        assertNotSame(firstProxy, secondProxy);
+    }
+
+    private void prepareContext(final ApplicationContext context) {
+        Contract contract = mock(Contract.class);
+        when(contract.parseAndValidateRequestTemplate(eq(TestClient.class), 
any(ShenyuClientFactoryBean.class))).thenReturn(Collections.emptyList());
+        when(context.getBean(Contract.class)).thenReturn(contract);
+        
when(context.getBean(ShenyuSdkClient.class)).thenReturn(mock(ShenyuSdkClient.class));
+        
when(context.getBeansOfType(org.apache.shenyu.sdk.spring.factory.AnnotatedParameterProcessor.class)).thenReturn(Collections.emptyMap());
+    }
 
-        final ConcurrentMap<Class<?>, Object> proxyMap = 
(ConcurrentMap<Class<?>, Object>) PROXY_CACHE.get(null);
-        assertNotNull(proxyMap);
+    private interface TestClient {
     }
 
 }

Reply via email to