spring源碼學習系列1-spring事務代理深入分析

本文主要分析spring事務代理的源碼,跟蹤代碼,瞭解事務代理的生命週期


1.學習資料和書籍

2.代理的生成(原理)

3.代理的使用-使用中遇到的常見問題


涉及的源碼類:
org.springframework.aop.config.AopNamespaceUtils

org.springframework.aop.config.AopConfigUtils

org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator

org.springframework.aop.framework.ProxyConfig

org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator

org.springframework.aop.support.AopUtils

org.springframework.aop.framework.ProxyFactory

org.springframework.transaction.config.TxNamespaceHandler

org.springframework.transaction.config.AnnotationDrivenBeanDefinitionParser

org.springframework.transaction.annotation.AnnotationTransactionAttributeSource

org.springframework.transaction.interceptor.TransactionInterceptor

org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction

org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor


1.學習資料和書籍
grepcode源碼及相關博客


2.代理的生成

涉及事務代理的spring元素及特性:


xml配置:
[size=large]2.1 <tx:annotation-driven transaction-manager="transactionManager" />[/size]

當XML中存在“<tx:annotation-driven/>”時,命名空間爲tx。從相關的jar包(如spring-tx.jar)中可查看標籤處理類。從配置文件spring.handlers中查找到的NamespaceHandler爲org.springframework.transaction.config.TxNamespaceHandler
。由TxNamespaceHandler負責具體的解析tx命名空間
部分代碼如下:

static final String TRANSACTION_MANAGER_ATTRIBUTE = "transaction-manager";
static final String DEFAULT_TRANSACTION_MANAGER_BEAN_NAME = "transactionManager";
public void init() {
registerBeanDefinitionParser("advice", new TxAdviceBeanDefinitionParser());
registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenBeanDefinitionParser());
registerBeanDefinitionParser("jta-transaction-manager", new JtaTransactionManagerBeanDefinitionParser());
}


解析annotation-driven的類爲org.springframework.transaction.config.AnnotationDrivenBeanDefinitionParser,源碼可查看如下:

[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.transaction/3.2.2/org/springframework/transaction/config/AnnotationDrivenBeanDefinitionParser.java?av=f[/url]

部分源碼如下:

public static final String TRANSACTION_ADVISOR_BEAN_NAME =
"org.springframework.transaction.config.internalTransactionAdvisor";



AnnotationDrivenBeanDefinitionParser#parse
public BeanDefinition parse(Element element, ParserContext parserContext) {
String mode = element.getAttribute("mode");
if ("aspectj".equals(mode)) {
// mode="aspectj"
registerTransactionAspect(element, parserContext);
}
else {
// mode="proxy"
AopAutoProxyConfigurer.configureAutoProxyCreator(element, parserContext);
}
return null;
}


查看spring-tx-3.0.xsd可知,mode=proxy。內部類AopAutoProxyConfigurer爲實際代理模式下引入aop框架,部分代碼如下:


[size=large]AopAutoProxyConfigurer.configureAutoProxyCreator方法分析:事務定義的入口[/size]
AnnotationDrivenBeanDefinitionParser.AopAutoProxyConfigurer#configureAutoProxyCreator
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.
RootBeanDefinition sourceDef = new RootBeanDefinition(AnnotationTransactionAttributeSource.class);
sourceDef.setSource(eleSource);
sourceDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
String sourceName = parserContext.getReaderContext().registerWithGeneratedName(sourceDef);

// Create the TransactionInterceptor definition.
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.
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);
}
}



[b][size=medium]創建InfrastructureAdvisorAutoProxyCreator的beanDefinition[/size][/b]
a.定義後置處理器(InfrastructureAdvisorAutoProxyCreator)-[b][color=green]根據配置信息,定義生成代理的入口[/color][/b]

AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(parserContext, element);  

創建事務代理的後置處理器InfrastructureAdvisorAutoProxyCreator
[img]http://dl2.iteye.com/upload/attachment/0123/9164/e1b6144b-fc73-372a-913d-602ddb64e882.png[/img]


[img]http://dl2.iteye.com/upload/attachment/0123/8459/9742b9ad-1466-3979-9c4e-5d6d6a1826d5.png[/img]

----------------------代碼直譯start----------------------
第一行:創建名爲AUTO_PROXY_CREATOR_BEAN_NAME=org.springframework.aop.config.internalAutoProxyCreator的後置處理器InfrastructureAdvisorAutoProxyCreator([color=red]實現了BeanPostProcessor[/color]),且名爲AUTO_PROXY_CREATOR_BEAN_NAME的後置處理器只有一個

