【笔记】SpringBoot实现AOP的快速入门

前言

SpringBoot实现AOP的快速入门

正文

  • 在service添加一个切面,用于添加代码执行的日志信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Aspect
@Slf4j
@Component
public class SysLogAspect {

@Pointcut("bean(sysUserServiceImpl)")
public void logPointCut() {}

@Around("logPointCut()")
public Object around(ProceedingJoinPoint jp)
throws Throwable{
try {
long t1 = System.currentTimeMillis();
Object result=jp.proceed();//最终会调用目标方法
long t2 = System.currentTimeMillis();
log.info("目标方法执行时常: {}",t2-t1);
return result;
}catch(Throwable e) {
log.error("目标方法执行过程中出现了问题: {}"+e.getMessage());
throw e;
}
}
}

完成