SpringBoot中Aop的使用

依赖

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
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

...

<!--spring-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>

打印方法出入参

MethodLogAop.java:

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
57
58
59
60
61
62
63
64
65
package com.kangyonggan.demo.aop;

import com.alibaba.fastjson.JSON;
import com.kangyonggan.demo.annotation.MethodLog;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;

/**
* @author kangyonggan
* @since 2019-03-26
*/
@Aspect
@Configuration
@Log4j2
public class MethodLogAop {

/**
* 切入点
*/
@Pointcut("execution(* com.kangyonggan.demo.service.*.*(..))")
public void pointCut() {
}

/**
* 环绕方法
*
* @param joinPoint
* @return
* @throws Throwable
*/
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args = joinPoint.getArgs();
Class clazz = joinPoint.getTarget().getClass();

MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = clazz.getDeclaredMethod(methodSignature.getName(), methodSignature.getParameterTypes());
String targetName = "[" + clazz.getName() + "." + method.getName() + "]";

MethodLog methodLog = method.getAnnotation(MethodLog.class);
Object result;
if (methodLog != null) {
log.info("进入方法:{} - args: {}", targetName, JSON.toJSONString(args));

long beginTime = System.currentTimeMillis();
result = joinPoint.proceed(args);
long endTime = System.currentTimeMillis();
long time = endTime - beginTime;

log.info("离开方法:{} - return: {}", targetName, JSON.toJSONString(result));
log.info("方法耗时:{}ms - {}", time, targetName);
} else {
result = joinPoint.proceed(args);
}

return result;
}
}

其中MethodLog.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.kangyonggan.demo.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 方法日志。打印出参、入参和执行时间
*
* @author kangyonggan
* @since 2018/6/3 0003
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodLog {

}