如何创建切入点来伪装支持接口继承的客户端?切入点、客户端、接口

2023-09-04 02:24:52 作者:初见.

在一个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{ 
}
用蛤蟆连细胞分裂5局域网提示没有找到比赛

我已尝试:

"@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只是为了避免创建代理。