You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by zh...@apache.org on 2022/06/10 13:43:39 UTC

[incubator-shenyu] branch master updated: [ISSUE #3485] simplify ShenyuSpringWebSocketClient (#3520)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 9945eb9f9 [ISSUE #3485] simplify ShenyuSpringWebSocketClient (#3520)
9945eb9f9 is described below

commit 9945eb9f9034de47524b91ba5c14676131185a81
Author: weihubeats <we...@163.com>
AuthorDate: Fri Jun 10 21:43:31 2022 +0800

    [ISSUE #3485] simplify ShenyuSpringWebSocketClient (#3520)
---
 .../annotation/ShenyuSpringWebSocketClient.java    | 10 +++++++
 .../SpringWebSocketClientBeanPostProcessor.java    | 35 ++++++++++++++--------
 .../examples/websocket/ws/WsServerEndpoint.java    |  2 +-
 .../examples/websocket/config/WebSocketConfig.java |  2 +-
 .../websocket/config/WebSocketConfiguration.java   |  7 +++--
 .../examples/websocket/handler/EchoHandler.java    |  2 +-
 6 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/main/java/org/apache/shenyu/client/spring/websocket/annotation/ShenyuSpringWebSocketClient.java b/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/main/java/org/apache/shenyu/client/spring/websocket/annotation/ShenyuSpringWebSocketClient.java
index de45b7c35..148ba199d 100644
--- a/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/main/java/org/apache/shenyu/client/spring/websocket/annotation/ShenyuSpringWebSocketClient.java
+++ b/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/main/java/org/apache/shenyu/client/spring/websocket/annotation/ShenyuSpringWebSocketClient.java
@@ -17,6 +17,8 @@
 
 package org.apache.shenyu.client.spring.websocket.annotation;
 
+import org.springframework.core.annotation.AliasFor;
+
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -28,12 +30,20 @@ import java.lang.annotation.Target;
 @Retention(RetentionPolicy.RUNTIME)
 @Target({ElementType.TYPE, ElementType.METHOD})
 public @interface ShenyuSpringWebSocketClient {
+
+    /**
+     * value string.
+     * @return the string
+     */
+    @AliasFor(attribute = "path")
+    String value() default "";
     
     /**
      * Path string.
      *
      * @return the string
      */
+    @AliasFor(attribute = "value")
     String path() default "";
     
     /**
diff --git a/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/main/java/org/apache/shenyu/client/spring/websocket/init/SpringWebSocketClientBeanPostProcessor.java b/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/main/java/org/apache/shenyu/client/spring/websocket/init/SpringWebSocketClientBeanPostProcessor.java
index 1eaf154b7..86aba860a 100644
--- a/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/main/java/org/apache/shenyu/client/spring/websocket/init/SpringWebSocketClientBeanPostProcessor.java
+++ b/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/main/java/org/apache/shenyu/client/spring/websocket/init/SpringWebSocketClientBeanPostProcessor.java
@@ -17,12 +17,6 @@
 
 package org.apache.shenyu.client.spring.websocket.init;
 
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
-import java.util.Properties;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.shenyu.client.core.constant.ShenyuClientConstants;
@@ -35,13 +29,22 @@ import org.apache.shenyu.register.common.config.PropertiesConfig;
 import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.aop.support.AopUtils;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.config.BeanPostProcessor;
+import org.springframework.core.annotation.AnnotatedElementUtils;
 import org.springframework.core.annotation.AnnotationUtils;
 import org.springframework.lang.NonNull;
 import org.springframework.lang.Nullable;
 import org.springframework.util.ReflectionUtils;
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Properties;
+
 /**
  * The type Shenyu websocket client bean post processor.
  */
@@ -93,20 +96,26 @@ public class SpringWebSocketClientBeanPostProcessor implements BeanPostProcessor
     public Object postProcessAfterInitialization(@NonNull final Object bean,
         @NonNull final String beanName) throws BeansException {
 
-        final String superPath = buildApiSuperPath(bean.getClass());
+        Class<?> clazz;
+        clazz = bean.getClass();
+        if (AopUtils.isAopProxy(bean)) {
+            clazz = AopUtils.getTargetClass(bean);
+        }
+
+        final String superPath = buildApiSuperPath(clazz);
         // Filter out is not controller out
-        if (Boolean.TRUE.equals(isFull) || !hasAnnotation(bean.getClass(), ShenyuSpringWebSocketClient.class)) {
+        if (Boolean.TRUE.equals(isFull) || !hasAnnotation(clazz, ShenyuSpringWebSocketClient.class)) {
             return bean;
         }
-        final ShenyuSpringWebSocketClient beanShenyuClient = AnnotationUtils.findAnnotation(bean.getClass(), ShenyuSpringWebSocketClient.class);
+        final ShenyuSpringWebSocketClient beanShenyuClient = AnnotatedElementUtils.findMergedAnnotation(clazz, ShenyuSpringWebSocketClient.class);
         // Compatible with previous versions
         if (Objects.nonNull(beanShenyuClient) && superPath.contains("*")) {
             publisher.publishEvent(buildMetaDataDTO(beanShenyuClient, pathJoin(contextPath, superPath)));
             return bean;
         }
-        final Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(bean.getClass());
+        final Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(clazz);
         for (Method method : methods) {
-            ShenyuSpringWebSocketClient webSocketClient = AnnotationUtils.findAnnotation(method, ShenyuSpringWebSocketClient.class);
+            ShenyuSpringWebSocketClient webSocketClient = AnnotatedElementUtils.findMergedAnnotation(method, ShenyuSpringWebSocketClient.class);
             webSocketClient = Objects.isNull(webSocketClient) ? beanShenyuClient : webSocketClient;
             if (Objects.nonNull(webSocketClient)) {
                 publisher.publishEvent(buildMetaDataDTO(webSocketClient, buildApiPath(method, superPath)));
@@ -117,7 +126,7 @@ public class SpringWebSocketClientBeanPostProcessor implements BeanPostProcessor
 
     private <A extends Annotation> boolean hasAnnotation(final @NonNull Class<?> clazz,
         final @NonNull Class<A> annotationType) {
-        return Objects.nonNull(AnnotationUtils.findAnnotation(clazz, annotationType));
+        return Objects.nonNull(AnnotatedElementUtils.findMergedAnnotation(clazz, annotationType));
     }
 
     private String buildApiPath(@NonNull final Method method, @NonNull final String superPath) {
@@ -161,7 +170,7 @@ public class SpringWebSocketClientBeanPostProcessor implements BeanPostProcessor
     }
 
     private String buildApiSuperPath(@NonNull final Class<?> method) {
-        ShenyuSpringWebSocketClient webSocketClient = AnnotationUtils.findAnnotation(method, ShenyuSpringWebSocketClient.class);
+        ShenyuSpringWebSocketClient webSocketClient = AnnotatedElementUtils.findMergedAnnotation(method, ShenyuSpringWebSocketClient.class);
         if (Objects.nonNull(webSocketClient) && StringUtils.isNotBlank(webSocketClient.path())) {
             return webSocketClient.path();
         }
diff --git a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-annotation-websocket/src/main/java/org/apache/shenyu/examples/websocket/ws/WsServerEndpoint.java b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-annotation-websocket/src/main/java/org/apache/shenyu/examples/websocket/ws/WsServerEndpoint.java
index f5a9d8e3c..68248a590 100644
--- a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-annotation-websocket/src/main/java/org/apache/shenyu/examples/websocket/ws/WsServerEndpoint.java
+++ b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-annotation-websocket/src/main/java/org/apache/shenyu/examples/websocket/ws/WsServerEndpoint.java
@@ -32,7 +32,7 @@ import java.util.Map;
 /**
  * WsServerEndpoint
  */
-@ShenyuSpringWebSocketClient(path = "/myWs", desc = "myWs")
+@ShenyuSpringWebSocketClient("/myWs")
 @ServerEndpoint("/myWs")
 @Component
 public class WsServerEndpoint {
diff --git a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/config/WebSocketConfig.java b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/config/WebSocketConfig.java
index 5223830aa..913fb038f 100644
--- a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/config/WebSocketConfig.java
+++ b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/config/WebSocketConfig.java
@@ -32,7 +32,7 @@ import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry
  */
 @Configuration
 @EnableWebSocket
-@ShenyuSpringWebSocketClient(path = "/myWebSocket", desc = "myWebSocket")
+@ShenyuSpringWebSocketClient("/myWebSocket")
 public class WebSocketConfig implements WebSocketConfigurer {
 
     @Autowired
diff --git a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/config/WebSocketConfiguration.java b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/config/WebSocketConfiguration.java
index 8d2cb1217..7084d095a 100644
--- a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/config/WebSocketConfiguration.java
+++ b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/config/WebSocketConfiguration.java
@@ -17,8 +17,6 @@
 
 package org.apache.shenyu.examples.websocket.config;
 
-import java.util.HashMap;
-import java.util.Map;
 import org.apache.shenyu.client.spring.websocket.annotation.ShenyuSpringWebSocketClient;
 import org.apache.shenyu.examples.websocket.handler.EchoHandler;
 import org.springframework.context.annotation.Bean;
@@ -29,11 +27,14 @@ import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
 import org.springframework.web.reactive.socket.WebSocketHandler;
 import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * The type Web socket configuration.
  */
 @Configuration
-@ShenyuSpringWebSocketClient(path = "/websocket/**", desc = "myWebSocket")
+@ShenyuSpringWebSocketClient("/websocket/**")
 public class WebSocketConfiguration {
 
     /**
diff --git a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java
index 13ed969d8..c03cd69f5 100644
--- a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java
+++ b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java
@@ -32,7 +32,7 @@ public class EchoHandler implements WebSocketHandler {
 
     @Override
     @NonNull
-    @ShenyuSpringWebSocketClient(path = "/org/apache/shenyu/examples/websocket/**", desc = "onlineusers")
+    @ShenyuSpringWebSocketClient("/org/apache/shenyu/examples/websocket/**")
     public Mono<Void> handle(final WebSocketSession session) {
         return session.send(
                 session.receive()