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());
            }
        }
    }
  • 相关阅读:
    DOM基础
    9个实用的Javascript代码高亮脚本
    去掉超链接虚线框去掉chrome浏览器中input或textarea在得到焦点时出现黄色边框和取消可以拖动大小
    我的Gvim配置文件
    收集的一些有关UED的团队和个人博客
    JS函数:返回下一个元素节点而不是下一个节点
    分享一款图片导航效果 Animated Slideshow Navigation
    原生javascript写的Tab菜单(函数版)
    单个select语句实现MySQL查询统计次数
    mysql 语句case when
  • 原文地址:https://www.cnblogs.com/rinack/p/6393589.html
Copyright © 2011-2022 走看看