spring声明式事务
@EnableTransactionManagement
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(TransactionManagementConfigurationSelector.class)
public @interface EnableTransactionManagement {
...........
}
@EnableTransactionManagement注解中使用了import注解,引入了另一个TransactionManagementConfigurationSelector类,然后我们看一下这个类
@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;
}
}
TransactionManagementConfigurationSelector类中有个selectImports方法,在这个方法中有两个重要的组件,分别是:AutoProxyRegistrar和ProxyTransactionManagementConfiguration
加载事务控制组件
AutoProxyRegistrar 组件
在AutoProxyRegistrar类中只有一个方法是registerBeanDefinitions,在这个方法中又注册了一个组件
进入这个方法,可以发现他注册了一个叫InfrastructureAdvisorAutoProxyCreator的bean
InfrastructureAdvisorAutoProxyCreator这个bean是AbstractAdvisorAutoProxyCreator的子类实现了SmartInstantiationAwareBeanPostProcessor接口
继承结构图
他实现了SmartInstantiationAwareBeanPostProcessor接口,也就是说他是以后后置处理器,而且和AOP开启@EnableAspectJAutoProxy注解时AnnotationAwareAspectJProxyCreator是实现的同一个接口,这说明了声明式事务是AOP思想的一种运用。
ProxyTransactionManagementConfiguration 组件
@Configuration
public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {
@Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() {
// 事务增强器
BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
// 向事务增强器中注入 属性解析
advisor.setTransactionAttributeSource(transactionAttributeSource());
// 向事务增强器中注入 拦截器
advisor.setAdvice(transactionInterceptor());
if (this.enableTx != null) {
advisor.setOrder(this.enableTx.<Integer>getNumber("order"));
}
return advisor;
}
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
// 事务解析器
public TransactionAttributeSource transactionAttributeSource() {
return new AnnotationTransactionAttributeSource();
}
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
// 事务拦截器
public TransactionInterceptor transactionInterceptor() {
TransactionInterceptor interceptor = new TransactionInterceptor();
interceptor.setTransactionAttributeSource(transactionAttributeSource());
if (this.txManager != null) {
interceptor.setTransactionManager(this.txManager);
}
return interceptor;
}
}
ProxyTransactionManagementConfiguration是一个容器配置类,在这个类中注册了一个组件BeanFactoryTransactionAttributeSourceAdvisor,被称为事务增强器,然后在这个组件中又注入了两个属性,分别是事务解析器transactionAttributeSource,和事务拦截器transactionInterceptor
事务解析器 AnnotationTransactionAttributeSource
属性解析器中有一个成员变量annotationParsers,是一个集合,可以添加多种解析器TransactionAnnotationParser,我们点进去这个解析器最后可以定位到下面的代码
这些就是@Transaction注解的属性,所以属性解析器的作用之一就是解析@Transaction注解
事务拦截器 TransactionInterceptor
那么上面的组件要如何联系起来呢?
事务拦截器实现了MethodInterceptor接口,上面提到的InfrastructureAdvisorAutoProxyCreator后置处理器,他会在代理对象执行目标方法的时候,获取其拦截链,这个拦截链就是TransactionInterceptor
最后我们看一下invokeWithinTransaction方法
// 获取属性解析器
TransactionAttributeSource tas = getTransactionAttributeSource();
final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null);
// 获取事务管理器
final PlatformTransactionManager tm = determineTransactionManager(txAttr);
final String joinpointIdentification = methodIdentification(method, targetClass, txAttr);
try {
// This is an around advice: Invoke the next interceptor in the chain.
// This will normally result in a target object being invoked.
retVal = invocation.proceedWithInvocation();
}
catch (Throwable ex) {
// target invocation exception
// 如果事务执行异常就会执行completeTransactionAfterThrowing方法回滚事务
completeTransactionAfterThrowing(txInfo, ex);
throw ex;
}
finally {
cleanupTransactionInfo(txInfo);
}
// 如果事务正常执行就会commit提交事务
commitTransactionAfterReturning(txInfo);
return retVal;
回滚
提交