亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring-Retry在實現接口時無法定位恢復方法

Spring-Retry在實現接口時無法定位恢復方法

藍山帝景 2021-12-30 17:17:18
我在用 spring boot 做 spring-retry 時發現了一個問題。類實現接口時,超過最大重試次數后無法進入@recover方法。但是當我注入一個普通的類時,我可以進入這個方法。您的及時幫助和善意的建議將不勝感激,謝謝!當我這樣做時,我可以進入@Recover 方法@Servicepublic 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;    }}但是一旦類實現了另一個接口,它就不起作用了@Servicepublic 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;    }}
查看完整描述

2 回答

?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

Spring-Retry 使用 AOP 來實現@Retry. 使用 AOP 時,默認使用 JDK 動態代理。JDK 動態代理是基于接口的。

這意味著您將獲得一個動態創建的類,該類偽裝成 aTestServiceI但它不是TestService. 代理不包含該recover方法(因為它不在接口上),因此 Spring Retry 無法檢測到它。

要解決此問題,您需要通過將proxyTargetClass屬性設置@EnableRetrytrue( 請參閱javadoc )來為 Spring Retry 啟用基于類的代理。

@EnableRetry(proxyTargetClass=true)


查看完整回答
反對 回復 2021-12-30
?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

  1. 標記 proxyTargetClass=true: @EnableRetry(proxyTargetClass = true)

  2. @Recover 方法必須返回與原方法相同的類型;



查看完整回答
反對 回復 2021-12-30
  • 2 回答
  • 0 關注
  • 227 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號