這一章我們來(lái)看看事務(wù)的具體參數(shù)在項(xiàng)目啟動(dòng)后,是在哪個(gè)入口加載和解析的
入口
TxNamespaceHandler類,TxNamespaceHandler類是spring-tx包的自定義解析器,在它的init方法中,有一個(gè)AnnotationDrivenBeanDefinitionParser類,
@Override
public void init() {
registerBeanDefinitionParser("advice", new TxAdviceBeanDefinitionParser());
//<tx:annotation-driven開(kāi)頭的配置聂使, Spring會(huì)使用AnnotationDrivenBeanDefinitionParser類的 parse方法進(jìn)行解析
registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenBeanDefinitionParser());
registerBeanDefinitionParser("jta-transaction-manager", new JtaTransactionManagerBeanDefinitionParser());
}
再看AnnotationDrivenBeanDefinitionParser類中的parse方法
@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
registerTransactionalEventListenerFactory(parserContext);
String mode = element.getAttribute("mode");
if ("aspectj".equals(mode)) {
//類型為"aspectj"
registerTransactionAspect(element, parserContext);
if (ClassUtils.isPresent("javax.transaction.Transactional", getClass().getClassLoader())) {
registerJtaTransactionAspect(element, parserContext);
}
}
else {
//類型為"proxy" 默認(rèn)
AopAutoProxyConfigurer.configureAutoProxyCreator(element, parserContext);
}
return null;
}
private static class AopAutoProxyConfigurer {
public static void configureAutoProxyCreator(Element element, ParserContext parserContext) {
AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(parserContext, element);
String txAdvisorBeanName = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME;
if (!parserContext.getRegistry().containsBeanDefinition(txAdvisorBeanName)) {
Object eleSource = parserContext.extractSource(element);
// Create the TransactionAttributeSource definition.
//創(chuàng)建TransactionAttributeSource的bean
RootBeanDefinition sourceDef = new RootBeanDefinition(
"org.springframework.transaction.annotation.AnnotationTransactionAttributeSource");
sourceDef.setSource(eleSource);
sourceDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
String sourceName = parserContext.getReaderContext().registerWithGeneratedName(sourceDef);
// Create the TransactionInterceptor definition.
//創(chuàng)建TransactionInterceptor的bean 后面使用TransactionInterceptor作為事務(wù)增強(qiáng)器實(shí)現(xiàn)事務(wù)的邏輯
RootBeanDefinition interceptorDef = new RootBeanDefinition(TransactionInterceptor.class);
interceptorDef.setSource(eleSource);
interceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
registerTransactionManager(element, interceptorDef);
interceptorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
String interceptorName = parserContext.getReaderContext().registerWithGeneratedName(interceptorDef);
// Create the TransactionAttributeSourceAdvisor definition.
//創(chuàng)建TransactionAttributeSourceAdvisor的bean
RootBeanDefinition advisorDef = new RootBeanDefinition(BeanFactoryTransactionAttributeSourceAdvisor.class);
advisorDef.setSource(eleSource);
advisorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
advisorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
advisorDef.getPropertyValues().add("adviceBeanName", interceptorName);
if (element.hasAttribute("order")) {
advisorDef.getPropertyValues().add("order", element.getAttribute("order"));
}
parserContext.getRegistry().registerBeanDefinition(txAdvisorBeanName, advisorDef);
CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), eleSource);
compositeDef.addNestedComponent(new BeanComponentDefinition(sourceDef, sourceName));
compositeDef.addNestedComponent(new BeanComponentDefinition(interceptorDef, interceptorName));
compositeDef.addNestedComponent(new BeanComponentDefinition(advisorDef, txAdvisorBeanName));
parserContext.registerComponent(compositeDef);
}
}
}
一步步往下走,在configureAutoProxyCreator方法中我們目前只關(guān)注registerAutoProxyCreatorIfNecessary方法
public static void registerAutoProxyCreatorIfNecessary(
ParserContext parserContext, Element sourceElement) {
BeanDefinition beanDefinition = AopConfigUtils.registerAutoProxyCreatorIfNecessary(
parserContext.getRegistry(), parserContext.extractSource(sourceElement));
useClassProxyingIfNecessary(parserContext.getRegistry(), sourceElement);
registerComponentIfNecessary(beanDefinition, parserContext);
}
然后在AopConfigUtils.registerAutoProxyCreatorIfNecessary方法中
@Nullable
public static BeanDefinition registerAutoProxyCreatorIfNecessary(
BeanDefinitionRegistry registry, @Nullable Object source) {
return registerOrEscalateApcAsRequired(InfrastructureAdvisorAutoProxyCreator.class, registry, source);
}
那么在registerAutoProxyCreatorIfNecessary方法中辆苔,我們能看到的是注冊(cè)了一個(gè)InfrastructureAdvisorAutoProxyCreator類型的bean而已菲驴,那么屬于事務(wù)關(guān)鍵流程的又是哪一步呢?
其實(shí)上面這一串代碼流程走下來(lái),主要往正常的bean注冊(cè)的流程靠近,InfrastructureAdvisorAutoProxyCreator類繼承了AbstractAdvisorAutoProxyCreator類,而AbstractAdvisorAutoProxyCreator類又繼承了AbstractAutoProxyCreator類统诺,而AbstractAutoProxyCreator類就是bean實(shí)例化的重要類,其中的postProcessAfterInitialization方法才是接下來(lái)流程的關(guān)鍵
@Override
public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
if (bean != null) {
// 根據(jù)給定的 bean的class和name 構(gòu)建一個(gè) key
Object cacheKey = getCacheKey(bean.getClass(), beanName);
if (this.earlyProxyReferences.remove(cacheKey) != bean) {
// 如果它適合被代理 則要封裝指定 bean
return wrapIfNecessary(bean, beanName, cacheKey);
}
}
return bean;
}