如何创建指向支持接口继承的伪客户端的切入点?

问题描述 投票:0回答:1

在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的情况下使其工作。它在另一个项目中,我没有控制权。

java spring spring-aop spring-cloud-feign pointcut
1个回答
0
投票
@Around("execution(* (@MyAnnotation *).*(..)) || execution(@MyAnnotation * *(..))")

如所解释的here,我仅使用execution来避免创建代理。

© www.soinside.com 2019 - 2024. All rights reserved.