Spring AOP就要面向切面编程,目的是对真实对象做增强,AOP配置的关键点有两点
第一:配置“对谁做增强”,也叫做PointCut或者JoinPoint,意思都是一样的。
第二:配置“具体做什么增强”,也叫做advice
示例配置方案1(一般方案):
<aop:config>
<aop:aspect id="" ref="">//做具体怎么样的增强,精确到增强类
<aop:pointcut id="" expression="" />//对谁做增强
<aop:before method="" pointcut-ref=""/>//做具体什么样的增强,精确到增强类的增强方法
<aop:aspect>
</aop:config>
示例:
<!-- 事务管理器(功能:定义了增强的方法,没有说怎么增强) -->
<bean id="transactionManager" class="com.tutorialspoint.codewolf.aop.demo01.tx.TransactionManager" />
<!-- 属性:代理目标对象吗,false表示使用JDK代理,true表示使用cglib代理 -->
<aop:config proxy-target-class="false">
<!-- 配置切面Aspect,切面=真实对象(切面)+具体的增强(切面)。做关联 -->
<aop:aspect id="txAspect" ref="transactionManager">
<!-- 配置切入点,也就是WHERE -->
<aop:pointcut id="txPointCut"
expression="execution(* com.tutorialspoint.codewolf.aop.demo01.service.*Service.*(..))" />
<!-- 配置时机,也就是WHEN;并且做关联 -->
<aop:before method="begin" pointcut-ref="txPointCut" />
<aop:after-returning method="commit" pointcut-ref="txPointCut" />
<aop:after-throwing method="rollback" pointcut-ref="txPointCut" throwing="ex"/>
<aop:after method="close" pointcut-ref="txPointCut" />
</aop:aspect>
</aop:config>
实例配置方法2(事务方法):
因为Spring为事务提供了特定的增强类,而且提供了特定的增强语法,所以对于事务的增强适用这一种:
<tx:advice transaction-manager="具体的事务管理器,一般是特定组织机构提供">
<tx:attributes>
<tx:method name="对谁做增强,在切入点基础上的细化" isolation="隔离性" propagation="传播性" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="切入点表达式" id="" />
<aop:advisor advice-ref="引用哪个advice" pointcut-ref="引用哪个切入点" />
</aop:config>
示例:
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="druidDataSource" />
</bean>
<aop:config>
<aop:pointcut
expression="execution(* com.tutorialspoint.codewolf.tx.demo01.service.*Service.*(..))"
id="txPointCut" />
<aop:advisor advice-ref="xxx" pointcut-ref="txPointCut" />
</aop:config>
<tx:advice id="xxx" transaction-manager="txManager">
<tx:attributes>
<!-- 配置事务的属性;在切入点的基础上,对那些方法做具体怎么样的增强。 -->
<!-- method="trans"。 在切入点txPointCut基础上,对trans方法做具体增强(隔离性是数据库默认的隔离性;两个事务方法调用时,传播性配置为required:即默认,即要求有事务,如果环境有,则使用环境的,如果环境没有,则自己创建) -->
<tx:method name="trans" isolation="DEFAULT" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
事务的xml配置就介绍到这里,下一篇介绍事务的注解配置