resilience4j注释不适用于chlid类

时间:2020-04-22 08:35:27

标签: spring-boot aop resilience4j

我在SpringBoot中使用了resilience4j。我看到,只有将resilience4j批注放置在引发异常的类中时,它们才起作用。如果该类被另一个类扩展,并且父类具有注释,则重试将不起作用。

Resilience4j配置

resilience4j.retry: 
  instances:       
    service: 
      maxRetryAttempts: 5
      waitDuration: 1000
      retryException: 
        - com.common.exception.RetriableException

父母阶级

@Retry(name = "service")
@Component
public class httpClient extends client{

  // This method is invoked from outside
  public HttpResponse<T> getResponse(
      String url, Class<T> responseType) {
    return super.getResponse(url, requestEntity, responseType);
  }
}

儿童班

@Retry(name = "service") // Without this line, the retries don't work, even though it is present in the parent class
@Component
public class client{

  public HttpResponse<T> getResponse(
      String url, Class<T> responseType) {
    //Impl which throws RetriableException
  }
}

这是预期的行为吗?你能告诉我我是否想念东西

1 个答案:

答案 0 :(得分:1)

我以前从未使用过Resilience4j,但是我可以大致了解一下Java注释:

  • 子类中的重写方法从不继承原始父类方法的注释。
  • 在实现接口的类中,实现方法从不从相应的接口方法继承注释。
  • 实现接口的类从不继承接口本身的注释。
  • 扩展另一个接口的接口也从不继承任何类型或方法级别的注释。
  • 默认情况下,子类甚至都不从其父类继承注释。
  • 此“永不继承注释”规则有一个例外:类型级别注释(例如@Foo class Base,也可以是抽象的) 可以 由子类(类似class Sub extends Base之类) 当且仅当 时,注释类本身带有元注释@Inherited

说了这么多,然后看着@Retry注释,您会发现那里没有@Inherited注释,因此它也无法在您的情况下使用。

如果有其他方法(例如通过反射)在Resilience4j中完成此操作,我不知道,因为正如我所说,我以前从未使用过。

相关问题