zoukankan      html  css  js  c++  java
  • Spring AOP

    AOP

    在xml中的配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!--增强类-->
        <bean id="txManger" class="cn.jiedada._06xmlaop.TxManger"></bean>
        
        <bean id="userService" class="cn.jiedada._06xmlaop.service.impl.UserServiceImpl"></bean>
        <aop:config>
            <!--想要增强的类,切点设置-->
            <aop:pointcut id="pointcut" expression="execution(* cn.jiedada._06xmlaop.service.I*Service.*(..))"></aop:pointcut>
            <aop:aspect ref="txManger">
               <!-- <aop:before method="begin" pointcut-ref="pointcut"></aop:before>
                <aop:after-returning method="commit" pointcut-ref="pointcut"></aop:after-returning>
                <aop:after-throwing method="rollback" pointcut-ref="pointcut"></aop:after-throwing>
                <aop:after method="close" pointcut-ref="pointcut"></aop:after>-->
                <!--增强方法-->
                <aop:around method="around" pointcut-ref="pointcut"></aop:around>
            </aop:aspect>
        </aop:config>
    </beans>

    TxManger中的配置

    这样就可以了

    package cn.jiedada._06xmlaop;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    
    public class TxManger {
        public void begin(){
            System.out.println("开启事务");
        }
        public void commit(){
            System.out.println("提交事务");
        }
        public void rollback(Throwable e){
            System.out.println("回滚事务"+e.getMessage());
        }
        public void close(){
            System.out.println("关闭事务");
        }
        public void around(ProceedingJoinPoint proceedingJoinPoint){
            try {
                begin();
                //获得传入的方法名
                proceedingJoinPoint.proceed();
                commit();
            }catch (Throwable e){
                rollback(e);
            }finally {
                close();
            }
        }
    }
    View Code

    这只是一个模板

  • 相关阅读:
    Python 42 mysql用户管理 、pymysql模块
    Python 41 多表查询 和 子查询
    Python 41 完整查询语句 和 一堆关键字
    Python 40 数据库-外键约束 、多对一与多对多的处理
    Python 40 数据库-约束
    Python 38 注册和修改密码
    eas之关于编码规则
    eas之界面之间传递参数
    eas之获取集合
    eas之单据删除代码
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11639363.html
Copyright © 2011-2022 走看看