【Spring框架】Spring AOP

什么是AOP?

AOP(Aspect Oriented Programming):⾯向切⾯编程,它是⼀种思想,它是对某⼀类事情的集中处理。⽐如⽤户登录权限的效验,没学 AOP 之前,我们所有需要判断⽤户登录的⻚⾯(中的⽅法),都要各⾃实现或调⽤⽤户验证的⽅法,然⽽有了 AOP 之后,我们只需要在某⼀处配置⼀下,所有需要判断⽤户登录⻚⾯(中的⽅法)就全部可以实现⽤户登录验证了,不再需要每个⽅法中都写相同的⽤户登录验证了。

AOP组成

  1. 切面(Aspect):定义的是事件(AOP是啥的)。ex:用户登录校验
  2. 切点(Pointcut):定义具体规则。ex:定义用户登录拦截规则,哪些接口判断用户登录权限?哪些不判断。
  3. 通知(Advice):AOP执行的具体方法。ex:获取用户登录信息,如果获取到说明已经登录,否则未登录。
    前置通知
    后置通知
    环绕通知
    返回通知
  4. 连接点(Join Point):有可能触发切点的所有点。ex:所有接口

Spring AOP 实现步骤

1.添加Spring AOP依赖

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-bo
ot-starter-aop -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2.定义切面。

package com.example.demo.common;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect // 定义切面
@Component
public class UserAspect {
    
      
}

3.定义切点。

package com.example.demo.common;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect // 定义切面
@Component
public class UserAspect {
    
    
    
    // 切点
    @Pointcut("execution(* com.example.demo.controller.UserController.*(..))")
    public void pointcut() {
    
    }
}

4.执行通知。

package com.example.demo.common;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect // 定义切面
@Component
public class UserAspect {
    
    

    // 切点
    @Pointcut("execution(* com.example.demo.controller.UserController.*(..))")
    public void pointcut() {
    
    
    }

    // 通知
    @Before("pointcut()")
    public void doBefore() {
    
    
        System.out.println("执行了前置通知");
    }
}

package com.example.demo.common;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect // 定义切面
@Component
public class UserAspect {
    
    

    // 切点
    @Pointcut("execution(* com.example.demo.controller.UserController.*(..))")
    public void pointcut() {
    
    
    }

    // 通知
    @Before("pointcut()")
    public void doBefore() {
    
    
        System.out.println("执行了前置通知");
    }

    // 后置通知
    @After("pointcut()")
    public void doAfter() {
    
    
        System.out.println("执行了后置方法");
    }

    // 环绕通知
    @Around("pointcut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    
    
        System.out.println("环绕通知执行之前");
        // 执行目标方法
        Object result = joinPoint.proceed();
        System.out.println("环绕通知执行之后");
        return result;
    }
}

在这里插入图片描述

Spring AOP实现原理

Spring AOP 是构建在动态代理基础上,因此 Spring 对 AOP 的⽀持局限于⽅法级别的拦截。
Spring 动态代理组成:
1.JDK Proxy 代理对象必须实现接口,才能使用JDK Proxy 。
2.CGLIB 通过实现代理类的子类来实现动态代理。

JDK Proxy VS CGLIB

1.出生不同。
2.实现不同:JDK Proxy要求代理类实现接口才能实现代理; CGLIB 是通过实现子类完成动态代理。
3.性能不同:JDK 7+ JDK Proxy性能是略高于CGLIB ;JDK 7之前 CGLIB 性能远远高于JDK Proxy。

猜你喜欢

转载自blog.csdn.net/weixin_61341342/article/details/132078005