Java 为什么Spring@Retryable不提供重试?

Java 为什么Spring@Retryable不提供重试?,java,spring,spring-retry,Java,Spring,Spring Retry,我只是用@Retryable注释设置了最简单的Spring应用程序 @Service public class MyRestService { @Autowired private RestTemplate restTemplate; @Retryable(Exception.class) public void runRest() { ResponseEntity<String> response = restTemplate.ge

我只是用@Retryable注释设置了最简单的Spring应用程序

@Service
public class MyRestService {
    @Autowired
    private RestTemplate restTemplate;

    @Retryable(Exception.class)
    public void runRest() {
        ResponseEntity<String> response = restTemplate.getForEntity(
            "https://dat.sparkfun.com/streams/dZ4EVmE8yGCRGx5XRX1W.json",
            String.class);
    }

    @Recover
    public void recover(Exception e) {
        System.out.println("Recover=" + e);
    }
}
@服务
公共类myrest服务{
@自动连线
私有RestTemplate RestTemplate;
@可检索(Exception.class)
公共void runRest(){
ResponseEntity response=restemplate.getForEntity(
"https://dat.sparkfun.com/streams/dZ4EVmE8yGCRGx5XRX1W.json",
字符串(类);
}
@恢复
公共无效恢复(例外e){
System.out.println(“Recover=“+e”);
}
}

根据Spring文档()的说明,runRest方法应该运行三次,因为它会引发异常(特别是org.springframework.web.client.ResourceAccessException)。但是,我没有观察到任何重试。将ResourceAccessException用作@Retryable的参数没有帮助。

对不起,答案很简单。我需要在类中使用main方法指定@enablerery

@Configuration
@EnableRetry
public class Application {

您也可以使用基于XML的配置

<aop:config>
<aop:pointcut id="retryable" expression="execution(* it..*class.method(..))" />
<aop:advisor pointcut-ref="retryable" advice-ref="retryAdvice" order="-1"/>


本期杂志包含了一些与此相关的详细信息