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

dengliming pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shenyu-client-java.git


The following commit(s) were added to refs/heads/main by this push:
     new 4c13bd1  Fix/fix local springmvc client cant register success (#12)
4c13bd1 is described below

commit 4c13bd1d9142923ab149df21b1085b8bb755aa20
Author: hengyuss <[email protected]>
AuthorDate: Fri Sep 11 18:44:25 2026 +0800

    Fix/fix local springmvc client cant register success (#12)
    
    * fix: the variable 'isDiscoveryLocalMode' always false in 
AbstractontextRefreshedEventListener cause local springmvc client can't 
register success
    
    * fix: fix test and checkStyle
    
    ---------
    
    Co-authored-by: Liming Deng <[email protected]>
---
 .../client/core/client/AbstractContextRefreshedEventListener.java  | 6 +++---
 .../client/springmvc/init/SpringMvcClientEventListenerTest.java    | 6 ++++--
 .../shenyu/client/tars/TarsServiceBeanPostProcessorTest.java       | 7 +++++++
 3 files changed, 14 insertions(+), 5 deletions(-)

diff --git 
a/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/client/AbstractContextRefreshedEventListener.java
 
b/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/client/AbstractContextRefreshedEventListener.java
index 0d9ac05..063ac52 100644
--- 
a/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/client/AbstractContextRefreshedEventListener.java
+++ 
b/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/client/AbstractContextRefreshedEventListener.java
@@ -103,8 +103,6 @@ public abstract class 
AbstractContextRefreshedEventListener<T, A extends Annotat
 
     private ApplicationContext context;
 
-    private final Boolean isDiscoveryLocalMode;
-    
     /**
      * multiple namespace support.
      */
@@ -139,7 +137,6 @@ public abstract class 
AbstractContextRefreshedEventListener<T, A extends Annotat
         this.ipAndPort = props.getProperty(ShenyuClientConstants.IP_PORT);
         this.host = props.getProperty(ShenyuClientConstants.HOST);
         this.port = props.getProperty(ShenyuClientConstants.PORT);
-        this.isDiscoveryLocalMode = 
Boolean.valueOf(props.getProperty(ShenyuClientConstants.DISCOVERY_LOCAL_MODE_KEY));
         publisher.start(shenyuClientRegisterRepository);
     }
 
@@ -153,6 +150,9 @@ public abstract class 
AbstractContextRefreshedEventListener<T, A extends Annotat
         if (!registered.compareAndSet(false, true)) {
             return;
         }
+        String discoveryMode = context.getEnvironment()
+                .getProperty("shenyu.discovery.type", 
ShenyuClientConstants.DISCOVERY_LOCAL_MODE);
+        boolean isDiscoveryLocalMode = 
ShenyuClientConstants.DISCOVERY_LOCAL_MODE.equals(discoveryMode);
         if (isDiscoveryLocalMode) {
             List<String> namespaceIds = this.getNamespace();
             namespaceIds.forEach(namespaceId -> 
publisher.publishEvent(buildURIRegisterDTO(context, beans, namespaceId)));
diff --git 
a/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListenerTest.java
 
b/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListenerTest.java
index fa6aa47..7a2e1a4 100644
--- 
a/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListenerTest.java
+++ 
b/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListenerTest.java
@@ -93,6 +93,8 @@ public class SpringMvcClientEventListenerTest {
         results.put("springMvcClientTestBean3", springMvcClientTestBean3);
         results.put("springMvcClientTestBean4", springMvcClientTestBean4);
         
when(applicationContext.getBeansWithAnnotation(any())).thenReturn(results);
+        when(applicationContext.getEnvironment()).thenReturn(env);
+        when(env.getProperty("shenyu.discovery.type", 
ShenyuClientConstants.DISCOVERY_LOCAL_MODE)).thenReturn("local");
         contextRefreshedEvent = new ContextRefreshedEvent(applicationContext);
         ShenyuClientConfig shenyuClientConfig = mock(ShenyuClientConfig.class);
         Assert.assertThrows(ShenyuClientIllegalArgumentException.class, () -> 
new SpringMvcClientEventListener(shenyuClientConfig, 
mock(ShenyuClientRegisterRepository.class), env));
@@ -182,12 +184,12 @@ public class SpringMvcClientEventListenerTest {
         SpringMvcClientEventListener springMvcClientEventListener = 
buildSpringMvcClientEventListener(false, false);
 
         Assertions.assertEquals("/order", 
springMvcClientEventListener.buildApiSuperPath(
-            SpringMvcClientTestBean.class, 
AnnotatedElementUtils.findMergedAnnotation(SpringMvcClientTestBean.class, 
ShenyuSpringMvcClient.class)), "super-path");
+                SpringMvcClientTestBean.class, 
AnnotatedElementUtils.findMergedAnnotation(SpringMvcClientTestBean.class, 
ShenyuSpringMvcClient.class)), "super-path");
 
         
when(env.getProperty("spring.mvc.servlet.path")).thenReturn("/servlet-path");
         
when(env.getProperty("server.servlet.context-path")).thenReturn("/servlet-context-path");
         Assertions.assertEquals("/servlet-context-path/servlet-path/order", 
springMvcClientEventListener.buildApiSuperPath(
-            SpringMvcClientTestBean.class, 
AnnotatedElementUtils.findMergedAnnotation(SpringMvcClientTestBean.class, 
ShenyuSpringMvcClient.class)), "super-path");
+                SpringMvcClientTestBean.class, 
AnnotatedElementUtils.findMergedAnnotation(SpringMvcClientTestBean.class, 
ShenyuSpringMvcClient.class)), "super-path");
         registerUtilsMockedStatic.close();
     }
 
