在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
放到对假接口使用继承的假客户机上。当我初始化带有继承接口调用的客户端时,不再进行拦截。
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
来避免创建代理。