`
adapterofcoms
  • 浏览: 72287 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Spring AOP&AutoProxy&Transactions

阅读更多

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-->org.aopalliance.intercept.MethodInterceptor
Before advice-->MethodBeforeAdvice
Throws advice-->ThrowsAdvice
After Returning advice-->AfterReturningAdvice
Introduction advice-->IntroductionInterceptor extends MethodInterceptor


Advice---wrapped--->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_APPLICATIONnot  AutoProxyCreator  !
Extends------>
DefaultPointcutAdvisor[Pointcut-filtering , Advice-MethodInterceptor]
e.g:BeanFactoryTransactionAttributeSourceAdvisor:
Pointcut--matches-->AnnotationTransactionAttributeSource:
AbstractFallbackTransactionAttributeSource how to get @Transactinal attributes.
TransactionInterceptor:MethodInterceptor--invoke-->how to get actual targetClass.


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

<aop:config>
<aop:aspectj-autoproxy />
当使用<aop:config>配置时自动注册AspectJAwareAdvisorAutoProxyCreator,
而使用<aop:aspectj-autoproxy>时会自动注册AnnotationAwareAspectJAutoProxyCreator。

Spring 3.2.13 Transactions:
http://www.springframework.org/schema/tx/spring-tx.xsd   
<!--default order Integer.MAX_VALUE:2147483647 order="2147483646"  BeanFactoryTransactionAttributeSourceAdvisor-->

<tx:annotation-driven/>

AnnotationDrivenBeanDefinitionParser-->parse-->AopNamespaceUtils.registerAutoProxyCreatorIfNecessary-->AopConfigUtils.registerAutoProxyCreatorIfNecessary-->

InfrastructureAdvisorAutoProxyCreator<------AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean scanning container mgmt beans and findEligibleAdvisors  include  BeanFactoryTransactionAttributeSourceAdvisor

+

@Configuration

ProxyTransactionManagementConfiguration-->BeanFactoryTransactionAttributeSourceAdvisor-->

TransactionInterceptor<--TransactionAttributeSource

TransactionProxyFactoryBean<--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-->PlatformTransactionManager around[ getTransaction, commit, rollback ]

AnnotationTransactionAttributeSource-->getTransactionAttribute/TransactionDefinition

TransactionStatus getTransaction(TransactionDefinition:TransactionAttribute definition)

if (isExistingTransaction(transaction)) {

return handleExistingTransaction(definition, transaction, debugEnabled)-->

//for PROPAGATION_SUPPORTS or PROPAGATION_REQUIRED

DefaultTransactionStatus:isNewTransaction(false)&isNewSynchronization-->

actualNewSynchronization-->!TransactionSynchronizationManager.isSynchronizationActive()-->(local synchronizations.get() != null)

}

//else

 DefaultTransactionStatus:isNewTransaction&isNewSynchronization-->true/false:-->PROPAGATION_*


if TransactionStatus.isNewSynchronization()-->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()-->TransactionSynchronization

if TransactionStatus.isNewTransaction()-->doCommit



@Configuration -->

@ImportResource

org.springframework.config.java.context.JavaConfigApplicationContext

<dependency>

    <groupId>org.springframework.javaconfig</groupId>

    <artifactId>spring-javaconfig</artifactId>

</dependency>

 

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics