1.明确概念
• Joinpoint (连接点):类里面可以被增强的方法即为连接点。例如,想修改哪个方法的功能, 那么该方法就是一个连接点。
• Pointcut(切入点):对 Joinpoint 进行拦截的定义即为切入点,例如,拦截所有以 insert 开始的 方法,这个定义即为切入点。
• Advice (通知):拦截到 Joinpoint 之后所要做的事情就是通知。例如, 上文说到的打印日志 监控。 通知分为前直通知、后直通知、异常通知、最终通知和环绕通知。
• Aspect (切面): Pointcut 和 Advice 的结合。
• Target (目标对象):要增强的类称为 Target。
2.spring boot 配置文件
3. 编写 aspect 类
/**
* @Author: CARRY
* @CreateTime: 2019-06-28 09:52
* @Description: ${Description}
*/
@Component
@Aspect //声明这是个切面类
public class LogAspect {
//切入点表达式
// execution 中的第一 个*表示方法返回任意值,第二个*表示 service 包下的任意类,第三个*表示类中的任意方法,
// 括号中的两个点表示方法参数任意, 这里描述的切入点为 service 包下所有类中的所有方法。
@Pointcut("execution(* org.carry.service.*.*(..))")
public void pc1() {
}
//前置通知 通过JoinPoint参数可以获取目标方法的方法名、 修饰符等信息
@Before(value = "pc1()")
public void before(JoinPoint jp) {
String name = jp.getSignature().getName();
System.out.println(name + "方法开始执行...");
}
//后置通知
@After(value = "pc1()")
public void after(JoinPoint jp) {
String name = jp.getSignature().getName();
System.out.println(name + "方法执行结束...");
}
//返回通知,在该方法中可以获取目标方法的返回值
@AfterReturning(value = "pc1()", returning = "result")
public void afterReturning(JoinPoint jp, Object result) {
String name = jp.getSignature().getName();
System.out.println(name + "方法返回值为:" + result);
}
//异常通知 可获取异常e
@AfterThrowing(value = "pc1()", throwing = "e")
public void afterThrowing(JoinPoint jp, Exception e) {
String name = jp.getSignature().getName();
System.out.println(name + "方法抛异常了,异常是:" + e.getMessage());
}
//环绕通知 包含上述通知
@Around("pc1()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
return pjp.proceed();
}
}
4. 执行结果
5. 项目代码:https://github.com/XueKX/spring-boot-notes/tree/master/spring-boot-aop
----------------------------------------------------我是分割线--------------------------------------------------------------------------------------------
---------------------
欢迎关注博主个人公众号,一起来嗨皮呀