aop有关事务与日志管理
1. 添加约束
在spring配置文件中加入有关的事务与aop头部约束
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
2. 事务管理
2.1 事务管理bean
<!--添加事务管理bean,注入定义的数据源datasource-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
2.2 配置事务通知
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--为对应的方法配置需要的传播特性与隔离级别-->
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
2.3 使用aop进行事务织入
<aop:config>
<!--定义aop切入点-->
<aop:pointcut id="txPointcut" expression="execution(public * gsly.fun.dao.*.* (..))"/>
<!--事务管理通知-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
之后再匹配的方法执行时,就会进行事务的管理
3. 日志管理
3.1 编写日志扩展类
public class MyLogger {
public void before(){
System.out.println("方法执行前---------------------------------->");
}
}
3.2 注入MyLogger
<!--注入日志输出类-->
<bean id="myLogger" class="gsly.fun.config.MyLogger"/>
3.3 使用aop进行日志织入
<!--配置aop织入事务-->
<aop:config>
<!--切入点-->
<aop:pointcut id="txPointcut" expression="execution(* gsly.fun.dao.*.* (..))"/>
<!--切面类-->
<aop:aspect ref="myLogger">
<aop:before method="before" pointcut-ref="txPointcut"/>
</aop:aspect>
</aop:config>