学习这篇博文需要首先明白Spring AOP的实现原理,可以参考Spring的BeanPostProcessor扩展点实现类AbstractAutoProxyCreator和Spring AOP创建代理类之JdkDynamicAopProxy这两篇博文。
EnableTransactionManagement启用 Spring 的注解驱动事务管理能力,和
@EnableTransactionManagement和
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@documented
@import(TransactionManagementConfigurationSelector.class)
public @interface EnableTransactionManagement {
boolean proxyTargetClass() default false;
//指明事务型的advice如何被应用,默认是AdviceMode.PROXY
//需要注意的是,proxy模式只允许拦截通过代理的调用,同一个类的方法本地调用是拦截不了的
//如果在这个方法上加@Transactional注解也是会被Spring忽略的。
//对于更高级的拦截模式,请考虑将其切换为 {@link AdviceMode#ASPECTJ}。
AdviceMode mode() default AdviceMode.PROXY;
//当在特点连接点存在多个advice的时候,指明该事务advisor的执行顺序,默认是最低级别的
int order() default Ordered.LOWEST_PRECEDENCE;
}
关于AdviceMode的解释还是需要理解明白的,可以参考Spring 的注解方式的事务实现机制这个博客里提到的事务在方法自调用中存在的问题。
TransactionManagementConfigurationSelector根据不同EnableTransactionManagement注解中AdviceMode的属性而决定使用抽象事务管理类AbstractTransactionManagementConfiguration的哪个实现类。
public class TransactionManagementConfigurationSelector extends AdviceModeimportSelectorProxyTransactionManagementConfiguration{ @Override protected String[] selectimports(AdviceMode adviceMode) { switch (adviceMode) { case PROXY: return new String[] {AutoProxyRegistrar.class.getName(), ProxyTransactionManagementConfiguration.class.getName()}; case ASPECTJ: return new String[] {determineTransactionAspectClass()}; default: return null; } } private String determineTransactionAspectClass() { return (ClassUtils.isPresent("javax.transaction.Transactional", getClass().getClassLoader()) ? TransactionManagementConfigUtils.JTA_TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME : TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME); } }
是一个@Configuration 类,它用于注册,启用基于代理注解驱动(proxy-based annotation-driven)的事务管理,所需的 Spring 基础 bean。
@Configuration(proxyBeanMethods = false)
public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {
//事务advisor
@Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor(
TransactionAttributeSource transactionAttributeSource,
TransactionInterceptor transactionInterceptor) {
//
BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
//给advisor设置事务属性源TransactionAttributeSource,里面的pointcut在匹配方法时会用到
advisor.setTransactionAttributeSource(transactionAttributeSource);
//给advisor设置通知advice
advisor.setAdvice(transactionInterceptor);
if (this.enableTx != null) {
advisor.setOrder(this.enableTx.getNumber("order"));
}
return advisor;
}
//注解事务属性源,通过内部的注解属性解析器解析相应注解上的属性,形成事务属性源
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionAttributeSource transactionAttributeSource() {
return new AnnotationTransactionAttributeSource();
}
//事务代理拦截器,实现了MethodInterceptor接口,最终实现了Advice接口
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionInterceptor transactionInterceptor(
TransactionAttributeSource transactionAttributeSource) {
TransactionInterceptor interceptor = new TransactionInterceptor();
//给拦截器设置事务属性源;方法、类上的事务属性信息从该属性源获取,其存储着所有事务属性信息
interceptor.setTransactionAttributeSource(transactionAttributeSource);
//设置事务管理器TransactionManager
if (this.txManager != null) {
interceptor.setTransactionManager(this.txManager);
}
return interceptor;
}
}
扩展学习
Spring 的注解方式的事务实现机制Spring的BeanPostProcessor扩展点实现类AbstractAutoProxyCreatorSpring AOP创建代理类之JdkDynamicAopProxySpring 的注解方式的事务实现机制Spring的TransactionDefinition中定义的事务的传播特性和隔离级别详解Spring的事务管理PlatformTransactionManager



