1. 注册BeanFactoryTransactionAttributeSourceAdvisor
@EnableTransactionManagement --> @Import(TransactionManagementConfigurationSelector.class) --> 注册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.setAdvice(transactionInterceptor()); ...return advisor; } ... @Bean @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public TransactionInterceptor transactionInterceptor() { TransactionInterceptor interceptor = new TransactionInterceptor(); ...return interceptor; } }
2. TransactionInterceptor
public class TransactionInterceptor extends TransactionAspectSupport implements MethodInterceptor, Serializable { ... @Override public Object invoke(final MethodInvocation invocation) throws Throwable { Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null); return invokeWithinTransaction(invocation.getMethod(), targetClass, new InvocationCallback() { @Override public Object proceedWithInvocation() throws Throwable { return invocation.proceed(); } }); } ... }
public abstract class TransactionAspectSupport implements BeanFactoryAware, InitializingBean { ... protected Object invokeWithinTransaction(Method method, Class<?> targetClass, final InvocationCallback invocation) throws Throwable { final TransactionAttribute txAttr = getTransactionAttributeSource().getTransactionAttribute(method, targetClass); // @Transactional final PlatformTransactionManager tm = determineTransactionManager(txAttr); // 获取事务管理器 ... if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) { TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification); // 创建事务 Object retVal = null; try { retVal = invocation.proceedWithInvocation(); // 执行切面 } catch (Throwable ex) { completeTransactionAfterThrowing(txInfo, ex); // 异常处理 throw ex; } finally { cleanupTransactionInfo(txInfo); // 清理事务信息(事务嵌套) } commitTransactionAfterReturning(txInfo); // 提交事务 return retVal; } else { ... } } protected PlatformTransactionManager determineTransactionManager(TransactionAttribute txAttr) { ... String qualifier = txAttr.getQualifier(); if (StringUtils.hasText(qualifier)) { // @Qualifier ... } else if (StringUtils.hasText(this.transactionManagerBeanName)) { ... } else { PlatformTransactionManager defaultTransactionManager = getTransactionManager(); if (defaultTransactionManager == null) { defaultTransactionManager = this.transactionManagerCache.get(DEFAULT_TRANSACTION_MANAGER_KEY); // 缓存look-up if (defaultTransactionManager == null) { defaultTransactionManager = this.beanFactory.getBean(PlatformTransactionManager.class); // 容器look-up this.transactionManagerCache.putIfAbsent(DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager); // 缓存 } } return defaultTransactionManager; } } ... }