zoukankan      html  css  js  c++  java
  • Spring aop 切面编程

    package cn.annals.demo.proc.aop;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    
    //声明这是一个组件
    @Component
    //声明这是一个切面Bean
    @Aspect
    public class proc_service_aop {
    
        private final Logger LOG = LoggerFactory.getLogger(this.getClass());
        
        //配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点
        //@Pointcut("execution(public * cn.annals.demo.proc.service..*(..))")
        @Pointcut("execution(public * cn.annals.demo.proc.service..*.save(..))")
        public void aspect(){}
        
        /*
         * 配置前置通知,使用在方法aspect()上注册的切入点
         * 同时接受JoinPoint切入点对象,可以没有该参数
         */
        @Before("aspect()")
        public void before(JoinPoint joinPoint){
            if(LOG.isInfoEnabled()){
                LOG.info("before " + joinPoint);
            }
        }
        
        //配置后置通知,使用在方法aspect()上注册的切入点
        @After("aspect()")
        public void after(JoinPoint joinPoint){
            if(LOG.isInfoEnabled()){
                LOG.info("after " + joinPoint);
            }
        }
        
        //配置环绕通知,使用在方法aspect()上注册的切入点
        @Around("aspect()")
        public void around(ProceedingJoinPoint joinPoint){
            long start = System.currentTimeMillis();
            try {
                joinPoint.proceed();
                long end = System.currentTimeMillis();
                if(LOG.isInfoEnabled()){
                    LOG.info("around " + joinPoint + "	Use time : " + (end - start) + " ms!");
                }
            } catch (Throwable e) {
                long end = System.currentTimeMillis();
                if(LOG.isInfoEnabled()){
                    LOG.info("around " + joinPoint + "	Use time : " + (end - start) + " ms with exception : " + e.getMessage());
                }
            }
        }
        
        //配置后置返回通知,使用在方法aspect()上注册的切入点
        @AfterReturning("aspect()")
        public void afterReturn(JoinPoint joinPoint){
            if(LOG.isInfoEnabled()){
                LOG.info("afterReturn " + joinPoint);
            }
        }
        
        //配置抛出异常后通知,使用在方法aspect()上注册的切入点
        @AfterThrowing(pointcut="aspect()", throwing="ex")
        public void afterThrowing(JoinPoint joinPoint, Exception ex){
            if(LOG.isInfoEnabled()){
                LOG.info("afterThrow " + joinPoint + "	" + ex.getMessage());
            }
        }
    }
  • 相关阅读:
    列出python中可变数据类型和不可变数据类型,并简述原理
    python 字典操作
    Python的is和==
    python2和python3区别
    下面的代码在Python2中的输出是什么?解释你的答案
    编程用sort进行排序,然后从最后一个元素开始判断,去重
    如何在一个function里面设置一个全局的变量?
    用Python匹配HTML tag的时候,<.>和<.?>有什么区别?
    请写出一段Python代码实现删除一个list里面的重复元素
    什么是lambda函数?它有什么好处?
  • 原文地址:https://www.cnblogs.com/rinack/p/6393589.html
Copyright © 2011-2022 走看看