我正在尝试设置一个非常简单的测试,看看我是否可以让 Springs Retry API 工作,但它似乎没有按预期工作。下面是我的代码/配置。我正在使用 Spring 3.0
POM.xml 中定义的 Spring 重试版本
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.1.2.RELEASE</version>
</dependency>
被测类的接口
public interface RetryTest
{
void test() throws Exception;
}
接口实现
@Component("retryTest")
@EnableRetry
public class RetryTestImpl implements RetryTest
{
@Retryable(maxAttempts = 3)
public void test() throws Exception
{
System.out.println("try!");
throw new Exception();
}
}
JUnit 类
public class TestIt
{
@Before
public void initSpringContext()
{
// Load the spring context
ApplicationContextUtil.initAppContext(MyConfig.class);
}
@Test
public void test_retryLogic() throws Exception
{
RetryTest retryTest = (RetryTest)ApplicationContextUtil.getApplicationContext().getBean(
"retryTest");
retryTest.test();
}
}
控制台输出
Bean loaded: retryTest
try!
我期待着“尝试!” 打印到控制台 3 次。而是抛出一个异常,并且 JUnit 在那里失败。重试不应该运行3次吗?我在这里想念什么?