---
title: "Spring AOP&AutoProxy&Transactions"
author: "adapterofcoms"
date: "2015-06-27T19:50:44Z"
tags: []
categories: ["开源软件"]
slug: "spring-aop-autoproxy-transactions"
draft: false
lastmod: "2019-06-06T10:43:25Z"
id: 2222555
url: "https://adapterofcoms.iteye.com/blog/2222555"
---

AOP  
**Advice**: action taken by an aspect at a particular join point. Different types of advice include "**around**,"  
"**before**" and "**after**" advice. (Advice types are discussed below.) Many AOP frameworks, including  
Spring, model an advice as an interceptor, maintaining **a chain of interceptors around the join point**.  
**Advice types in Spring**:  
Interception **around** advice--&gt;org.aopalliance.intercept.MethodInterceptor  
**Before** advice--&gt;MethodBeforeAdvice  
**Throws** advice--&gt;ThrowsAdvice  
**After Returning** advice--&gt;AfterReturningAdvice  
**Introduction** advice--&gt;IntroductionInterceptor extends MethodInterceptor

**Advice---wrapped---&gt;Advisor\[.getAdvice]**

**IntroductionAdvisor**\[DefaultIntroductionAdvisor , IntroductionInfo]  **PointcutAdvisor**\[DefaultPointcutAdvisor] 

**Note the advisor and the interceptor\[Advice] order in the chain !**

**Customize your specific Advisor/Advice\[BeanDefinition.ROLE\_APPLICATION] not  AutoProxyCreator  !**  
**Extends------&gt;DefaultPointcutAdvisor\[Pointcut-filtering , Advice-MethodInterceptor]**  
**e.g:BeanFactoryTransactionAttributeSourceAdvisor:**  
**Pointcut--matches--&gt;AnnotationTransactionAttributeSource:**  
**AbstractFallbackTransactionAttributeSource how to get @Transactinal attributes.**  
**TransactionInterceptor:MethodInterceptor--invoke--&gt;how to get actual targetClass.**

ProxyFactory factory = new ProxyFactory(myBusinessInterfaceImpl);  
factory.addAdvice(myMethodInterceptor);  
factory.addAdvisor(myAdvisor);  
MyBusinessInterface tb = (MyBusinessInterface) factory.getProxy();

&lt;aop:config&gt;  
&lt;aop:aspectj-autoproxy /&gt;  
当使用&lt;aop:config&gt;配置时自动注册AspectJAwareAdvisorAutoProxyCreator，  
而使用&lt;aop:aspectj-autoproxy&gt;时会自动注册AnnotationAwareAspectJAutoProxyCreator。

**Spring 3.2.13 Transactions:**  
http://www.springframework.org/schema/tx/spring-tx.xsd     
&lt;!--**default order Integer.MAX\_VALUE**:2147483647 order="2147483646"  **BeanFactoryTransactionAttributeSourceAdvisor**--&gt;

**&lt;tx:annotation-driven/&gt;**

**AnnotationDrivenBeanDefinitionParser--&gt;parse--&gt;AopNamespaceUtils.registerAutoProxyCreatorIfNecessary--&gt;AopConfigUtils.registerAutoProxyCreatorIfNecessary--&gt;**

**InfrastructureAdvisorAutoProxyCreator&lt;------****AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean scanning container mgmt beans and findEligibleAdvisors  include**  **BeanFactoryTransactionAttributeSourceAdvisor**

**+**

**@Configuration**

**ProxyTransactionManagementConfiguration**--&gt;**BeanFactoryTransactionAttributeSourceAdvisor**--&gt;

**TransactionInterceptor**&lt;--**TransactionAttributeSource**

**TransactionProxyFactoryBean**&lt;--setTransactionAttributeSource

 

**AnnotationDrivenBeanDefinitionParser.AopAutoProxyConfigurer how to build BeanFactoryTransactionAttributeSourceAdvisor BeanDefinition.ROLE\_INFRASTRUCTURE\[filtering]**

BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(X.class);

GenericBeanDefinition definition = (GenericBeanDefinition) builder.getRawBeanDefinition();

definition.setRole(BeanDefinition.ROLE\_INFRASTRUCTURE);            

ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) AC;

DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();            

defaultListableBeanFactory.registerBeanDefinition("x", definition);  
**For dynamically registered beans, if you want AOP to affect them, they must be registered through the BeanPostProcessor.**

   
**TransactionInterceptor extends TransactionAspectSupport.invokeWithinTransaction--&gt;PlatformTransactionManager around\[ getTransaction, commit, rollback ]**

**AnnotationTransactionAttributeSource**--&gt;getTransactionAttribute/TransactionDefinition

TransactionStatus **getTransaction**(TransactionDefinition:TransactionAttribute definition)

if (**isExistingTransaction**(transaction)) {

return **handleExistingTransaction**(definition, transaction, debugEnabled)--&gt;

//for PROPAGATION\_SUPPORTS or **PROPAGATION\_REQUIRED**

DefaultTransactionStatus:**isNewTransaction**(**false**)&isNewSynchronization--&gt;

**actualNewSynchronization**--&gt;!TransactionSynchronizationManager.isSynchronizationActive()--&gt;(**local synchronizations.get() != null**)

}

//**else**

 DefaultTransactionStatus:isNewTransaction&isNewSynchronization--&gt;true/false:--&gt;**PROPAGATION\_\***

**if TransactionStatus.isNewSynchronization()--&gt;TransactionSynchronization called on commit.**  
**TransactionSynchronization.**  
\[**suspend,resume,beforeCommit/Completion,afterCommit/Completion,flush,STATUS\_COMMITTED/ROLLED\_BACK/UNKNOWN**]  
**TransactionSynchronizationManager**.**registerSynchronization**(TransactionSynchronization)

if **DataSourceTransactionManager** around , similarly refer to org.springframework.jdbc.datasource.DataSourceUtils.ConnectionSynchronization

1.org.mybatis.spring.SqlSessionUtils.**SqlSessionSynchronization**

**commit**(TransactionStatus status)

if TransactionStatus.**isNewSynchronization**()--&gt;**TransactionSynchronization**

if TransactionStatus.**isNewTransaction**()--&gt;**doCommit**

**@Configuration --&gt;**

@ImportResource

org.springframework.config.java.context.JavaConfigApplicationContext

&lt;dependency&gt;

    &lt;groupId&gt;org.springframework.javaconfig&lt;/groupId&gt;

    &lt;artifactId&gt;spring-javaconfig&lt;/artifactId&gt;

&lt;/dependency&gt;

 

**获取Spring动态代理目标对象(不是单独使用JDK Proxy/CGLIB生成的)**  
ref--&gt;http://jinnianshilongnian.iteye.com/blog/1613222