[spring]Spring AOP 及 代理模式

文章目录

一. AOP介绍

Spring框架两大核心:

  1. IoC
  2. AOP



二. AOP使用

  1. 引入依赖
xml 复制代码
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-aop</artifactId>
 </dependency>
  1. 编写AOP程序
    记录Controller中每个方法的执行时间
java 复制代码
@Slf4j
@Aspect
@Component
public class TimeAspect {
    @Around("execution(* com.example.demo.controller.*.*(..))")
    public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {
        //记录方法执行开始时间
        long begin = System.currentTimeMillis();
        //执行方法
        Object result = pjp.proceed();
        //记录执行方法结束时间
        long end = System.currentTimeMillis();
        //记录方法执行耗时
        log.info(pjp.getSignature() + "耗时: {} ms",end-begin);
        return result;
    }
}

Spring AOP核心概念

切点


这就是切点表达式

连接点

java 复制代码
@RequestMapping("/test")
@RestController
public class TestController {
    @RequestMapping("/t1")
    public String t1() {
        return "t1";
    }
    @RequestMapping("/t2")
    public Boolean t2() {

        return true;
    }
    @RequestMapping("/t3")
    public Integer t3() {
        return 1;
    }
}

这些方法都是连接点

通知


切面


切⾯所在的类,我们⼀般称为切⾯类(被@Aspect注解标识的类)

通知类型

java 复制代码
@Slf4j
@Aspect
@Component
public class TimeAspect {
    @Around("execution(* com.example.demo.controller.*.*(..))")
    public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {
        //记录方法执行开始时间
        long begin = System.currentTimeMillis();
        //执行方法
        Object result = pjp.proceed();
        //记录执行方法结束时间
        long end = System.currentTimeMillis();
        //记录方法执行耗时
        log.info(pjp.getSignature() + "耗时: {} ms",end-begin);
        return result;
    }

    @Before("execution(* com.example.demo.controller.*.*(..))")
    public void doBefore(){
        log.info("执行before方法");
    }
    @After("execution(* com.example.demo.controller.*.*(..))")
    public void doAfter(){
        log.info("执行After方法");

    }
    @AfterReturning("execution(* com.example.demo.controller.*.*(..))")
    public void doAfterReturning(){
        log.info("执行AfterReturning方法");

    }
    @AfterThrowing("execution(* com.example.demo.controller.*.*(..))")
    public void doAfterThrowing(){
        log.info("执行AfterThrowing方法");

    }
    @Around("execution(* com.example.demo.controller.*.*(..))")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("Around方法开始前");
        Object object = joinPoint.proceed();
        log.info("Around方法开始后");
        return object;
    }
}

测试:

发生异常:


@PointCut

java 复制代码
 @Pointcut("execution(* com.example.demo.controller.*.*(..))")
    public void pt(){}

    @Before("pt()")
    public void doBefore(){
        log.info("执行before方法");
    }

切面优先级@Order

现有两个切面类, 分别有before和after方法, 那么方法的执行顺序为:




切点表达式

execution(...)

execution() 是最常⽤的切点表达式,⽤来匹配⽅法,语法为:

execution(<访问修饰符> <返回类型> <包名.类名.⽅法(⽅法参数)> <异常>)

其中:访问修饰符和异常可以省略

@annotation

可是使用自定义注解和@annotation的方式来描述哪些方法需要切面

自定义注解
java 复制代码
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAspect {
}
使用@annotation

使用@annotation来描述自定义注解, 放在切点表达式中

表示, 只有加了自定义注解的方法, 才是连接点

java 复制代码
@Aspect
@Component
@Slf4j
public class MyAspectDemo {
    @Around("@annotation(com.example.demo.aspect.MyAspect)")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("方法前");
        Object object = joinPoint.proceed();
        log.info("方法后");
        return object;
    }
}
添加自定义注解

只给t3加注解, 测试:

三. 代理模式

使用代理前:

使用代理后:



静态代理:在程序运⾏前,代理类的.class⽂件就已经存在了

相⽐于静态代理来说,动态代理更加灵活

Spring AOP是基于动态代理实现的

Java也对动态代理进⾏了实现,并给我们提供了⼀些API,常⻅的实现⽅式有两种:

JDK动态代理有⼀个最致命的问题是其只能代理实现了接⼝的类

相关推荐
wifi___9 小时前
全局异常处理的原理
java·开发语言
2601_9638702013 小时前
【计算机毕业设计】基于Spring Boot的专科医院医疗管理系统
java·spring boot·课程设计
Bingo_BIG13 小时前
Java Spring 批量修改,实体、接口、方法的定义
java·spring
画中有画14 小时前
软件架构中质量属性(性能、安全、可扩展性)的权衡设计
java·运维·安全
Shaoxi Zhang14 小时前
JAVA学习笔记035——对象和JSON格式
java·笔记·学习
赵丙双14 小时前
CountDownLatch 源码分析
java·aqs·countdownlatch
余额瞒着我当琳14 小时前
C++--深拷贝三件套 + swap + 写时拷贝 + vector 扩容 + reserve
java·开发语言·c++
thefool11226615 小时前
翻转二叉树
java
喜欢打篮球的普通人15 小时前
LLVM Backend Lowering 从入门到实战:把 IR 变成机器码的完整链路
android·java·数据库
FL162386312916 小时前
室内易燃物识别易燃评估室内易燃程度识别分割数据集labelme格式1015张85类别
java·服务器·前端