Ant*_*yev 1 java aop aspectj spring-aop feign
我对如何在AOP中使用Feign客户感兴趣.例如:
API:
public interface LoanClient {
@RequestLine("GET /loans/{loanId}")
@MeteredRemoteCall("loans")
Loan getLoan(@Param("loanId") Long loanId);
}
Run Code Online (Sandbox Code Playgroud)
配置:
@Aspect
@Component // Spring Component annotation
public class MetricAspect {
@Around(value = "@annotation(annotation)", argNames = "joinPoint, annotation")
public Object meterRemoteCall(ProceedingJoinPoint joinPoint,
MeteredRemoteCall annotation) throws Throwable {
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何"拦截"api方法调用.我哪里做错了?
更新:
我的Spring类注释:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MeteredRemoteCall {
String serviceName();
}
Run Code Online (Sandbox Code Playgroud)
您的情况有点复杂,因为您有几个问题:
LoanClient的不是Spring @Component.call()而且仅支持execution().即它只编织到执行方法的地方,而不是它被调用的地方.@MeteredRemoteCall它们永远不会被它们的实现类继承.事实上,方法注释永远不会在Java中继承,只能从类(不是接口!)到相应的子类进行类级注释.也就是说,即使你的注释类有一个@Inherited元注解,它不会帮助@Target({ElementType.METHOD}),只为@Target({ElementType.TYPE}).更新:因为我之前已经多次回答过这个问题,所以我刚刚记录了问题,并且还使用了AspectJ来模拟接口和方法的注释继承.所以,你可以做什么?最好的选择是在Spring应用程序中通过LTW(加载时编织)使用完整的AspectJ.这使您可以使用call()切入点而不是execution()Spring AOP隐式使用的切入点.如果你@annotation()在AspectJ中的方法上使用切入点,它将匹配调用和执行,因为我将在一个独立的示例中显示(没有Spring,但效果与Spring中的LTW相同):
标记注释:
package de.scrum_master.app;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MeteredRemoteCall {}
Run Code Online (Sandbox Code Playgroud)
假装客户:
此示例客户端将完整的StackOverflow问题页面(HTML源代码)作为字符串进行抓取.
package de.scrum_master.app;
import feign.Param;
import feign.RequestLine;
public interface StackOverflowClient {
@RequestLine("GET /questions/{questionId}")
@MeteredRemoteCall
String getQuestionPage(@Param("questionId") Long questionId);
}
Run Code Online (Sandbox Code Playgroud)
司机申请:
此应用程序以三种不同的方式使用Feign客户端界面进行演示:
package de.scrum_master.app;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import feign.Feign;
import feign.codec.StringDecoder;
public class Application {
public static void main(String[] args) {
StackOverflowClient soClient;
long questionId = 41856687L;
soClient = new StackOverflowClient() {
@Override
public String getQuestionPage(Long loanId) {
return "StackOverflowClient without Feign";
}
};
System.out.println(" " + soClient.getQuestionPage(questionId));
soClient = new StackOverflowClient() {
@Override
@MeteredRemoteCall
public String getQuestionPage(Long loanId) {
return "StackOverflowClient without Feign + extra annotation";
}
};
System.out.println(" " + soClient.getQuestionPage(questionId));
// Create StackOverflowClient via Feign
String baseUrl = "http://stackoverflow.com";
soClient = Feign
.builder()
.decoder(new StringDecoder())
.target(StackOverflowClient.class, baseUrl);
Matcher titleMatcher = Pattern
.compile("<title>([^<]+)</title>", Pattern.CASE_INSENSITIVE)
.matcher(soClient.getQuestionPage(questionId));
titleMatcher.find();
System.out.println(" " + titleMatcher.group(1));
}
}
Run Code Online (Sandbox Code Playgroud)
无方面的控制台日志:
StackOverflowClient without Feign
StackOverflowClient without Feign + extra annotation
java - How to use AOP with Feign calls - Stack Overflow
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,在#3的情况下,它只打印了这个StackOverflow问题的问题标题.;-)我正在使用正则表达式匹配器,以便从HTML代码中提取它,因为我不想打印完整的网页.
方面:
这基本上是您使用其他连接点日志记录的方面.
package de.scrum_master.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import de.scrum_master.app.MeteredRemoteCall;
@Aspect
public class MetricAspect {
@Around(value = "@annotation(annotation)", argNames = "joinPoint, annotation")
public Object meterRemoteCall(ProceedingJoinPoint joinPoint, MeteredRemoteCall annotation)
throws Throwable
{
System.out.println(joinPoint);
return joinPoint.proceed();
}
}
Run Code Online (Sandbox Code Playgroud)
带方面的控制台日志:
call(String de.scrum_master.app.StackOverflowClient.getQuestionPage(Long))
StackOverflowClient without Feign
call(String de.scrum_master.app.StackOverflowClient.getQuestionPage(Long))
execution(String de.scrum_master.app.Application.2.getQuestionPage(Long))
StackOverflowClient without Feign + extra annotation
call(String de.scrum_master.app.StackOverflowClient.getQuestionPage(Long))
java - How to use AOP with Feign calls - Stack Overflow
Run Code Online (Sandbox Code Playgroud)
如您所见,以下三个案例中的每一个都会拦截以下连接点:
call()因为即使使用手动实例化,实现类也没有接口方法的注释.所以execution()无法匹敌.call()并execution()因为我们手动添加标记注释实现类.call()因为Feign创建的动态代理没有接口方法的注释.所以execution()无法匹敌.我希望这可以帮助您了解发生了什么以及为什么.
底线:使用完整的AspectJ以使您的切入点与call()连接点匹配.然后你的问题就解决了.
| 归档时间: |
|
| 查看次数: |
1695 次 |
| 最近记录: |