声明式事务有两种方式,一种是在配置文件(xml)中做相关的事务规则声明,另一种是基于 @Transactional 注解的方式。
<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>
单一的针对bean 配置非常的麻烦, Auto proxy creator 自动诞生!
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>
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>
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>
仔细看代码,发现这样的自动创建逻辑非常多的类,最终只有一个自动创建代理去处理这些信息! 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>
顺带过一下@AspectJ的支持,也是采用自动代理!
org.springframework.aop.aspectj.annotation. AnnotationAwareAspectJAutoProxyCreator
通过aop命名空间的 aop:aspectj-autoproxy 声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。在内部依旧采用AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被aop:aspectj-autoproxy隐藏起来了 or 使用 @EnableAspectJAutoProxy
<aop:aspectj-autoproxy