diff --git 
a/shenyu-client-tars/src/test/java/org/apache/shenyu/client/tars/TarsServiceBeanPostProcessorTest.java
 
b/shenyu-client-tars/src/test/java/org/apache/shenyu/client/tars/TarsServiceBeanPostProcessorTest.java
index a81395e..e50e6a1 100644
--- 
a/shenyu-client-tars/src/test/java/org/apache/shenyu/client/tars/TarsServiceBeanPostProcessorTest.java
+++ 
b/shenyu-client-tars/src/test/java/org/apache/shenyu/client/tars/TarsServiceBeanPostProcessorTest.java
@@ -17,6 +17,7 @@
 
 package org.apache.shenyu.client.tars;
 
+import org.apache.shenyu.client.core.constant.ShenyuClientConstants;
 import org.apache.shenyu.client.core.enums.RpcTypeEnum;
 import 
org.apache.shenyu.client.core.exception.ShenyuClientIllegalArgumentException;
 import org.apache.shenyu.client.core.register.ShenyuClientRegisterRepository;
@@ -38,6 +39,7 @@ import org.mockito.MockedStatic;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.event.ContextRefreshedEvent;
+import org.springframework.core.env.Environment;
 
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -68,6 +70,9 @@ public final class TarsServiceBeanPostProcessorTest {
     @Mock
     private ApplicationContext applicationContext;
 
+    @Mock
+    private Environment env;
+
     private ContextRefreshedEvent contextRefreshedEvent;
 
     @BeforeEach
@@ -77,6 +82,8 @@ public final class TarsServiceBeanPostProcessorTest {
         results.put("tarsDemoService2", tarsDemoService2);
         results.put("tarsDemoService3", tarsDemoService3);
         
when(applicationContext.getBeansWithAnnotation(any())).thenReturn(results);
+        when(applicationContext.getEnvironment()).thenReturn(env);
+        when(env.getProperty("shenyu.discovery.type", 
ShenyuClientConstants.DISCOVERY_LOCAL_MODE)).thenReturn("local");
         contextRefreshedEvent = new ContextRefreshedEvent(applicationContext);
         
         ShenyuClientConfig shenyuClientConfig = mock(ShenyuClientConfig.class);

Reply via email to