尚未註冊,第三行註冊


第二行:給AUTO_PROXY_CREATOR_BEAN_NAME設置屬性
解析
PROXY_TARGET_CLASS_ATTRIBUTE = "proxy-target-class"
EXPOSE_PROXY_ATTRIBUTE = "expose-proxy"
設置後置處理器相關屬性值
proxyTargetClass=true或
exposeProxy=true
(這2個屬性在org.springframework.aop.framework.ProxyConfig中)

第三行:註冊到容器
----------------------代碼直譯end----------------------


InfrastructureAdvisorAutoProxyCreator源碼:
[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.aop/3.2.2/org/springframework/aop/framework/autoproxy/InfrastructureAdvisorAutoProxyCreator.java#InfrastructureAdvisorAutoProxyCreator[/url]

AbstractAdvisorAutoProxyCreator源碼:
[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.aop/3.2.2/org/springframework/aop/framework/autoproxy/AbstractAdvisorAutoProxyCreator.java#AbstractAdvisorAutoProxyCreator[/url]

AbstractAutoProxyCreator源碼:
[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.aop/3.2.2/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java#AbstractAutoProxyCreator[/url]

[b][size=medium]執行InfrastructureAdvisorAutoProxyCreator[/size][/b]
a.1 AbstractApplicationContext.refresh-[b][color=green]執行生成代理的入口,調用以上定義的入口生成代理[/color][/b]

單一職責原則:[color=red]AbstractAutoProxyCreator[/color].postProcessAfterInitialization(Object bean, String beanName)(有圖可知AbstractAutoProxyCreator爲InfrastructureAdvisorAutoProxyCreator祖先類,祖先類中定義了模板方法),繼續調用wrapIfNecessary方法

[color=red]在實例化bean後,調用初始化方法後執行[/color]

該方法中:
[color=red](1)獲取攔截器主要在後置處理器中完成 (2)創建代理委託給ProxyFactory完成[/color]

(1) 首先查找所有符合條件的Advisor類型的類(抽象方法:getAdvicesAndAdvisorsForBean)。
該任務委託由AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean完成(有圖可知[color=red]AbstractAdvisorAutoProxyCreator[/color]爲AbstractAutoProxyCreator的子類,InfrastructureAdvisorAutoProxyCreator的父類),繼續調用findEligibleAdvisors,在該方法中,

(1.1)首先調用findCandidateAdvisors()方法,查找所有advisor類型的類
[size=medium][color=red]abstractAutoProxyCreator[/color].postProcessAfterInitialization->[color=red]abstractAutoProxyCreator[/color].wrapIfNecessary->[color=red]abstractAdvisorAutoProxyCreator[/color].getAdvicesAndAdvisorsForBean->[color=red]abstractAdvisorAutoProxyCreator[/color].findEligibleAdvisors->[color=red]abstractAdvisorAutoProxyCreator[/color].findCandidateAdvisors->[color=red]advisorRetrievalHelper[/color].findAdvisorBeans()->[color=red]BeanFactoryUtils[/color].beanNamesForTypeIncludingAncestors->[color=red]beanFactory[/color].getBean(name, Advisor.class)[/size]


[size=medium][color=red]advisorRetrievalHelper[/color].findAdvisorBeans()[/size]方法中,循環advisorNames

BeanFactoryAdvisorRetrievalHelper源碼:
[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.aop/3.2.2/org/springframework/aop/framework/autoproxy/BeanFactoryAdvisorRetrievalHelper.java#BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans%28%29[/url]

(1.2)其次調用findAdvisorsThatCanApply()方法,查找所有上一步中符合條件的advisor。

[size=medium][color=red]abstractAutoProxyCreator[/color].postProcessAfterInitialization->[color=red]abstractAutoProxyCreator[/color].wrapIfNecessary->[color=red]abstractAdvisorAutoProxyCreator[/color].getAdvicesAndAdvisorsForBean->[color=red]abstractAdvisorAutoProxyCreator[/color].findEligibleAdvisors->[color=red]abstractAdvisorAutoProxyCreator[/color].findAdvisorsThatCanApply->[color=red]AopUtils[/color].findAdvisorsThatCanApply->[color=red]advisor[/color].getPointCut->[color=red]pointcut[/color].matches->[color=red]tas[/color].getTransactionAttribute 最終返回符合條件的advisor[/size]

[size=medium][color=red]AopUtils[/color].findAdvisorsThatCanApply(candidateAdvisors, beanClass)[/size]方法,
該方法中循環candidateAdvisors,調用canApply(Advisor advisor, Class<?> targetClass, boolean hasIntroductions),根據advisor獲取pointcut,繼續調用canApply(Pointcut pc, Class<?> targetClass, boolean hasIntroductions),通過pc.getMethodMatcher()獲取methodMatcher,這個類是真正判斷該類或方法適合符合事物,即該類或方法是否有@Transaction註釋

如果bean不適合該advisor,即沒有@Transaction註釋,則返回false,並將該bean放入this.advisedBeans.put(cacheKey, Boolean.FALSE),並最終返回bean

否則創建代理,(1)最終過濾符合條件的攔截器specificInterceptors


對於BeanFactoryTransactionAttributeSourceAdvisor持有對象TransactionAttributeSourcePointcut,具體分析見(d)


(2) 創建代理AopProxy,並通過aopProxy.getProxy返回最終代理bean。

[img]http://dl2.iteye.com/upload/attachment/0123/9283/a36fc802-1a2e-3c78-8f1a-ba040c31c17c.png[/img]

[size=medium][color=red]abstractAutoProxyCreator[/color].postProcessAfterInitialization->[color=red]abstractAutoProxyCreator[/color].wrapIfNecessary->[color=red]abstractAutoProxyCreator[/color].createProxy->[color=red]proxyFactory[/color].getProxy->[color=red]proxyFactory[/color].createAopProxy().getProxy->[color=red]jdkDynamicAopProxy[/color].getProxy或cglibProxy.getProxy->[color=red]java.lang.reflect.Proxy[/color].newProxyInstance(classLoader, proxiedInterfaces, this): 最終返回proxy[/size]

this即爲aopProxy(jdkDynamicAopProxy或cglibProxy)其實現了InvocationHandler接口

[color=red]proxy何時調用?[/color]
用戶業務對象的代理調用時,最終調用invocationHandler.invoke方法

通過jdkDynamicAopProxy源碼瞭解其invoke方法
[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.aop/3.2.2/org/springframework/aop/framework/JdkDynamicAopProxy.java#JdkDynamicAopProxy[/url]


[size=medium][color=red]abstractAutoProxyCreator[/color].createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean))[/size],在該方法中創建[color=red]ProxyFactory[/color],並設置屬性

ProxyFactory源碼:
[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.aop/3.2.2/org/springframework/aop/framework/ProxyFactory.java#ProxyFactory.getProxy%28java.lang.ClassLoader%29[/url]

ProxyFactory proxyFactory = new ProxyFactory();
// Copy our properties (proxyTargetClass etc) inherited from ProxyConfig.
proxyFactory.copyFrom(this);

if (!shouldProxyTargetClass(beanClass, beanName)) {
// Must allow for introductions; can't just set interfaces to
// the target's interfaces only.
Class<?>[] targetInterfaces = ClassUtils.getAllInterfacesForClass(beanClass, this.proxyClassLoader);
for (Class<?> targetInterface : targetInterfaces) {
proxyFactory.addInterface(targetInterface);
}
}

Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
for (Advisor advisor : advisors) {
proxyFactory.addAdvisor(advisor);
}

proxyFactory.setTargetSource(targetSource);
customizeProxyFactory(proxyFactory);

proxyFactory.setFrozen(this.freezeProxy);
if (advisorsPreFiltered()) {
proxyFactory.setPreFiltered(true);
}

return proxyFactory.getProxy(this.proxyClassLoader);

最後一句proxyFactory.getProxy(this.proxyClassLoader);此處創建代理。

public Object getProxy(ClassLoader classLoader) {
return createAopProxy().getProxy(classLoader);
}


該方法中createAopProxy()返回AopProxy如下:
[color=red]proxyCreatorSupport[/color].createAopProxy()創建代理
protected final synchronized AopProxy createAopProxy() {
if (!this.active) {
activate();
}
return getAopProxyFactory().createAopProxy(this);
}


getAopProxyFactory()方法中獲取ProxyCreatorSupport的內置對象aopProxyFactory([color=red]new DefaultAopProxyFactory()[/color])。
DefaultAopProxyFactory源碼:
[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.aop/3.2.2/org/springframework/aop/framework/DefaultAopProxyFactory.java#DefaultAopProxyFactory[/url]

getAopProxyFactory().createAopProxy(this)該方法返回AopProxy
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
if (targetClass.isInterface()) {
return new JdkDynamicAopProxy(config);
}
return CglibProxyFactory.createCglibProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}



[img]http://dl2.iteye.com/upload/attachment/0123/9292/22447235-2511-3553-ab46-c039b48652f9.png[/img]

createAopProxy().getProxy(classLoader);以jdk生成代理爲例
jdkDynamicAopProxy.getProxy(classLoader)
public Object getProxy(ClassLoader classLoader) {
if (logger.isDebugEnabled()) {
logger.debug("Creating JDK dynamic proxy: target source is " + this.advised.getTargetSource());
}
Class[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised);
findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
}


最終返回代理對象


JdkDynamicAopProxy源碼:
[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.aop/3.2.2/org/springframework/aop/framework/JdkDynamicAopProxy.java#JdkDynamicAopProxy[/url]

[color=red]此後置處理器處理@Transaction註釋的bean,創建aopProxy。其他advisor如何處理,事務代理的外面再加一層代理?[/color]
嘗試解釋:
該後置處理器可以處理多個advisor(包括事務advisor,即BeanFactoryTransactionAttributeSourceAdvisor)
[color=red]自定義advisor,實現相應接口,看否是可以識別?[/color]
[color=red]自定義beanpostprocess,在此前代理上創建代理或者覆蓋原先代理對象?[/color]


ProxyCreatorSupport源碼:
[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.aop/3.2.2/org/springframework/aop/framework/ProxyCreatorSupport.java#ProxyCreatorSupport.createAopProxy%28%29[/url]


[size=large]斷點2[/size]


[b][size=medium]創建AnnotationTransactionAttributeSource的beanDefinition[/size][/b]
b.定義AnnotationTransactionAttributeSource-[b][color=green]事務屬性,每個業務類或方法可能不同,大部分是一樣的[/color][/b]


// Create the TransactionAttributeSource definition.
RootBeanDefinition sourceDef = new RootBeanDefinition(AnnotationTransactionAttributeSource.class);
sourceDef.setSource(eleSource);
sourceDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
String sourceName = parserContext.getReaderContext().registerWithGeneratedName(sourceDef);

創建[color=red]AnnotationTransactionAttribute[/color]Source定義(事務屬性來源於註解@Transactional)

單一職責原則:getTransactionAttribute(Method method, Class<?> targetClass)

判斷方法或類是否有@Transactional註釋,並解析返回TransactionAttribute或null。其內部調用SpringTransactionAnnotationParser來解析是否有@Transactional註解

AnnotationTransactionAttributeSource源碼見:
[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.transaction/3.2.2/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.java?av=f[/url]

[color=red]在哪[/color]調用其getTransactionAttribute(Method method, Class<?> targetClass)方法,以獲取事務屬性?
(1) 在後置處理器,avvisor->pointcut.matches->getTransactionAttribute,用於判斷advisor適合適合此bean

(2) 在TransactionInterceptor的invoke方法中調用,更確切的說是在TransactionAspectSupport的invokeWithinTransaction方法中。用於事務定義TransactionInfo

[color=red]用於判斷該方法或類是否可開啓事務。[/color]

還有其他的事務屬性來源,應用於不同的事務配置方式如:NameMatchTransactionAttributeSource


[b][size=medium]創建TransactionInterceptor的beanDefinition[/size][/b]
c.定義TransactionInterceptor-[b][color=green]事務攔截器,執行事務開始,提交,回滾等操作[/color][/b]

[img]http://dl2.iteye.com/upload/attachment/0123/9311/ebb15fcb-47cb-3588-b411-fbfd0956cdfb.png[/img]



// Create the TransactionInterceptor definition.
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);

創建TransactionInterceptor定義

單一職責原則:invoke(final MethodInvocation invocation)

invoke中調用TransactionAspectSupport的invokeWithinTransaction(Method method, Class targetClass, final InvocationCallback invocation)方法,執行真正的攔截邏輯。在該方法中會調用transactionAttributeSource判斷類或方法是否可開啓事務,調用transactionManager執行攔截邏輯,即開啓提交事務等。

registerTransactionManager(element, interceptorDef);
這行代碼,主要是設置TransactionAspectSupport的transactionManager(註冊事務管理器)。調用invoke時,設置到生成的TransactionInfo中並管理事務狀態。

TransactionInterceptor(攔截器)本身不保存數據,只是起到傳遞的作用,把真正的處理過程交給TransactionAspectSupport 去完成

[color=red]在哪調用TransactionInterceptor.invoke(final MethodInvocation invocation)?[/color]
在(a)處後置處理器中,生成代理(jdk或cglib代理),invoke方法中調用

transactionInterceptor是advice,是否包裝成advisor何時包裝?
創建代理時(a(2))封裝成advisor:
abstractAutoProxyCreator.buildAdvisors->advisorAdapterRegistry.wrap

調用代理方法時,執行的是methodInterceptor,advisor何時拆解成methodInterceptor?
執行代理方法invoke時,通過advised.getInterceptorsAndDynamicInterceptionAdvice獲取methodInterceptor,其中會調用advisorAdapterRegistry.getInterceptors(advisor);


TransactionAspectSupport源碼可見:
[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.transaction/3.2.2/org/springframework/transaction/interceptor/TransactionAspectSupport.java#TransactionAspectSupport.TransactionInfo.bindToThread%28%29[/url]

joinpointIdentification

TransactionInterceptor源碼見:
[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.transaction/3.2.2/org/springframework/transaction/interceptor/TransactionInterceptor.java#TransactionInterceptor[/url]


[b][size=medium]創建BeanFactoryTransactionAttributeSourceAdvisor的beanDefinition[/size][/b]
d.定義BeanFactoryTransactionAttributeSourceAdvisor-[b][color=green]事務集成到業務bean,連接中介類[/color][/b]

[img]http://dl2.iteye.com/upload/attachment/0123/9287/a9f6d1c0-b319-3120-9cf8-e95e300fa823.png[/img]



// Create the TransactionAttributeSourceAdvisor definition.
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);

創建TransactionAttributeSourceAdvisor定義
BeanFactoryTransactionAttributeSourceAdvisor源碼:
[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.transaction/3.2.2/org/springframework/transaction/interceptor/BeanFactoryTransactionAttributeSourceAdvisor.java?av=f[/url]


BeanFactoryTransactionAttributeSourceAdvisor是一個標識類,使其能夠被後置處理器(InfrastructureAdvisorAutoProxyCreator見(a)出分析)識別。
具體分析見:
http://www.cnblogs.com/youzhibing/p/6414780.html


持有對象TransactionAttributeSourcePointcut

[img]http://dl2.iteye.com/upload/attachment/0123/9301/ef7d446c-7ca8-3545-b6ec-411e16afd54e.png[/img]

public boolean matches(Method method, Class targetClass) {
TransactionAttributeSource tas = getTransactionAttributeSource();
return (tas == null || tas.getTransactionAttribute(method, targetClass) != null);
}


後置處理器中調用此方法,判斷bean或method是否適合此advisor


TransactionAttributeSourcePointcut源碼見
[url]http://grepcode.com/file/repository.springsource.com/org.springframework/org.springframework.transaction/3.1.2/org/springframework/transaction/interceptor/TransactionAttributeSourcePointcut.java#TransactionAttributeSourcePointcut[/url]


[b][size=medium]創建CompositeComponentDefinition的beanDefinition[/size][/b]
e.定義組件之間的關係bean

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);


該類的具體作用是什麼?
定義事務處理之間的關係?

[size=large]斷點4:[/size]


總結:
條件語句裏面創建了3個bean定義(TransactionAttributeSource ,TransactionInterceptor,TransactionAttributeSourceAdvisor ),將3個類組合嵌入compositeDef中,代表整個<tx:annotation-driven transaction-manager="transactionManager" />

還創建了一個後置處理器,bean實例化後,創建事務代理

創建3個定義或者後置處理器的過程中,多處可以繼續跟蹤代碼,如如何創建aopProxy代理,transactioninterceptor中的invoke方法是如何執行的等等。限於篇幅,這裏進一步跟蹤,後續補上


[size=large]2.2 @Transactionl[/size]
事務屬性來源,主要在TransactionAttributeSource中判斷bean或者method中是否有註解


3.代理的使用-使用中遇到的常見問題


參考:
https://www.ibm.com/developerworks/cn/education/opensource/os-cn-spring-trans/

http://www.myexception.cn/open-source/1942796.html

http://www.codeceo.com/article/software-outsourcing-and-lover.html

http://blog.csdn.net/qq418517226/article/details/51282035

http://jinnianshilongnian.iteye.com/blog/1901694

http://www.cnblogs.com/wade-luffy/p/6080183.html Spring事務解析2-標籤解析

http://jinnianshilongnian.iteye.com/blog/1508018 TransactionAttributeSource

http://lgbolgger.iteye.com/blog/2180251 Spring事務源碼分析(一)Spring事務入門
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章