This is an automated email from the ASF dual-hosted git repository.
xiaoyu 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 807cd22 [ISSUE #2833] fix TarsServiceBeanPostProcessor when client
use AOP (#2834)
807cd22 is described below
commit 807cd2265ad8dcb7d57478456477cbc7d395fb99
Author: dragon-zhang <[email protected]>
AuthorDate: Wed Feb 9 11:14:58 2022 +0800
[ISSUE #2833] fix TarsServiceBeanPostProcessor when client use AOP (#2834)
* fix TarsServiceBeanPostProcessor when client use AOP
* remove @author
---
.../client/tars/TarsServiceBeanPostProcessor.java | 5 ++-
shenyu-examples/shenyu-examples-tars/pom.xml | 5 +++
.../org/apache/shenyu/examples/tars/aop/Log.java | 30 ++++++++++++++
.../shenyu/examples/tars/aop/LogInterceptor.java | 48 ++++++++++++++++++++++
.../servant/testapp/impl/HelloServantImpl.java | 2 +
5 files changed, 88 insertions(+), 2 deletions(-)
diff --git
a/shenyu-client/shenyu-client-tars/src/main/java/org/apache/shenyu/client/tars/TarsServiceBeanPostProcessor.java
b/shenyu-client/shenyu-client-tars/src/main/java/org/apache/shenyu/client/tars/TarsServiceBeanPostProcessor.java
index 8dc7040..637dab7 100644
---
a/shenyu-client/shenyu-client-tars/src/main/java/org/apache/shenyu/client/tars/TarsServiceBeanPostProcessor.java
+++
b/shenyu-client/shenyu-client-tars/src/main/java/org/apache/shenyu/client/tars/TarsServiceBeanPostProcessor.java
@@ -35,6 +35,7 @@ import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
+import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
@@ -78,7 +79,7 @@ public class TarsServiceBeanPostProcessor implements
BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(final Object bean, final
String beanName) throws BeansException {
- if (bean.getClass().getAnnotation(ShenyuTarsService.class) != null) {
+ if (AnnotationUtils.findAnnotation(bean.getClass(),
ShenyuTarsService.class) != null) {
handler(bean);
}
return bean;
@@ -90,7 +91,7 @@ public class TarsServiceBeanPostProcessor implements
BeanPostProcessor {
clazz = AopUtils.getTargetClass(serviceBean);
}
Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(clazz);
- String serviceName =
serviceBean.getClass().getAnnotation(ShenyuTarsService.class).serviceName();
+ String serviceName =
clazz.getAnnotation(ShenyuTarsService.class).serviceName();
for (Method method : methods) {
ShenyuTarsClient shenyuTarsClient =
method.getAnnotation(ShenyuTarsClient.class);
if (Objects.nonNull(shenyuTarsClient)) {
diff --git a/shenyu-examples/shenyu-examples-tars/pom.xml
b/shenyu-examples/shenyu-examples-tars/pom.xml
index d8bc8ae..7a4aaad 100644
--- a/shenyu-examples/shenyu-examples-tars/pom.xml
+++ b/shenyu-examples/shenyu-examples-tars/pom.xml
@@ -39,6 +39,11 @@
</dependency>
<dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-aop</artifactId>
+ </dependency>
+
+ <dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-spring-boot-starter-client-tars</artifactId>
<version>${project.version}</version>
diff --git
a/shenyu-examples/shenyu-examples-tars/src/main/java/org/apache/shenyu/examples/tars/aop/Log.java
b/shenyu-examples/shenyu-examples-tars/src/main/java/org/apache/shenyu/examples/tars/aop/Log.java
new file mode 100644
index 0000000..6d624f8
--- /dev/null
+++
b/shenyu-examples/shenyu-examples-tars/src/main/java/org/apache/shenyu/examples/tars/aop/Log.java
@@ -0,0 +1,30 @@
+/*
+ * 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.shenyu.examples.tars.aop;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface Log {
+}
diff --git
a/shenyu-examples/shenyu-examples-tars/src/main/java/org/apache/shenyu/examples/tars/aop/LogInterceptor.java
b/shenyu-examples/shenyu-examples-tars/src/main/java/org/apache/shenyu/examples/tars/aop/LogInterceptor.java
new file mode 100644
index 0000000..c4f4686
--- /dev/null
+++
b/shenyu-examples/shenyu-examples-tars/src/main/java/org/apache/shenyu/examples/tars/aop/LogInterceptor.java
@@ -0,0 +1,48 @@
+/*
+ * 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.shenyu.examples.tars.aop;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+@Aspect
+@Component
+public class LogInterceptor {
+
+ private static final Logger log =
LoggerFactory.getLogger(LogInterceptor.class);
+
+ @Pointcut("@annotation(org.apache.shenyu.examples.tars.aop.Log)")
+ public void logPointcut() {
+ //just for pointcut
+ }
+
+ @Around("logPointcut()")
+ public Object around(ProceedingJoinPoint point) throws Throwable {
+ try {
+ log.info("before");
+ return point.proceed(point.getArgs());
+ } finally {
+ log.info("after");
+ }
+ }
+}
diff --git
a/shenyu-examples/shenyu-examples-tars/src/main/java/org/apache/shenyu/examples/tars/servant/testapp/impl/HelloServantImpl.java
b/shenyu-examples/shenyu-examples-tars/src/main/java/org/apache/shenyu/examples/tars/servant/testapp/impl/HelloServantImpl.java
index e334696..9791163 100644
---
a/shenyu-examples/shenyu-examples-tars/src/main/java/org/apache/shenyu/examples/tars/servant/testapp/impl/HelloServantImpl.java
+++
b/shenyu-examples/shenyu-examples-tars/src/main/java/org/apache/shenyu/examples/tars/servant/testapp/impl/HelloServantImpl.java
@@ -20,6 +20,7 @@ package org.apache.shenyu.examples.tars.servant.testapp.impl;
import com.qq.tars.spring.annotation.TarsServant;
import org.apache.shenyu.client.tars.common.annotation.ShenyuTarsClient;
import org.apache.shenyu.client.tars.common.annotation.ShenyuTarsService;
+import org.apache.shenyu.examples.tars.aop.Log;
import org.apache.shenyu.examples.tars.servant.testapp.HelloServant;
@TarsServant("HelloObj")
@@ -32,6 +33,7 @@ public class HelloServantImpl implements HelloServant {
return String.format("hello no=%s, name=%s, time=%s", no, name,
System.currentTimeMillis());
}
+ @Log
@Override
@ShenyuTarsClient(path = "/helloInt", desc = "helloInt")
public int helloInt(final int no, final String name) {