Spring--AOP

本文介绍了面向切面编程(AOP)的基础概念,包括AOP底层原理(动态代理与连接点、切入点、通知等),以及如何在Spring框架中使用AspectJ进行AOP操作,如基于XML和注解的配置。重点讲解了如何配置通知并配合相关依赖实现业务增强。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 什么是AOP

面向切面编程,面向方面编程。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高开发效率。

不通过修改源代码的方式,在主干功能里面添加新的功能

2. AOP底层原理

2.1 AOP底层使用动态代理

有两种情况动态代理

  • 有接口的情况:
    • 使用JDK动态代理
    • 创建UserDao接口实现类代理对象
  • 没有接口的情况:
    • 使用CGLIB动态代理
    • 创建当前类子类的代理对象
3. AOP术语
  1. 连接点:类里面的哪些方法可以被增强,这些方法称为连接点
  2. 切入点:实际被真正增强的方法,称为切入点
  3. 通知(增强):
    • 实际增强的逻辑部分称为通知
    • 通知分为多种类型:
      • 前置通知
      • 后置通知
      • 环绕通知
      • 异常通知
      • 最终通知
  4. 切面: 动作
    • 把通知应用到切入点的过程
4. AOP操作(准备)
  1. Spring框架一般都是基于AspectJ实现AOP操作
  2. 什么是AspectJ
    • AspectJ不是Spring组成部分,独立AOP框架,一般把AspectJ和Spring框架一起使用,进行AOP操作。
  3. 基于AspectJ实现AOP操作
    • 基于xml配置文件实现
    • 基于注解方式实现
  4. 在项目中引入相关依赖
  5. 切入点表达式
    • 切入点表达式作用:知道对哪个类里面的哪个方法进行增强
    • 语法结构:
      • execution([权限修饰符] [返回类型] [方法名称] ([参数列表]))
5. 相关依赖
 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>


    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>3.3.0</version>
    </dependency>


    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.8</version>
    </dependency>

    <dependency>
      <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
    </dependency>

6.操作步骤
  1. 创建类,在类中定义方法

  2. 创建增强类,编写增强逻辑

  3. 进行通知的配置

    • 在spring配置文件或者配置类中开启注解扫描
    • 使用注解创建User和增强类对象
    • 在增强类上添加 @Aspect
    • 在spring配置文件中开启生成代理对象
    @Component
    @Aspect //生成代理对象
    public class addProxy {
    
        //前置通知
        @Before(value ="execution(* com.java.Spring5.User.add(..))")
        public void before(){
            System.out.println("before....");
        }
    
        @After(value ="execution(* com.java.Spring5.User.add(..))")
        public void after(){
            System.out.println("after...");
        }
    
        @Around(value ="execution(* com.java.Spring5.User.add(..))")
        public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
            System.out.println("环绕之前");
            proceedingJoinPoint.proceed();
            System.out.println("环绕之后");
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值