上一篇文章,我们快速介绍了下spring-retry的使用技巧,本篇我们将会剖析源码去学习
一、 EnableRetry注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@EnableAspectJAutoProxy(proxyTargetClass = false)
@Import(RetryConfiguration.class)
@Documented
public @interface EnableRetry {
/**
* Indicate whether subclass-based (CGLIB) proxies are to be created as opposed to
* standard Java interface-based proxies. The default is {@code false}.
* @return whether to proxy or not to proxy the class
*/
@AliasFor(annotation = EnableAspectJAutoProxy.class)
boolean proxyTargetClass() default false;
/**
* Indicate the order in which the {@link RetryConfiguration} AOP <b>advice</b> should
* be applied.
* <p>
* The default is {@code Ordered.LOWEST_PRECEDENCE - 1} in order to make sure the
* advice is applied before other advices with {@link Ordered#LOWEST_PRECEDENCE} order
* (e.g. an advice responsible for {@code @Transactional} behavior).
*/
int order() default Ordered.LOWEST_PRECEDENCE - 1;
}
英文翻译我就不再解释了,上面说的很清楚;这里重点提一下@Import(RetryConfiguration.class)这个注解,表明了@EnableRetry注解的启动配置类是RetryConfiguration, 通过@Import注解来注入对应的配置类,这样的做法同样可见于@EnableAsync/@EnableScheduling等注解上; 看到这里,如何定义一个Enable配置注解是不是会了呢~
二、 RetryConfiguration如何构建 ponintCut和advisor
@SuppressWarnings("serial")
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Component
public class RetryConfiguration extends AbstractPointcutAdvisor
implements IntroductionAdvisor, BeanFactoryAware, InitializingBean, SmartInitializingSingleton, ImportAware {
//在这里构建了ponintCut和advice
@Override
public void afterPropertiesSet() throws Exception {
this.retryContextCache = findBean(RetryContextCache.class);
this.methodArgumentsKeyGenerator = findBean(MethodArgumentsKeyGenerator.class);
this.newMethodArgumentsIdentifier = findBean(NewMethod