Spring事务的原理是基于AOP实现的,所以流程也可以理解为与AOP一样分为3步:解析切面、织入通知和运行时增强。
1、解析切面
Srping事务的是通过@EnableTransactionManagement注解开启的,该注解往IoC容器中注入了四个对象:InfrastructureAdvisorAutoProxyCreator(类似于AnnotationAwareAspectJAutoProxyCreator,用于创建代理类的)、BeanFactoryTransactionAttributeSourceAdvisor、TransactionAttributeSource、TransactionInterceptor。 其中InfrastructureAdvisorAutoProxyCreator实现了BeanPostProcessor的接口,因此会在第一个Bean实例化之前调用applyBeanPostProcessorsBeforeInstantiation方法,里面会跑到InfrastructureAdvisorAutoProxyCreator的postProcessBeforeInstantiation方法里面:
@Nullable
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
if (result != null) {
return result;
}
}
}
return null;
}
然后会调用AbstractAutoProxyCreator的postProcessBeforeInstantiation方法对切面进行初始化,其流程和AOP初始化流程一致(这里要注意事务的切面是AnnotationAwareAspectJAutoProxyCreator调用findCandidateAdvisors跑到父类AbstractAdvisorAutoProxyCreator的findCandidateAdvisors方法里面调用BeanFactoryAdvisorRetrievalHelper的findAdvisorBeans方法加载BeanFactoryTransactionAttributeSourceAdvisor缓存起来的)被切面类顺便加载
public List<Advisor> findAdvisorBeans() {
String[] advisorNames = this.cachedAdvisorBeanNames;
if (advisorNames == null) {
advisorNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
this.beanFactory, Advisor.class, true, false);
this.cachedAdvisorBeanNames = advisorNames;
}
if (advisorNames.length == 0) {
return new ArrayList<>();
}
List<Advisor> advisors = new ArrayList<>();
for (String name : advisorNames) {
if (isEligibleBean(name)) {
if (this.beanFactory.isCurrentlyInCreation(name)) {
if (logger.isTraceEnabled()) {
logger.trace("Skipping currently created advisor '" + name + "'");
}
}
else {
try {
advisors.add(this.beanFactory.getBean(name, Advisor.class));
}
catch (BeanCreationException ex) {
Throwable rootCause = ex.getMostSpecificCause();
if (rootCause instanceof BeanCurrentlyInCreationException) {
BeanCreationException bce = (BeanCreationException) rootCause;
String bceBeanName = bce.getBeanName();
if (bceBeanName != null && this.beanFactory.isCurrentlyInCreation(bceBeanName)) {
if (logger.isTraceEnabled()) {
logger.trace("Skipping advisor '" + name +
"' with dependency on currently created bean: " + ex.getMessage());
}
continue;
}
}
throw ex;
}
}
}
}
return advisors;
}
2、织入
根据AOP的原理,在创建代理对象的时候,会将BeanFactoryTransactionAttributeSourceAdvisor织入到代理对象中,真正织入的通知则是封装在BeanFactoryTransactionAttributeSourceAdvisor里面的TransactionInterceptor
@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;
}
3、运行时增强
实际上是调用TransactionInterceptor的invoke方法,TransactionInterceptor的invoke方法会开启事务
@Override
@Nullable
public Object invoke(MethodInvocation invocation) throws Throwable {
Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);
return invokeWithinTransaction(invocation.getMethod(), targetClass, invocation::proceed);
}
|