如何创建切入点来伪装支持接口继承的客户端? [英] How to create pointcut to feign client that supports interface inheritance?

查看:0
本文介绍了如何创建切入点来伪装支持接口继承的客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个Spring Boot项目中,我有一个简单的假客户端

@MyAnnotation
@FeignClient(name="some-name", url="http://test.url")
public interface MyClient {
    @RequestMapping(method = RequestMethod.GET, value = "/endpoint")
    List<Store> getSomething();
}

我需要拦截所有调用,为此,我正在创建一个可在不同项目中使用的公共库。为了实现它,我尝试使用了Spring AOP。我创建了一个方面,它包装了用MyAnnotation

注释的对象的所有公共方法
@Around("@within(MyAnnotation) && execution(public * *(..))")
public Object myWrapper(ProceedingJoinPoint invocation) throws Throwable {
   // ...
}
它工作正常,所有调用都会被截获,直到我尝试将MyAnnotation放在使用feign接口继承的feign客户端上。当我使用继承的接口调用初始化我的客户端时,不再被拦截。

public interface FeignClientInterface {
    @RequestMapping(method = RequestMethod.GET, value = "/endpoint")
    List<Store> getSomething();
}

@MyAnnotation
@FeignClient(name="some-name", url="http://test.url")
public interface MyClient extends FeignClientInterface{ 
}

我已尝试:

  • "@target(MyAnnotation) && execution(public * *(..))"但当我将我的库连接到实际项目时,我得到了java.lang.IllegalArgumentException: Cannot subclass final class org.springframework.boot.autoconfigure.AutoConfigurationPackages$BasePackages,它似乎想要将所有内容包装到代理,并且有最终的类。
  • "@target(MyAnnotation) && execution(public * com.my.company.base.package.*(..))"删除了上一个问题,但给出了另一个问题,如某些Bean没有名称无法实例化等。
问题是如何在不将@MyAnnotation移动到基本接口FeignClientInterface的情况下使其工作。它在另一个项目中,我无法控制它。

推荐答案

好的,经过几个小时的调查,我用这个替换了我的切入点

@Around("execution(* (@MyAnnotation *).*(..)) || execution(@MyAnnotation * *(..))")

如前所述here我使用execution只是为了避免创建代理。

这篇关于如何创建切入点来伪装支持接口继承的客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