我必须处理一个无法使用 Java-Config for Spring 的项目,但我必须使用 XML-Config。现在我正在寻找与 Java-Config 中的 @EnableRetry 等效的 XML-Config。
我希望这条线能正常工作。
@Retryable(SQLException.class)
public void saveOrUpdate(Entity entity)
最佳答案
是的,空的@Configuration
上的@EnableRetry
将起作用(OP 评论)。
为了完整起见,这里是 equivalent in XML .
<context:annotation-config />
<aop:aspectj-autoproxy />
<bean class="org.springframework.retry.annotation.RetryConfiguration" />
编辑
使用 XML 启用重试的完整示例:
@SpringBootApplication
@ImportResource("context.xml")
public class So31923175Application {
public static void main(String[] args) {
SpringApplication.run(So31923175Application.class, args);
}
@Bean
public ApplicationRunner runner(Foo app) {
return args -> {
try {
app.retry("foo");
}
catch (Exception e) {
e.printStackTrace();
}
};
}
@Component
public static class Foo {
@Retryable
public void retry(String param) {
System.out.println(param);
throw new RuntimeException("test retry");
}
}
}
和
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config />
<aop:aspectj-autoproxy />
<bean class="org.springframework.retry.annotation.RetryConfiguration" />
</beans>
和
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.gprussell.filemailer</groupId>
<artifactId>so31923175</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>so31923175</name>
<description>Mail non-empty files passed on the command line</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
和
foo
foo
foo
java.lang.RuntimeException: test retry
关于java - Spring 重试 XML 等效于 @EnableRetry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31923175/