SpringMVC - XML配置AOP方法案例详解

XML配置AOP详解

切点表达式的写法

表达式语法:

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qWbG69n9-1650867990190)(spring.assets/image-20220425133216837.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IIlCCpTd-1650867990192)(spring.assets/image-20220425133507549.png)]

尝试修改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>

成功测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tQrZWOEc-1650867990193)(spring.assets/image-20220425133817560.png)]

通知类型

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HcNTlZSa-1650867990194)(spring.assets/image-20220425134843418.png)]

修改通知类(后置增强)

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>
成功测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hkHZQipp-1650867990194)(spring.assets/image-20220425135433611.png)]

修改通知类(切点环绕)

添加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>
成功测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lrCouBJW-1650867990195)(spring.assets/image-20220425140408105.png)]

修改通知类(异常抛出)

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>
成功测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UIjlboeJ-1650867990196)(spring.assets/image-20220425141025505.png)]

修改通知类(最终增强)

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>
成功测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CGRhCnOD-1650867990198)(spring.assets/image-20220425141409032.png)]

切点表达式抽取

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iS92nFWD-1650867990198)(spring.assets/image-20220425141548827.png)]

抽取案例

<!--    配置织入:告诉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>

成功测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EZKRU2ZX-1650867990199)(spring.assets/image-20220425142118335.png)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鬼鬼骑士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值