0

我正在尝试设置一个非常简单的测试,看看我是否可以让 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次吗?我在这里想念什么?

4

1 回答 1

2

你需要展示你的MyConfig班级。

@EnableRetry继续你的一个类@Configuration,而不是可重试的目标。

有关注释,请参见 java doc:

 * Global enabler for <code>@Retryable</code> annotations in Spring beans. If this is
 * declared on any <code>@Configuration</code> in the context then beans that have
 * retryable methods will be proxied and the retry handled according to the metadata in
 * the annotations.
于 2015-12-17T21:45:17.290 回答