Spring retry @Retryable与JDK动态代理一起工作吗?

Spring retry @Retryable与JDK动态代理一起工作吗?,spring-retry,Spring Retry,我一直在试验spring重试项目。我已经成功地使用了它的@Retryable特性,但是我无法使用JDK动态代理让它工作 我在测试中使用了以下代码片段 @Configuration @EnableRetry public class TestConfig { @Bean public MethodInterceptor retryInterceptor() { return RetryInterceptorBuilder.stateful().maxAttempts

我一直在试验spring重试项目。我已经成功地使用了它的@Retryable特性,但是我无法使用JDK动态代理让它工作

我在测试中使用了以下代码片段

@Configuration
@EnableRetry
public class TestConfig {
    @Bean
    public MethodInterceptor retryInterceptor() {
        return RetryInterceptorBuilder.stateful().maxAttempts(3).build();
    }
}

@Service
public class RetryableServiceImpl implements RetryableService {

    private int count = 0;

    @Retryable(RuntimeException.class)
    @Override
    public void service() {

        if (count++ < 2) {
            throw new RuntimeException("Planned");
        }
    }

    @Override
    public int getCount() {
        return count;
    }

}

@ContextConfiguration(...)
public class RetryableServiceImplTest ... {

    @Autowired
    private RetryableService retryableService;

    @Test
    public void test() {
        assertTrue(AopUtils.isAopProxy(retryableService));
        assertTrue(AopUtils.isJdkDynamicProxy(retryableService));
        assertFalse(AopUtils.isCglibProxy(retryableService));
        retryableService.service();
        assertEquals(3, retryableService.getCount());
    }

}
@配置
@启用重试
公共类TestConfig{
@豆子
public MethodInterceptor retryInterceptor(){
返回RetryInterceptorBuilder.stateful().maxAttempts(3.build();
}
}
@服务
公共类RetryableServiceImpl实现RetryableService{
私有整数计数=0;
@可重试(RuntimeException.class)
@凌驾
公共服务{
如果(计数+++<2){
抛出新的运行时异常(“计划”);
}
}
@凌驾
public int getCount(){
返回计数;
}
}
@上下文配置(…)
公共类RetryableServiceImpletest。。。{
@自动连线
私人可回收服务可回收服务;
@试验
公开无效测试(){
资产真实(AopUtils.isAopProxy(可检索服务));
assertTrue(AopUtils.isJdkDynamicProxy(retryableService));
资产假(AopUtils.isglibproxy(可检索服务));
retryableService.service();
assertEquals(3,retryableService.getCount());
}
}
此处提供的示例项目:

所以,我的问题是

  • 这应该使用cglib或JDK动态代理吗

  • 如果是,我的设置有什么问题

  • 谢谢你的帮助


    你好,乔

    经过进一步调查,回答我自己的问题:

  • 这两种代理机制都可以使用

  • 必须将@Retryable注释应用于接口 为了正确地应用它,类的

  • 调试AnnotationAwareTryOperationsInAcceptor可以帮助我理解这一点