普通的通知
<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的解释