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

    这只是一个模板

  • 相关阅读:
    网页中15秒后重新发送验证码,多少秒后发送验证码
    javascript 数组去重 unique
    微信支付和微信支付通知基于sdk的说明
    php对象序列化和cookie的问题,反序列化false
    php对象序列化总出错false
    图片懒加载lazyload.js详解
    手机联动地址选择框,移动端地址联动
    Java类加载原理解析(转)
    location对象介绍
    SpringMVC使用fastjson自定义Converter支持返回jsonp格式(转)
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11639363.html
Copyright © 2011-2022 走看看