我在做弹簧重试时发现了一个问题。当一个类实现一个接口时,它不能在超过最大重试次数之后进入@recover方法。但是当我注入一个正常的类,我可以输入这个method.Your提示帮助和善意的建议将不胜感激,谢谢!
当我这样做时,我可以输入@Recover方法
@Service
public class TestService {
@Retryable(Exception.class)
public String retry(String c) throws Exception{
throw new Exception();
}
@Recover
public String recover(Exception e,String c) throws Exception{
System.out.println("got error");
return null;
}
}但是一旦这个类实现了另一个接口,它就不能工作了
@Service
public class TestService implements TestServiceI{
@Override
@Retryable(Exception.class)
public String retry(String c) throws Exception{
throw new Exception();
}
@Recover
public String recover(Exception e,String c) throws Exception{
System.out.println("got error");
return null;
}
}发布于 2018-11-05 09:24:33
Spring使用AOP实现@Retry。在使用AOP时,默认情况是使用JDK动态代理。JDK动态代理是基于接口的。
这意味着您得到了一个动态创建的类,它预先准备成为一个TestServiceI,但是它不是一个TestService。代理不包括recover方法(因为它不在接口上),因此Spring无法检测它。
要解决这个问题,需要通过将proxyTargetClass属性设置为true (参见javadoc),从而为Spring启用基于类的代理。
@EnableRetry(proxyTargetClass=true) 发布于 2021-07-20 03:22:17
https://stackoverflow.com/questions/53150345
复制相似问题