spring-boot - 弹性4j注释不适用于chlid类
问题描述
我正在将弹性 4j 与 SpringBoot 一起使用。我看到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
}
}
这是预期的行为吗?如果我遗漏了什么,你能告诉我吗
解决方案
我以前从未使用过 Resilience4j,但我可以告诉你的关于 Java 注释的一般信息是:
- 子类中被覆盖的方法永远不会从原始父类方法继承注释。
- 在实现接口的类中,实现方法永远不会从相应的接口方法继承注释。
- 实现接口的类永远不会从接口本身继承注释。
- 扩展另一个接口的接口也永远不会继承任何类型或方法级别的注释。
- 默认情况下,甚至子类都不会从其父类继承注释。
- 这个“注解永远不会被继承”规则有一个例外:当且仅当注解类本身带有元注解时,类型级别注解(类似于
@Foo class Base
,也可以是抽象的)可以被子类(类似于class Sub extends Base
)继承.@Inherited
话虽如此并查看@Retry
注释,您会注意到那里没有@Inherited
注释,因此它也不适用于您的情况。
如果有另一种方法(例如通过反射)在 Resilience4j 中完成这项工作,我不知道,因为正如我所说,我以前从未使用过它。
推荐阅读
- leaflet - 透明 div“上方”地图阻止拖动
- css - Flexbox 行使用顶部菜单和灵活的底部内容
- angular - 查看服务器上存在的文档
- javascript - 如何为 Azure 表存储 REST 请求生成 SharedKeyLite
- php - 循环和排序结果而不重复
- android - 将对象从一个片段传递到另一个片段并将接收到的对象存储在全局对象中
- java - 无法为任务“:compileGroovy”捕获输入文件的指纹
- jenkins - Jenkins Terminal 不支持执行 hg purge
- javascript - 如何使用 Sinon 正确模拟 require('uuid/v1')
- java - 未触发 Firestore 的 AddonCompleteListener