SpringBoot中使用AOP 监控sql耗时

释放双眼,带上耳机,听听看~!

原文:http://blog.csdn.net/eacter/article/details/56016126

添加aop依赖


1
2
3
4
5
1<dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-aop</artifactId>
4</dependency>
5
  • 1
  • 2
  • 3
  • 4

写切面类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
1package com.mycompany.financial.nirvana.core.aspect;
2
3import org.aspectj.lang.JoinPoint;
4import org.aspectj.lang.ProceedingJoinPoint;
5import org.aspectj.lang.annotation.AfterReturning;
6import org.aspectj.lang.annotation.Around;
7import org.aspectj.lang.annotation.Aspect;
8import org.aspectj.lang.annotation.Pointcut;
9import org.slf4j.Logger;
10import org.slf4j.LoggerFactory;
11import org.springframework.stereotype.Component;
12
13import java.util.Arrays;
14
15/**
16 * 使用aop监控Mapper执行时间
17 * Created by me on 2017/2/20.
18 */
19@Aspect
20@Component
21public class MapperAspect {
22    private static final Logger logger = LoggerFactory.getLogger(MapperAspect.class);
23
24    @AfterReturning("execution(* com.mycompany.financial.nirvana..*Mapper.*(..))")
25    public void logServiceAccess(JoinPoint joinPoint) {
26        logger.info("Completed: " + joinPoint);
27    }
28
29
30    /**
31     * 监控com.caiyi.financial.nirvana..*Mapper包及其子包的所有public方法
32     */
33    @Pointcut("execution(* com.mycompany.financial.nirvana..*Mapper.*(..))")
34    private void pointCutMethod() {
35    }
36
37    /**
38     * 声明环绕通知
39     * @param pjp
40     * @return
41     * @throws Throwable
42     */
43    @Around("pointCutMethod()")
44    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
45        long begin = System.nanoTime();
46        Object obj = pjp.proceed();
47        long end = System.nanoTime();
48
49        logger.info("调用Mapper方法:{},\n参数:{},\n执行耗时:{}纳秒,\r\n耗时:{}毫秒",
50                pjp.getSignature().toString(), Arrays.toString(pjp.getArgs()),
51                (end - begin), (end - begin) / 1000000);
52        return obj;
53    }
54}
55
56
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++遍历文件夹

2022-1-11 12:36:11

安全经验

Junit使用教程(二)

2021-10-11 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索