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());
            }
        }
    }
  • 相关阅读:
    收藏好博客
    iOS设备的重力感应
    局域网内通过UDP协议进行传输接受数据——AsyncUdpSocket
    定时器NSTimer的用法
    线程数:5,ramp-up:1,循环::10 和 线程数:10,ramp-up:10,循环数:1,这两种情况有没有区别?
    什么是性能测试?
    JMeter 之 XPath提取器
    DNS--安装&&配置文件
    DNS--简介&&解析过程
    Tomcat--隐藏版本号
  • 原文地址:https://www.cnblogs.com/rinack/p/6393589.html
Copyright © 2011-2022 走看看