spring 事务背后的故事

一、spring 事务配置

声明式事务有两种方式,一种是在配置文件(xml)中做相关的事务规则声明,另一种是基于 @Transactional 注解的方式。

1.1 xml 声明 事务

1.1.1 单一的对于目标类进行代理

<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
     abstract="true">
   <property name="transactionManager" ref="transactionManager"/>
   <property name="transactionAttributes">
     <props>
       <prop key="insert*">PROPAGATION_REQUIREDprop>
       <prop key="update*">PROPAGATION_REQUIREDprop>
       <prop key="*">PROPAGATION_REQUIRED,readOnlyprop>
     props>
   property>
 bean>

 <bean id="myProxy" parent="baseTransactionProxy">
   <property name="target" ref="myTarget"/>
 bean>

 <bean id="yourProxy" parent="baseTransactionProxy">
   <property name="target" ref="yourTarget"/>
 bean>

1.1.2 自动代理时代

单一的针对bean 配置非常的麻烦, Auto proxy creator 自动诞生!

BeanNameAutoProxyCreator 根据配置的bean 名称进行自动代理

org.springframework.aop.framework.autoproxy. BeanNameAutoProxyCreator

<bean id="transactionInterceptor"
          class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager" ref="transactionManager"/>
        
        <property name="transactionAttributes">
            <props>
                <prop key="*">PROPAGATION_REQUIRED,readOnlyprop>
            props>
        property>
    bean>

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="beanNames">
        <list>
            <value>*Service*value>
        list>
    property>
    <property name="interceptorNames">
        <list>
            <value>transactionInterceptorvalue>
        list>
    property>
bean>

通用的AOP基于代理的解决方案(BeanPostProcessor)


org.springframework.aop.framework.autoproxy. DefaultAdvisorAutoProxyCreator
BeanPostProcessor implementation that creates AOP proxies based on all candidate Advisors in the current BeanFactory. This class is completely generic; it contains no special code to handle any particular aspects, such as pooling aspects.
AOP=Advisors(AOP 具体做的事情)+PointCut(基于方法名称?基于注解?基于表达式?)

 <bean id="transactionInterceptor"
          class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager" ref="transactionManager"/>
        
        <property name="transactionAttributes">
            <props>
                <prop key="*">PROPAGATION_REQUIRED,readOnlyprop>
            props>
        property>
    bean>
<bean id="transactionAttributeSourceAdvisor"
        class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
    
    <property name="transactionInterceptor" ref="transactionInterceptor"/>
bean>

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
    
    <property name="advisorBeanNamePrefix" value="transactionAttributeSource"/>
bean>

处理 aop:config

org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator
aop:config标签

<tx:advice id="transactionInterceptorAdvice">
    
    <tx:attributes>
        <tx:method name="*" rollback-for="Exception"/>
        <tx:method name="find*" rollback-for="Exception" read-only="true"/>
        <tx:method name="get*" rollback-for="Exception" read-only="true"/>
    tx:attributes>
tx:advice>
<aop:config proxy-target-class="true">
    
    
    <aop:pointcut id="id_point_cut"  expression="execution(* com.myapp.service.*.*(..)) and !(@annotation(org.springframework.transaction.annotation.Transactional) or @within(org.springframework.transaction.annotation.Transactional)))"/>
    <aop:advisor pointcut-ref="id_point_cut" advice-ref="transactionInterceptorAdvice"/>
aop:config>

1.2 注解@Transactional 事务

1.2.1 注解事务支持@Transactional

仔细看代码,发现这样的自动创建逻辑非常多的类,最终只有一个自动创建代理去处理这些信息! org.springframework.aop.config.AopConfigUtils 中有判断优先级,判断从低到高最终只有一个自动代理创建!因为最终创建的bean的名称都是 “org.springframework.aop.config.internalAutoProxyCreator" 意味着最终只有一个优先级比较高的自动代理进行创建代理活动!针对所有的advisor

static {
		APC_PRIORITY_LIST.add(InfrastructureAdvisorAutoProxyCreator.class);
		APC_PRIORITY_LIST.add(AspectJAwareAdvisorAutoProxyCreator.class);
		APC_PRIORITY_LIST.add(AnnotationAwareAspectJAutoProxyCreator.class);
}


org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true">
    
    
    
tx:annotation-driven>

1.2.2 @AspectJ 注解支持

顺带过一下@AspectJ的支持,也是采用自动代理!
org.springframework.aop.aspectj.annotation. AnnotationAwareAspectJAutoProxyCreator
通过aop命名空间的 aop:aspectj-autoproxy 声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。在内部依旧采用AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被aop:aspectj-autoproxy隐藏起来了 or 使用 @EnableAspectJAutoProxy

<aop:aspectj-autoproxy
    
                    

你可能感兴趣的:(spring,原理分析)