使用aop进行事务与日志管理

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>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值