AOP编程的基本概念与idea编辑器的配合体验过程
初始化一个基础的 AOP 程序
// 作为演示程序,没有更具体的方法体
@Slf4j
@Component
@Aspect
public class TryAspect {
@Before("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))") // * 任意方法 , .. 形参任意
public void before(){
log.info("Before ...");
}
public void after(){
log.info("After ...");
}
public void around(){
log.info("Around before ...");
// 调用目标方法
log.info("around after");
}
public void afterReturning(){
log.info("AfterReturning ...");
}
public void afterThrowing(){
log.info("After Throwing ...");
}
}
Before

注意编辑器这个位置的 m 的变化。

点击 m 提示了
@Before("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))") 切面目标的方法。
Around

before 是半圆;around 是完整的圆形。
同样点击会出现 execution 的指向的目标方法

注解: m = methods
对 around 方法,完善之后
@Around("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))")
public Object around(ProceedingJoinPoint proceedingjoinPoint) throws Throwable{
log.info("Around before ...");
// 调用目标方法
Object proceed = proceedingjoinPoint.proceed();
log.info("around after");
return proceed;
}Advise — 通知
方法体,是抽取的一组公共的实现逻辑。
log.info("Around before ...");
// 调用目标方法
Object proceed = proceedingjoinPoint.proceed();
log.info("around after");
return proceed;PointCut — 切入点
方法实际针对的目标方法的引入,是AOP方法和目标类方法的连接;
通过切入点表达式关联。
"execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))"
备注: * : 所有 DeptServiceImpl 下的方法 ; .. : 形参任意。
Acpect — 切面
advise 和 pointcut 的连接。
@Around("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))")
public Object around(ProceedingJoinPoint proceedingjoinPoint) throws Throwable{
log.info("Around before ...");
// 调用目标方法
Object proceed = proceedingjoinPoint.proceed();
log.info("around after");
return proceed;
}描述这个方法的类,就叫: 切面类。
@Slf4j
@Component
@Aspect
public class TryAspect {
@Before("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))") // * 任意方法 , .. 形参任意
public void before(){
log.info("Before ...");
}
public void after(){
log.info("After ...");
}
@Around("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))")
public Object around(ProceedingJoinPoint proceedingjoinPoint) throws Throwable{
log.info("Around before ...");
// 调用目标方法
Object proceed = proceedingjoinPoint.proceed();
log.info("around after");
return proceed;
}
public void afterReturning(){
log.info("AfterReturning ...");
}
public void afterThrowing(){
log.info("After Throwing ...");
}
}
TryAspect —— 切面类。
Target — 目标对象
biz.baijing.service.impl.DeptServiceImpl
DeptServiceImpl —— 就是目标对象。
JoinPoint — 连接点
被 AOP 控制的对象方法,即:
biz.baijing.service.impl.DeptServiceImpl
下的所有方法

这里被 红色 m 标识的方法就是 JoinPoint 。
结果

总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
IDEA下SpringBoot指定环境、配置文件启动操作过程
这篇文章主要介绍了IDEA下SpringBoot指定环境、配置文件启动过程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-08-08
SpringCloud @RefreshScope注解源码层面深入分析
@RefreshScope注解能帮助我们做局部的参数刷新,但侵入性较强,需要开发阶段提前预知可能的刷新点,并且该注解底层是依赖于cglib进行代理的,所以不要掉入cglib的坑,出现刷了也不更新情况2023-04-04
mybatis-generator 修改表结构后实体不更新问题及解决方法
本文主要介绍了如何在MyBatis Generator中修改表结构后更新实体类的问题,并提供了解决方案,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧2025-04-04
Spring中@RabbitHandler和@RabbitListener的区别详析
@RabbitHandler是用于处理消息的方法注解,它与@RabbitListener注解一起使用,这篇文章主要给大家介绍了关于Spring中@RabbitHandler和@RabbitListener区别的相关资料,需要的朋友可以参考下2024-02-02


最新评论