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

    这只是一个模板

  • 相关阅读:
    C++箴言:避免构造或析构函数中调用虚函数
    程序员面试题精选100题(32)-不能被继承的类
    面试题之数组统计
    面试题:找出数组中只出现一次的2个数(异或的巧妙应用)(出现3次)
    数组Magic Index
    ALAssetsLibrary学习总结
    设计模式
    android之LruCache源代码解析
    Mac OS X将CSV格式转换为Excel文档格式,Excel转CSV中文乱码问题
    Atitit.jquery 版本号新特性attilax总结
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11639363.html
Copyright © 2011-2022 走看看