演示spring AOP的切入表达式重用和优先级问题以及怎么实现基于xml的AOP

2023-08-08 07:42:25

😀前言
本篇的Spring-AOP系类文章第五篇讲解了演示spring AOP的切入表达式重用和优先级问题以及怎么实现基于xml的AOP

🏠个人主页:尘觉主页
在这里插入图片描述

🧑个人简介:大家好,我是尘觉,希望我的文章可以帮助到大家,您的满意是我的动力😉😉

在csdn获奖荣誉: 🏆csdn城市之星2名
⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ 💓Java全栈群星计划top前5
⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ 🤗 端午大礼包获得者

💕欢迎大家:这里是CSDN,我总结知识的地方,欢迎来到我的博客,感谢大家的观看🥰
如果文章有什么需要改进的地方还请大佬不吝赐教 先在次感谢啦😊

💖演示spring AOP的切入表达式重用和优先级问题以及怎么实现基于xml的AOP

💞AOP-切入点表达式重用

🤔应用实例

● 切入点表达式重用

为了统一管理切入点表达式,可以使用切入点表达式重用技术。

● 应用案例: 在原来的代码上修改即可
  1. 修改SmartAnimalAspect.java

image-20230805173029980


    /**
     * 使用切面编程来替代原来的动态代理类,机制是一样的.
     * @author Administrator
     */
    @Aspect //表示这个类是一个切面类
    @Component //需要加入到IOC 容器
    public class SmartAnimalAspect {
        //=====AOP-切入点表达式重用start ======
        /*
         * 这样定义的一个切入点表达式,就可以在其它地方直接使用
         */
        @Pointcut(value = "execution(public float
                com.wyxedu.spring.aop.joinpoint.SmartDog.getSum(float, float))")
        public void myPointCut() {
        }
        // @Before(value="execution(public float com.wyxedu.spring.aop.joinpoint.SmartDog.getSum(float, float))")
        @Before(value = "myPointCut()")
        public void showBeginLog(JoinPoint joinPoint) { //前置方法
            //得到方法的签名
            // 调用前置通知对应的方法签名: float com.wyxedu.spring.aop.joinpoint.SmartDog.getSum(float, float)
            Signature signature = joinPoint.getSignature();
            //得到方法名.
            String method_name = signature.getName();
            //得到参数
            Object[] args = joinPoint.getArgs();
            System.out.println("前置通知" + "--调用的方法是" + method_name + "--参数是
                    --" + Arrays.asList(args));
        }
        //@After(value = "execution(public float com.wyxedu.spring.aop.joinpoint.SmartDog.getSum(float, float))")
        @After(value = "myPointCut()")
        public void showFinallyEndLog() {
            System.out.println("最终通知-- AOP-切入点表达式重用");
        }
        /**
         * returning = "res", Object res 名称保持一致
         * @param joinPoint
         * @param res 调用getSum() 返回的结果
         */
        @AfterReturning(value = "execution(public float com.wyxedu.spring.aop.joinpoint.SmartDog.getSum(float, float))",
        returning = "res")
        public void showSuccessEndLog(JoinPoint joinPoint, Object res) {
            System.out.println("返回通知" + "--结果是--" + res);
        }
        @AfterThrowing(value = "execution(public float com.wyxedu.spring.aop.joinpoint.SmartDog.getSum(float, float))", throwing = "throwable")
        public void showExceptionLog(JoinPoint joinPoint, Throwable throwable) {
            System.out.println("异常通知-- 异常信息--" + throwable);
        }
        //=====AOP-切入点表达式重用end ======
测试AopJoinPointTest.java

image-20230805173406696

🥰AOP-切面优先级问题

😉应用实例

● 切面优先级问题:

如果同一个方法,有多个切面在同一个切入点切入,那么执行的优先级如何控制.

● 基本语法:

@order(value=n) 来控制 n值越小,优先级越高

● 案例说明

  1. 创建\SmartAnimalAspect2.java

@Aspect //表示这个类是一个切面类
@Order(value = 2)
@Component //需要加入IOC 容器
public class SmartAnimalAspect2 {
    /*
     * 这样定义的一个切入点表达式,就可以在其它地方直接使用
     */
    @Pointcut(value = "execution(public float com.wyxedu.spring.aop.joinpoint.SmartDog.getSum(float, float))")
    public void myPointCut() {
    }
    @Before(value = "myPointCut()")
    public void showBeginLog(JoinPoint joinPoint) {
        System.out.println("前置通知SmartAnimalAspect2 showBeginLog");
    }
    @AfterReturning(value = "myPointCut()", returning = "res")
    public void showSuccessEndLog(JoinPoint joinPoint, Object res) {
        System.out.println("返回通知SmartAnimalAspect2 showSuccessEndLog");
    }
    @AfterThrowing(value = "myPointCut()", throwing = "throwable")
    public void showExceptionLog(JoinPoint joinPoint, Throwable throwable) {
        System.out.println("异常通知SmartAnimalAspect2 showExceptionLog");
    }
    @After(value = "myPointCut()")
    public void showFinallyEndLog() {
        System.out.println("最终通知SmartAnimalAspect2 showFinallyEndLog");
    }
}
  1. 修改SmartAnimalAspect.java

image-20230805173922064

测试

image-20230805174346020

  1. 如何理解输出的信息顺序,类似Filter 的过滤链式调用机制. (示意图-就比较清楚了.)

💥注意事项和细节说明

如何理解输出的信息顺序,类似 Filter 的过滤链式调用机制. (示意图-就比较清楚了.)

  1. 不能理解成:优先级高的每个消息通知都先执行,这个和方法调用机制(和 Filter 过滤器链式调用类似)

  2. 如何理解执行顺序

img

😊AOP-基于 XML 配置 AOP

● 基本说明:

前面我们是通过注解来配置 aop 的,在 spring 中,我们也可以通过 xml 的方式来配置 AOP

😀代码示例

实现步骤
  1. 创建包com.wyxedu.spring.aop.xml , SmartAnimalable.java 和SmartDog.java 从其它包拷贝即可
  2. 创建SmartAnimalAspect.java , 从com.wyxedu.spring.aop 包拷贝,并去掉所有注解
具体实现
  1. 创建接口SmartAnimalable
public interface SmartAnimalable {
    //求和
    float getSum(float i, float j);
    //求差
    float getSub(float i, float j);
}
  1. 创建SmartAnimalAspect类注意没有注解
public class SmartAnimalAspect {


    public void showBeginLog(JoinPoint joinPoint) {
        //通过连接点对象joinPoint 可以获取方法签名
        Signature signature = joinPoint.getSignature();
        System.out.println("SmartAnimalAspect-XML配置-切面类showBeginLog()[使用的myPointCut()]-方法执行前-日志-方法名-" + signature.getName() + "-参数 "
                + Arrays.asList(joinPoint.getArgs()));
    }

    public void showSuccessEndLog(JoinPoint joinPoint, Object res) {
        Signature signature = joinPoint.getSignature();
        System.out.println("SmartAnimalAspect-XML配置-切面类showSuccessEndLog()-方法执行正常结束-日志-方法名-" + signature.getName() + " 返回的结果是=" + res);
    }


    public void showExceptionLog(JoinPoint joinPoint, Throwable throwable) {
        Signature signature = joinPoint.getSignature();
        System.out.println("SmartAnimalAspect-XML配置-切面类showExceptionLog()-方法执行异常-日志-方法名-" + signature.getName() + " 异常信息=" + throwable);
    }

    public void showFinallyEndLog(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println("SmartAnimalAspect-XML配置-切面类showFinallyEndLog()-方法最终执行完毕-日志-方法名-" + signature.getName());
    }
}
  1. 创建SmartDog注意没有注解
public class SmartDog implements SmartAnimalable {
    @Override
    public float getSum(float i, float j) {
        float result = i + j;
        //result = 1 / 0; //模拟一个算术异常
        System.out.println("方法内部打印result = " + result);
        return result;
    }

    @Override
    public float getSub(float i, float j) {
        float result = i - j;
        System.out.println("方法内部打印result = " + result);
        return result;
    }
}
  1. xml配置
<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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">

    <!--使用XML配置,完成AOP编程-->
    <!--配置一个切面类对象-bean-->
    <bean class="com.spring.aop.xml.SmartAnimalAspect" id="smartAnimalAspect"/>
    <!--配置一个SmartDog对象-bean-->
    <bean class="com.spring.aop.xml.SmartDog" id="smartDog"/>
    <!--配置切面类, 细节一定要引入 xmlns:aop-->
    <aop:config>
        <!--配置切入点-->
        <aop:pointcut id="myPointCut" expression="execution(public float com.spring.aop.xml.SmartDog.getSum(float, float)))"/>
        <!--配置切面的前置,返回, 异常, 最终通知-->
        <aop:aspect ref="smartAnimalAspect" order="10">
            <!--配置前置通知-->
            <aop:before method="showBeginLog" pointcut-ref="myPointCut"/>
            <!--返回通知-->
            <aop:after-returning method="showSuccessEndLog" pointcut-ref="myPointCut" returning="res"/>
            <!--异常通知-->
            <aop:after-throwing method="showExceptionLog" pointcut-ref="myPointCut" throwing="throwable"/>
            <!--最终通知-->
            <aop:after method="showFinallyEndLog" pointcut-ref="myPointCut"/>
            <!--配置环绕通知-->
            <!--<aop:around method=""/>-->
        </aop:aspect>
    </aop:config>
</beans>

测试

public class AopAspectjXMLTest {

    @Test
    public void testAspectByXML() {

        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans09.xml");
        SmartAnimalable smartAnimalable =
                ioc.getBean(SmartAnimalable.class);

        smartAnimalable.getSum(10, 2);
    }

}

😄总结

本文详细的讲解了spring AOP的切入表达式重用和AOP优先级问题以及AOP怎么实现基于xml的aop

😍Spring-AOP系类文章
第一篇-> Spring-AOP的基本介绍以及通过先动态代理方式实现

第二篇-> Spring-动态代理深入了解

第三篇-> 再次分析-提出 Spring AOP-真正的AOP

第四篇-> spring-aop的切入表达式和JoinPoint的使用以及怎么返回通知获取结果和在异常通知中获取异常还有环绕通知

😁热门专栏推荐
想学习vue的可以看看这个
java基础合集
数据库合集
redis合集
nginx合集
linux合集
等等等还有许多优秀的合集在主页等着大家的光顾感谢大家的支持

🤔欢迎大家加入我的社区 尘觉社区

文章到这里就结束了,如果有什么疑问的地方请指出,诸佬们一起来评论区一起讨论😁
希望能和诸佬们一起努力,今后我们一起观看感谢您的阅读🍻
如果帮助到您不妨3连支持一下,创造不易您们的支持是我的动力🤞

更多推荐

【python零基础入门学习】python进阶篇之时间表示方法和异常处理以及linux系统的os模块执行shell命令以及记账程序编写教学(一)

本站以分享各种运维经验和运维所需要的技能为主《python零基础入门》:python零基础入门学习《python运维脚本》:python运维脚本实践《shell》:shell学习《terraform》持续更新中:terraform_Aws学习零基础入门到最佳实战《k8》暂未更新《docker学习》暂未更新《ceph学习

从统计语言模型到预训练语言模型---神经网络语言模型

随着神经网络的发展,神经语言模型(NeuralNetworkLanguageModels,NNLM)展现出了比统计语言模型更强的学习能力,克服了N-gram语言模型的维度灾难,并且大大提升了传统语言模型的性能。神经网络先进的结构使其能有效的建模长距离上下文依赖,以词向量(WordEmbedding)为代表的分布式表示的

Windows 下 MySQL 8.1 图形化界面安装、配置详解

首先我们下载安装包官方下载链接:MySQL::BeginYourDownload网盘链接:https://pan.baidu.com/s/1FOew6-93XpknB-bYDhDYPw提取码:brys外网下载慢的同学可以使用上述网盘链接下载完成后我们双击安装包即可进入安装界面点击next勾选同意协议,点击next这里选

Vulnhub系列靶机-Hackadmeic.RTB1

文章目录Vulnhub系列靶机-Hackadmeic.RTB11.信息收集1.1主机扫描1.2端口扫描1.3目录爆破2.漏洞探测3.漏洞利用3.1反弹Shell4.内核提权Vulnhub系列靶机-Hackadmeic.RTB11.信息收集1.1主机扫描arp-scan-l1.2端口扫描nmap-A-p-192.168.

shell练习(grep,sed,awk,距离生日天数的计算,累加求和,文件操作和文本操作)

最近在学习shell脚本编写,把几个练习记录一下。包括grep,sed,awk,距离生日天数的计算,累加求和,文件操作和文本操作。grep相关练习找到/huiluczp下含有星号的文件与内容,主要是利用find来找到所有待查的文件作为参数给grep。echo'找到/huiluczp下含有星号的文件与内容'grep'\*

网络安全(黑客)自学

目录:一、什么是网络安全二、怎样规划网络安全三、网络安全的知识多而杂,怎么科学合理安排?1、基础阶段2、渗透阶段3、安全管理(提升)这一阶段主要针对已经从事网络安全相关工作需要提升进阶成管理层的岗位。如果你只学习参加工程师方面的岗位,这一阶段可学可不学。4、提升阶段(提升)1、Web安全相关概念(2周)2、熟悉渗透相关

vue3+element-plus权限控制实现(el-tree父子级不关联情况处理)

文章目录前言一、遇到的交互场景el-tree中check-strictly属性二、处理父级的半选中以及选中交互el-treecheck,check-change事件编辑进来,父级的半选状态处理总结前言在开发后台管理系统的时候,用户的权限控制是一个常见的需求。这里需要探讨下按钮的级别的权限控制,以及实现中使用elemen

【深度学习】树莓派Zero w深度学习模型Python推理

在机器学习开发过程中,当模型训练好后,接下来就要进行模型推理了,根据部署环境可分为三类场景:边缘计算:一般指手机,嵌入式设备,直接在数据生成的设备上进行推理,因为能避免将采集到的数据上传到云端,所以实时性非常好。端计算:介于云和边缘设备之间的计算平台,个人PC可以归为这一类。云计算:指云计算平台,具有强大的计算和存储能

Redis 持久化之 RDB 与 AOF 详解

Redis持久化我们知道Redis的数据是全部存储在内存中的,如果机器突然GG,那么数据就会全部丢失,因此需要有持久化机制来保证数据不会因为宕机而丢失。Redis为我们提供了两种持久化方案,一种是基于快照,另外一种是基于AOF日志。接下来就来了解一下这两种方案。操作系统与磁盘首先我们需要知道Redis数据库在持久化中扮

为何学linux及用处

目前企业使用的操作系统无非就是国产类的,windows和linux类。我们要提升自己的技能,需要学习这两款。我记得在大学时期,学习过windows以及linux,但当时觉得又不常用,就学的模棱两可。毕业之后,你会发现,其实这两种操作系统是很主流的。为什么学?下面就是一些工作中遇到的例子分享一下。我记得在企业中有次遇到数

Linux高性能服务器编程 学习笔记 第五章 Linux网络编程基础API

我们将从以下3方面讨论Linux网络API:1.socket地址API。socket最开始的含义是一个IP地址和端口对(ip,port),它唯一表示了使用TCP通信的一端,本书称其为socket地址。2.socket基础API。socket的主要API都定义在sys/socket.h头文件中,包括创建socket、命名

热文推荐