博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring aop 切面编程
阅读量:7169 次
发布时间:2019-06-29

本文共 2479 字,大约阅读时间需要 8 分钟。

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@Aspectpublic 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 + "\tUse time : " + (end - start) + " ms!");            }        } catch (Throwable e) {            long end = System.currentTimeMillis();            if(LOG.isInfoEnabled()){                LOG.info("around " + joinPoint + "\tUse 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 + "\t" + ex.getMessage());        }    }}

 

转载地址:http://nxmwm.baihongyu.com/

你可能感兴趣的文章
使用C#开发ActiveX控件(新)
查看>>
js冒泡排序及计算其运行时间
查看>>
mysql表加锁、全表加锁、查看加锁、解锁
查看>>
Python yield 使用浅析
查看>>
python之函数
查看>>
ADO.NET
查看>>
Exchange服务无法启动案例分享
查看>>
我的友情链接
查看>>
SQL- @@ROWCOUNT -返回上一行执行影响的行行数
查看>>
ulimit -SHn 65535
查看>>
二、2.4版本以后的apache的安装
查看>>
子网掩码的作用
查看>>
p0f(上)
查看>>
LNMP服务跨省迁移的解决方案
查看>>
我的友情链接
查看>>
DNS配置
查看>>
MPLS ××× 互访关系控制
查看>>
如果说搞技术没前途,那是因为你技术搞的还不够深
查看>>
柳州市第一职业技术学校中心机房双活虚拟引擎容灾备份系统需求
查看>>
我的友情链接
查看>>