2

我一直在尝试 spring-retry 项目。我已经成功使用了它的 @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());
    }

}

此处提供示例项目: https ://github.com/maddenj-ie/retry.git

所以,我的问题是

  1. 这应该使用 cglib 或 JDK 动态代理吗?

  2. 如果是这样,我的设置有什么问题?

谢谢您的帮助。

问候,乔

4

1 回答 1

3

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

  1. 它确实适用于两种代理机制。

  2. @Retryable 注解必须应用于接口而不是类,才能正确应用它。

调试 AnnotationAwareRetryOperationsInterceptor 有助于我理解这一点。

于 2014-11-29T12:50:41.187 回答