• Spring之AOP的切点、通知、切点表达式以及知识要点


    1.2.1、需要编写的内容

    • 编写核心业务代码(目标类的目标方法)

    • 编写切面类,切面类中有通知(增强功能方法)

    • 在配置文件中,配置织入关系,即将哪些通知与哪些连接点进行结合

    1.2.2、AOP 技术实现的内容

    Spring 框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。

    1.2.3、AOP 底层使用哪种代理方式

    在 spring 中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式。

    1.2.4、知识要点

    • aop:面向切面编程

    • aop底层实现:基于JDK的动态代理 和 基于Cglib的动态代理

    • aop的重点概念:

      Pointcut(切入点):被增强的方法
      
      Advice(通知/ 增强):封装增强业务逻辑的方法
      
      Aspect(切面):切点+通知
      
      Weaving(织入):将切点与通知结合的过程
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    • 开发明确事项:

      谁是切点(切点表达式配置)
      
      谁是通知(切面类中的增强方法)
      
      将切点和通知进行织入配置
      
      • 1
      • 2
      • 3
      • 4
      • 5

    1.2.5、 基于 XML 的 AOP 开发

    1.2.5.1 快速入门

    ①导入 AOP 相关坐标

    ②创建目标接口和目标类(内部有切点)

    ③创建切面类(内部有增强方法)

    ④将目标类和切面类的对象创建权交给 spring

    ⑤在 applicationContext.xml 中配置织入关系

    ⑥测试代码

    ①导入 AOP 相关坐标

    
    <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>5.0.5.RELEASEversion>
    dependency>
    
    
    <dependency>
    <groupId>org.aspectjgroupId>
    <artifactId>aspectjweaverartifactId>
    <version>1.8.13version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    ②创建目标接口和目标类(内部有切点)

    //接口
    public interface TargetInterface {
      public void method();
    }
    
    //实现类
    public class Target implements TargetInterface {
      @Override
      public void method() {
          System.out.println("Target running....");
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    ③创建切面类(内部有增强方法)

    public class MyAspect {
      //前置增强方法
      public void before(){
          System.out.println("前置代码增强.....");
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    ④将目标类和切面类的对象创建权交给 spring管理

    
    <bean id="target" class="com.itheima.aop.Target">bean>
    
    
    <bean id="myAspect" class="com.itheima.aop.MyAspect">bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    ⑤在 applicationContext.xml 中配置织入关系

    导入aop命名空间

    <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xsi:schemaLocation="
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    ⑤在 applicationContext.xml 中配置织入关系

    配置切点表达式和前置增强的织入关系

    
    <aop:config>
      
      
      <aop:aspect ref="myAspect">
          
          
          
          <aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.method())">aop:before>
      aop:aspect>
    aop:config>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    ⑥测试代码

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class AopTest {
      @Autowired
      private TargetInterface target;
      @Test
      public void test1(){
          target.method();
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1.2.5.2 切点表达式的写法

    表达式语法:

    execution([修饰符] 返回值类型 包名.类名.方法名(参数))
    
    • 1
    • 访问修饰符可以省略

    • 返回值类型、包名、类名、方法名可以使用星号* 代表任意

    • 包名与类名之间一个点 . 代表当前包下的类,两个点 … 表示当前包及其子包下的类

    • 参数列表可以使用两个点 … 表示任意个数,任意类型的参数列表

    例如:

    execution(public void com.itheima.aop.Target.method())	//不常用,具体的方法,限制了
    execution(void com.itheima.aop.Target.*(..))   //具体的类下的所有方法都会被增强,无返回值
    
    execution(* com.itheima.aop.*.*(..))   //常用(推荐),该包下的任意类的任意方法(参数任意)都会被增强,返回值任意
    execution(* com.itheima.aop..*.*(..))  //aop下及其子包下的任意类任意方法都会被增强
    execution(* *..*.*(..))   //全部都任意
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1.2.5.3 通知的类型

    通知的配置语法:

    aop:通知类型>
    
    • 1

    在这里插入图片描述

    Java类:

    //增强对象
    public class MyAspect {
        //后置增强
        public void before() {
            System.out.println("前置增强................................");
        }
    
        //后置增强
        public void afterReturning() {
            System.out.println("后置增强................................");
        }
    
        //Proceeding JoinPoint 正在执行的链接点----切点
        //环绕增强
        public Object Around(ProceedingJoinPoint point) throws Throwable {
            System.out.println("环绕前增强................................");
            Object proceed = point.proceed();//切点方法
            System.out.println("环绕后增强................................");
            return proceed;
        }
    
        //异常增强
        public void afterThrowing() {
            System.out.println("异常增强................................");
        }
    
        //最终增强
        public void after() {
            System.out.println("最终增强................................");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    xml:

                
               
                <aop:before method="before" pointcut="execution( * com.lfs.aop.*.*(..))"/>
    
                
    <aop:after-returning method="afterReturning" pointcut="execution(* com.lfs.aop.*.*(..))"/>
    
                
                <aop:around method="Around" pointcut="execution(* com.lfs.aop.*.*(..))"/>
    
                
    <aop:after-throwing method="afterThrowing" pointcut="execution(* com.lfs.aop.*.*(..))"/>
    
                
                <aop:after method="after" pointcut="execution(* com.lfs.aop.*.*(..))"/>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    1.2.5.4 切点表达式的抽取

    当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽取后的切点表达式。

    //切点表达式
    <aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
    
    • 1
    • 2
    <aop:config>
        
        <aop:aspect ref="myAspect">
            <aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
            <aop:before method="before" pointcut-ref="myPointcut">aop:before>
        aop:aspect>
    aop:config>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1.2.5.5 知识要点
    • aop织入的配置
    <aop:config>
        <aop:aspect ref=“切面类”>
            aop:before>
        aop:aspect>
    aop:config>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 通知的类型:前置通知、后置通知、环绕通知、异常抛出通知、最终通知
    • 切点表达式的写法:
    execution([修饰符] 返回值类型 包名.类名.方法名(参数))
    
    • 1
  • 相关阅读:
    .net core appsettings.json 配置 http 无法访问
    【甄选靶场】Vulnhub百个项目渗透——项目二十六:Pinky‘s-Palace-1(代理访问,缓冲区溢出)
    了解list
    第一百五十八回 SliverGrid组件
    Nginx中对红黑树的使用
    SQL拼接动态创建表A关联数据库表B查询(数据分区储存)
    通过 Radius 实现Dapr 云原生应用程序开发协作
    monai如何读取.nii/.nii.gz数据
    Linux进程概念
    JAVA_内部类 学习笔记
  • 原文地址:https://blog.csdn.net/qq_64071654/article/details/128029855