文章目录

  • XML配置AOP详解
  • 切点表达式的写法
  • 尝试修改spring.xml的织入
  • 成功测试
  • 通知类型
  • 修改通知类(后置增强)
  • 增加切面/增加通知
  • 成功测试
  • 修改通知类(切点环绕)
  • 编写切面/编写通知
  • 成功测试
  • 修改通知类(异常抛出)
  • 编写目标类(制造异常)
  • 编写切面/编写通知
  • 成功测试
  • 修改通知类(最终增强)
  • 编写切面/编写通知
  • 成功测试
  • 切点表达式抽取
  • 抽取案例
  • 成功测试


XML配置AOP详解

切点表达式的写法

表达式语法:

execution{[修饰符]返回值类型 包名.类名.方法名(参数)}

yml配置 springmvc 发生put 和delete 请求_AOP

yml配置 springmvc 发生put 和delete 请求_maven_02

尝试修改spring.xml的织入

<aop:config>
        <aop:aspect ref="myAspect">
            <aop:before method="before" pointcut="execution(* com.taotao.aop.Target.save())"></aop:before>
        </aop:aspect>
    </aop:config>

成功测试

yml配置 springmvc 发生put 和delete 请求_java_03

通知类型

yml配置 springmvc 发生put 和delete 请求_AOP_04

修改通知类(后置增强)

MyAspect.java

添加一个方法

package com.taotao.aop;

/**
 * create by 刘鸿涛
 * 2022/4/24 19:51
 */
@SuppressWarnings({"all"})
public class MyAspect {
    public void before(){
        System.out.println("前置增强....");
    }

    public void afterReturning(){
        System.out.println("后置增强.....");
    }
}
增加切面/增加通知

修改Spring.xml

<aop:config>
<!--        声明切面-->
        <aop:aspect ref="myAspect">
<!--            切面:切点 + 通知-->
            <aop:before method="before" pointcut="execution(* com.taotao.aop.Target.save())"></aop:before>
            <aop:after-returning method="afterReturning" pointcut="execution(* com.taotao.aop.Target.save())"></aop:after-returning>
        </aop:aspect>
    </aop:config>
成功测试

yml配置 springmvc 发生put 和delete 请求_maven_05

修改通知类(切点环绕)

添加around()方法

package com.taotao.aop;

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * create by 刘鸿涛
 * 2022/4/24 19:51
 */
@SuppressWarnings({"all"})
public class MyAspect {
    //Proceeding JoinPoint:正在执行的连接点 === 切点
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");

        Object proceed = pjp.proceed(); //切点方法

        System.out.println("环绕后增强...");
    }
}
编写切面/编写通知

编写Spring.xml

<!--    配置织入:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置...)-->
    <aop:config>
<!--        声明切面-->
        <aop:aspect ref="myAspect">
<!--            切面:切点 + 通知-->
            <aop:around method="around" pointcut="execution(* com.taotao.aop.Target.save())"></aop:around>
        </aop:aspect>
    </aop:config>
成功测试

yml配置 springmvc 发生put 和delete 请求_AOP_06

修改通知类(异常抛出)

package com.taotao.aop;

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * create by 刘鸿涛
 * 2022/4/24 19:51
 */
@SuppressWarnings({"all"})
public class MyAspect {
        //Proceeding JoinPoint:正在执行的连接点 === 切点
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");

        Object proceed = pjp.proceed(); //切点方法

        System.out.println("环绕后增强...");
    }
    
    public void afterThrowing(){
        System.out.println("异常抛出增强......");
    }

}
编写目标类(制造异常)
package com.taotao.aop;

/**
 * create by 刘鸿涛
 * 2022/4/24 16:27
 */
@SuppressWarnings({"all"})
public class Target implements TargetInterface {
    @Override
    public void save() {
        int i = 1 / 0;
        System.out.println("正在存储数据....");
    }
}
编写切面/编写通知
<!--    配置织入:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置...)-->
    <aop:config>
<!--        声明切面-->
        <aop:aspect ref="myAspect">
<!--            切面:切点 + 通知-->
            <aop:around method="around" pointcut="execution(* com.taotao.aop.Target.save())"></aop:around>
            
            <aop:after-throwing method="afterThrowing" pointcut="execution(* com.taotao.aop.Target.save())"></aop:after-throwing>
        </aop:aspect>
    </aop:config>
成功测试

yml配置 springmvc 发生put 和delete 请求_spring_07

修改通知类(最终增强)

package com.taotao.aop;

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * create by 刘鸿涛
 * 2022/4/24 19:51
 */
@SuppressWarnings({"all"})
public class MyAspect {
    public void before(){
        System.out.println("前置增强....");
    }

    public void afterReturning(){
        System.out.println("后置增强.....");
    }

    //Proceeding JoinPoint:正在执行的连接点 === 切点
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");

        Object proceed = pjp.proceed(); //切点方法

        System.out.println("环绕后增强...");
    }

    public void afterThrowing(){
        System.out.println("异常抛出增强......");
    }
    
    public void after(){
        System.out.println("最终增强...");
    }

}
编写切面/编写通知
<!--    配置织入:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置...)-->
    <aop:config>
<!--        声明切面-->
        <aop:aspect ref="myAspect">
<!--            切面:切点 + 通知-->
            <aop:around method="around" pointcut="execution(* com.taotao.aop.Target.save())"></aop:around>

            <aop:after-throwing method="afterThrowing" pointcut="execution(* com.taotao.aop.Target.save())"></aop:after-throwing>
            
            <aop:after method="after" pointcut="execution(* com.taotao.aop.Target.save())"></aop:after>
        </aop:aspect>
    </aop:config>
成功测试

yml配置 springmvc 发生put 和delete 请求_java_08

切点表达式抽取

yml配置 springmvc 发生put 和delete 请求_spring_09

抽取案例

<!--    配置织入:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置...)-->
    <aop:config>
<!--        声明切面-->
        <aop:aspect ref="myAspect">

<!--            抽取切点表达式-->
            <aop:pointcut id="MyPointcut" expression="execution(* com.taotao.aop.*.*(..))"/>

<!--            切面:切点 + 通知-->
            <aop:around method="around" pointcut-ref="MyPointcut"></aop:around>

        </aop:aspect>
    </aop:config>

成功测试

yml配置 springmvc 发生put 和delete 请求_XML_10