spring aop实现打印方法执行时间

来源:互联网 发布:太原市九鼎软件 编辑:程序博客网 时间:2024/06/05 18:02

spring aop基于代理实现,主要包含两种----接口代理,类代理


接口代理   可参考jdk代理,主要通过反射来实现
类代理     可参考cglib代理,主要使用继承来实现


注:两种代理均无法实现对私有方法和静态方法的代理


这两种代理都可以看做代理设计模式的一种实现.


前一段时间要统计一个项目中各个方法的执行时间,就用spring aop做了一个实现,以下是相关代码

1.添加包依赖  注:aspectj版本过低会导致无法识别execution表达式

<dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-aop</artifactId>    <version>${spring.version}</version></dependency><dependency>    <groupId>org.aspectj</groupId>    <artifactId>aspectjweaver</artifactId>    <version>1.8.10</version></dependency><dependency>    <groupId>org.aspectj</groupId>    <artifactId>aspectjrt</artifactId>    <version>1.8.9</version></dependency><dependency>    <groupId>org.aspectj</groupId>    <artifactId>aspectjtools</artifactId>    <version>1.8.9</version></dependency>

2.配置文件

<aop:aspectj-autoproxyproxy-target-class="true"/>

proxy-target-class强制代理,可以避免无接口类的代理失败

因为我还要统计Controller层的方法,所以此处强制使用类代理

3.代码

import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;import java.util.HashMap;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;/** * @Author MissNull * @Description: * @Date: Created in 2017/8/14. */@Aspect@Componentpublic class PrintTime {    private Map<Long, Map<String, List<Long>>> threadMap = new ConcurrentHashMap<>(200);    @Pointcut(value = "execution(* com.ai.shop.controller..*.*(..))")    public void controller() {    }    @Pointcut(value = "execution(* com.ai.shop.dao.impl.*.*(..))")    public void dao() {    }    @Pointcut(value = "execution(* com.ai.shop.services.impl.*.*(..))")    public void service() {    }    @Before(value = "controller() || dao() || service()")    public void before(JoinPoint joinPoint) {        System.out.println(joinPoint.toShortString() + " 开始");        Map<String, List<Long>> methodTimeMap = threadMap.get(Thread.currentThread().getId());        List<Long> list;        if (methodTimeMap == null) {            methodTimeMap = new HashMap<>();            list = new LinkedList<>();            list.add(System.currentTimeMillis());            methodTimeMap.put(joinPoint.toShortString(), list);            threadMap.put(Thread.currentThread().getId(), methodTimeMap);        } else {            list = methodTimeMap.get(joinPoint.toShortString());            if (list == null) list = new LinkedList<>();            list.add(System.currentTimeMillis());            methodTimeMap.put(joinPoint.toShortString(), list);        }    }    @After(value = "controller() || dao() || service()")    public void after(JoinPoint joinPoint) {        System.out.println(joinPoint.toShortString() + " 结束");        Map<String, List<Long>> methodTimeMap = threadMap.get(Thread.currentThread().getId());        List<Long> list = methodTimeMap.get(joinPoint.toShortString());        System.out.println("耗时:" +                (System.currentTimeMillis() - list.get(list.size() - 1)));        list.remove(list.size() - 1);    }}


4.也可以在配置文件中声明,不用注解

<aop:config>    <aop:pointcut id="allMethod" expression="execution(* com.ai.*.*.*.*(..))"/>    <aop:aspect ref="printTime">        <aop:before method="before" pointcut-ref="allMethod"/>        <aop:after method="after" pointcut-ref="allMethod"/>    </aop:aspect></aop:config>



阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 感情里总担心失去怎么办 眼石移出盲僧怎么办 打仗把小便踢肿了怎么办 腿上都是挠的疤怎么办 脚上的肉烂了怎么办 商铺门口有电杆怎么办 漏电保护器坏了怎么办 空开进线烧了怎么办 过压保护灯亮怎么办 美的热水器接地异常怎么办 欠压保护器坏了怎么办 三孔插座没地线怎么办 二胡琴筒裂缝宽怎么办 有了月亮从四星宠满级的怎么办 党委下属没有党支部了党委怎么办 发生日期大于制单日期怎么办 美的空调出现p0怎么办 薯片受潮不脆了怎么办 泡过的莲子煮不烂怎么办 绿豆有煮不熟的怎么办 吃了羊肉吃西瓜怎么办 吃了狗肉和绿豆怎么办 做的衣柜没有门怎么办 蒸馒头熟了会瘪怎么办 3dmax贴图太大了怎么办 嘴皮边缘颜色深怎么办 嘴巴周围肤色暗沉怎么办 中奖彩票被洗了怎么办 牙龈下面长米粒肉疙瘩怎么办 书画印章盖反了怎么办 金龙鱼一个月不吃东西怎么办 罗汉鱼头撞扁了怎么办 房顶开槽埋线白色不一样怎么办 顶上灯挪位置线怎么办 马蜂窝弄掉又来怎么办 蜂窝弄掉又有怎么办 2018年小龙虾底板脏怎么办 一本分数线擦边过怎么办 玩具塑料球扁了怎么办 胶皮与海绵开了怎么办 安卓不支持flash了怎么办