AOP(Aspect Oriented Programming,面向切面编程),可简单理解为面向特定方法编程。常见使用场景 :


AOP 优点 :
在 pom. xml 中添加如下依赖:
<dependency>
<groupId>org. Springframework. Boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>以统计方法执行耗时为例:
@Component
@Aspect // 当前类为AOP类
@Slf4j
public class RecordTimeAspect {
@Around("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {
long begin = System.currentTimeMillis();
Object result = pjp.proceed();
long end = System.currentTimeMillis();
log.info("方法执行耗时: {}毫秒", end - begin);
return result;
}
}概念 | 说明 |
|---|---|
连接点(JoinPoint) | 可被 AOP 控制的方法,包含方法执行时的相关信息 |
通知(Advice) | 抽取的共性功能 (如日志、耗时统计),最终体现为一个方法 |
切入点(PointCut) | 匹配连接点的条件,决定通知应用于哪些方法 |
切面(Aspect) | 通知+切入点,描述通知于切入点的对应关系,通常为带@Aspect 注解的类 |
目标对象(Target) | 通知所应用的对象 |
AOP 底层通过动态代理机制实现对特定方法的增强。


注解 | 说明 |
|---|---|
@Around | 环绕通知,此注解标注的通知方法在目标方法前、后都被执行 |
@Before | 前置通知,此注解标注的通知方法在目标方法前被执行 |
@After (重点) | 后置通知,此注解标注的通知方法在目标方法后被执行,无论是否有异常都会执行 |
@AfterReturning | 返回后通知,此注解标注的通知方法在目标方法后被执行,有异常不会执行 |
@AfterThrowing | 异常后通知,此注解标注的通知方法发生异常后执行 |
示例 |
@Slf4j
@Component
@Aspect
public class MyAspect1 {
@Before("execution(* com.itheima.service.*.*(..))")
public void before(JoinPoint joinPoint){
log.info("before ...");
}
@Around("execution(* com.itheima.service.*.*(..))")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
log.info("around before ...");
Object result = proceedingJoinPoint.proceed();
log.info("around after ...");
return result;
}
@After("execution(* com.itheima.service.*.*(..))")
public void after(JoinPoint joinPoint){
log.info("after ...");
}
@AfterReturning("execution(* com.itheima.service.*.*(..))")
public void afterReturning(JoinPoint joinPoint){
log.info("afterReturning ...");
}
@AfterThrowing("execution(* com.itheima.service.*.*(..))")
public void afterThrowing(JoinPoint joinPoint){
log.info("afterThrowing ...");
}
}注意点:
@Pointcut("execution(* com.itheima.service.*.*(..))")
private void pt(){}
@Before("pt()")
public void before(JoinPoint joinPoint){
log.info("before ...");
}@Order (数字) 注解指定顺序,数字越小优先级越高。@Order(数字) 加在切面类上控制顺序 语法:? 代表当前可以省略,如访问修饰符(public,private)、包名,类名、异常
execution (访问修饰符? 返回值包名.类名.?方法名 (方法参数) throws 异常?)

* :匹配任意.. :匹配多层包或任意参数@Before("execution(void com.itheima.service.impl.DeptServiceImpl.delete(java.lang.Integer))")
自定义注解示例:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogOperation{}切面类示例
@Slf4j
@Component
@Aspect
public class MyAspect6 {
@Before("@annotation(com.itheima.anno.LogOperation)")
public void before(){
log.info("MyAspect6 -> before ...");
}
@After("@annotation(com.itheima.anno.LogOperation)")
public void after(){
log.info("MyAspect6 -> after ...");
}
}可以使用通配符描述切入点
* :单个独立的任意符号,可以通配任意返回值、包名、类名、方法名、任意类型的一个参数,也可以通配包、类、方法名的一部分.. :多个连续的任意符号,可以通配任意层级的包,或任意类型、任意个数的参数切入点表达式的语法规则:
execution(void com.itheima.service.impl.DeptServiceImpl.delete(java.lang.Integer))* 号代替(任意返回值类型)execution(* com.itheima.service.impl.DeptServiceImpl.delete(java.lang.Integer))* 号代替,代表任意包(一层包使用一个 *)execution(* com.itheima.*.*.DeptServiceImpl.delete(java.lang.Integer)).. 配置包名,标识此包以及此包下的所有子包execution(* com..DeptServiceImpl.delete(java.lang.Integer)) * 号代替,标识任意类execution(* com..*.delete(java.lang.Integer))* 号代替,表示任意方法execution(* com..*.*(java.lang.Integer))* 配置参数,一个任意类型的参数execution(* com.itheima.service.impl.DeptServiceImpl.delete(*)).. 配置参数,任意个任意类型的参数execution(* com..*.*(..))注意事项:
execution(* com.itheima.service.DeptService.list(..)) || execution(* com.itheima.service.DeptService.delete(..))这篇文章到此就结束了,如果觉得有帮助,欢迎点赞、收藏、关注!