Spring-AOP入门案例

发布于:2022-12-21 ⋅ 阅读:(201) ⋅ 点赞:(0)

目录​​​​​​​

AOP相关概念

AOP开发过程

入门案例制作分析


  • AOP相关概念

  • 1.连接点(JoinPoint)∶程序执行过程中的任意位置,粒度为执行方法、抛出异常、设置变量等在SpringAOP中,理解为方法的执行
  • 可以理解为方法
  • 2.切入点(Pointcut) :匹配连接点的式子
  • 在SpringAoP中,一个切入点可以只描述一个具体方法,也可以匹配多个方法
  • 一个具体方法: com.demo.dao包下的BookDao接口中的无形参无返回值的save方法
  • 匹配多个方法:所有的save方法,所有的get开头的方法,所有以Dao结尾的接口中的任意方法,所有带有一个参数的方法
  • 可以理解为挖掉共性功能的方法
  • 3.通知(Advice):在切入点处执行的操作,也就是共性功能
  • 在SpringAOP中,功能最终以方法的形式呈现
  • 4.切面(Aspect)︰描述通知与切入点的对应关系
  • 就是共性功能与挖的位置的对应关系
  • 5.目标对象(Target)∶原始功能去掉共性功能对应的类产生的对象,这种对象是无法直接完成最终工作的
  • 6.Weaving(织入):就是将挖掉的功能回填的动态过程
  • 7.代理(Proxy)︰目标对象无法直接完成工作,需要对其进行功能回填,通过创建原始对象的代理对象实现
  • 8.(Introduction)引介/引入:是一种特殊的通知,在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field
  • 就是对原始对象无中生有的添加成员变量或成员方法
  • AOP开发过程

  • 开发阶段(开发者完成)
  • 正常的制作程序
  • 将非共性功能开发到对应的目标对象类中,并制作成切入点方法
  • 将共性功能独立开发出来,制作成通知
  • 在配置文件中,声明切入点
  • 在配置文件中,声明切入点通知间的关系(含通知类型),即切面
  • 运行阶段(AOP完成)
  • Spring容器加载配置文件,监控所有配置的切入点方法的执行
  • 当监控到切入点方法被运行,使用代理机制,动态创建目标对象代理对象,根据通知类别,在代理对象的对应位置将通知对应的功能织入,完成完整的代码逻辑并运行
  • AOP开发方式
  • XML方式,XML+注解方式,注解方式
  • 入门案例制作分析

  • 1.导入相关坐标
  • <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.3.22</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.9.1</version>
            </dependency>
        </dependencies>
  • 2.确认要抽取的功能,并将其制作成方法保存到专用的类中,删除原始业务中对应的功能
  • 
    public class UserServiceImpl implements UserService {
        public void save(){
            //0.将共性功能抽取出来
            //System.out.println("共性功能");
            System.out.println("user service running...");
        }
    }
    
    
    //1.制作通知类,在类中定义一个方法用于完成共性功能
    public class AOPAdvice {
        public void function(){
            System.out.println("共性功能");
        }
    }
  • 3.将所有进行AOP操作的资源加载到IOC容器中
  • 4.使用配置的方式描述被抽取功能的位置,并描述被抽取功能与对应位置的关系
  • <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
        <!--3.开启AOP命名空间-->
    
        <bean id="userService" class="com.superdemo.service.impl.UserServiceImpl"/>
        <!--2.配置共性功能成为spring控制的资源-->
        <bean id="myAdvice" class="com.superdemo.aop.AOPAdvice"/>
        <!--4.配置AOP-->
        <aop:config>
            <!--5.配置切入点-->
            <aop:pointcut id="pt" expression="execution(* *..*(..))"/>
            <!--6.配置切面(切入点与通知的关系)-->
            <aop:aspect ref="myAdvice">
                <!--7.配置具体的切入点对应通知中的哪个操作方法-->
                <aop:before method="function" pointcut-ref="pt"/>
            </aop:aspect>
        </aop:config>
    </beans>
  • 5.运行程序
本文含有隐藏内容,请 开通VIP 后查看