spring的AOP与声明事务

普通的通知

<bean id="serviceA" class="com.service.ServiceA"></bean>
<bean id="serviceB" class="com.service.ServiceB"></bean>
<bean id="serviceC" class="com.service.ServiceC"></bean>
<bean id="aopInterceptor" class="com.service.AopInterceptor"> </bean>
	
<aop:config>
	<!-- 切入点表达式,匹配出的方法组成一个切面 -->
	<aop:pointcut expression="execution(* com.service.Service*.*(..))" id="allMethod" />
	<aop:pointcut expression="execution(* com.service.ServiceA.*(..))" id="aMethod" />
	<aop:pointcut expression="execution(* com.service.ServiceB*.*(..))" id="bMethod"/>
	
	<!-- 配置面和通知的联系,ref指向通知 -->
	<aop:aspect ref="aopInterceptor">
		<aop:before method="before" pointcut-ref="allMethod" />
		<aop:after method="after" pointcut-ref="aMethod" />
		<aop:after-returning method="fenkai" pointcut-ref="allMethod"/>
		<aop:after-throwing method="t" pointcut-ref="allMethod"/>
		<aop:around method="around" pointcut-ref="allMethod" />
	</aop:aspect>
</aop:config>

声明事务示意图
在这里插入图片描述
声明事务的配置

<bean id="serviceA" class="com.service.ServiceA">
	<property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver">
	</property>
	<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8">
	</property>
	<property name="username" value="root"></property>
	<property name="password" value="root"></property>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"></property>	
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
	<tx:attributes>
		<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
	</tx:attributes>
</tx:advice>

<aop:config>
	<aop:pointcut expression="execution(* com.service.Service*.*(..))" id="allMethod" />
	<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod" />
</aop:config>

tx:advice是spring写好的通知
aop:aspect是我们自己配置的通知
大神的txadvice的解释

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值