Spring API 接口和自定义类来实现AOP(Spring学习笔记十)

news2024/10/6 16:17:59

1、什么是AOP

全称是 Aspect Oriented Programming 即:面向切面编程。是OOP(面向对象编程)的延续,也是Spring框架中的一个重要内容,是函数式编程的一种衍生泛型。简单的说他就是把我们程序重复的代码抽取出来,在需要执行的时候使用动态代理技术在不修改源码的基础上,对我们的已有方法进行增强。

AOC在Spring中的作用

2、使用Spring实现AOP

方式一:使用Spring的API接口来做

1、导入AOP支持
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.19</version>
    <scope>runtime</scope>
</dependency>
2、创建测试类

抽象类:

package com.li.service;

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}

抽象类实现:

package com.li.service;

public class UserServiceImp implements UserService{
    @Override
    public void add() {
        System.out.println("增加");
    }

    @Override
    public void delete() {
        System.out.println("删除");
    }

    @Override
    public void update() {
        System.out.println("修改");
    }

    @Override
    public void select() {
        System.out.println("查询");
    }
}
 3、创建切面类,编写切面方法

方法一:实现MethodBeforeAdvice  在切入点之前切入方法

package com.li.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
    //实现MethodBeforeAdvice会在方法执行前运行
    //method: 要执行的目标对象的方法
    //args:参数
    //target:目标对象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行了,"+"参数是:"+args.getClass().getName());
    }
}

方法二:实现 AfterReturningAdvice  在切入点后加入切面方法

package com.li.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    //实现AfterReturningAdvice会在方法执行后执行
    //method: 要执行的目标对象的方法
    //args:参数
    //target:目标对象
    //返回值:returnValue
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"执行了:"+method.getName()+"方法,方法参数是:"+args.getClass().toString()+"返回值是:"+returnValue);
    }
}
4、创建配置文件applicationContext.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: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/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--注册Bean-->
    <bean id="UserService" class="com.li.service.UserServiceImp"/>
    <bean id="log" class="com.li.log.Log"/>
    <bean id="AfterLog" class="com.li.log.AfterLog"/>

    <!--使用原生Spring api接口-->
    <!--配置aop: 需要先导入aop的约束 xmlns:aop="http://www.springframework.org/schema/aop"    http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd"-->
    <aop:config>
    <!--需要一个切入点就是我们需要在什么地方去执行我们的Spring的方法-->
    <!--id后面接你想给切入点取的名字 expression需要接表达式,表达式是死的:execution(* 要执行的位置.*(..))    这里的.*表示位置下的所有方法 (..)表示可以有任意的参数-->
        <aop:pointcut id="poi" expression="execution(* com.li.service.UserServiceImp.*(..))"/>

    <!--执行环绕增强-->
        <aop:advisor advice-ref="log" pointcut-ref="poi"/>
        <aop:advisor advice-ref="AfterLog" pointcut-ref="poi"/>
    </aop:config>
</beans>
5、测试类测试
package com.li.log;

import com.li.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestGo {
    @Test
    public void test1(){
        //通过配置文件获取ClassPath
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //根据抽象类获取Bean
        UserService user = context.getBean("UserService", UserService.class);
        //调用方法测试
        user.add();

    }
}

 测试结果

方式二:使用自定义类来实现Aop

创建自定义类和方法

package com.li.diy;

public class Diy {
    public void before(){
        System.out.println("====切面之前执行====");
    }

    public void after(){
        System.out.println("====切面之后执行====");
    }
}

配置applicationContext.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: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/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--注册Bean-->
    <bean id="UserService" class="com.li.service.UserServiceImp"/>

    <!--方式二: 通过自定义方法来Aop-->
    <!--注册Bean-->
    <bean id="diy" class="com.li.diy.Diy"/>

    <!--配置aop: 需要先导入aop的约束 xmlns:aop="http://www.springframework.org/schema/aop"    http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd"-->
    <aop:config>

    <!--自定义切面,ref:接需要引用类的bean id-->
        <aop:aspect ref="diy">

            <!--id后面接你想给切入点取的名字 expression需要接表达式,表达式是死的:execution(* 要执行的位置.*(..))    这里的.*表示位置下的所有方法 (..)表示可以有任意的参数-->
            <aop:pointcut id="point" expression="execution(* com.li.service.UserServiceImp.*(..))"/>

            <!--通知 method后接前面类中自定义的方法名 pointcut-ref后接切入点的id -->
            <aop:before method="before" pointcut-ref="point"/>

            <aop:after method="after" pointcut-ref="point"/>


        </aop:aspect>

    </aop:config>
</beans>

创建测试类

package com.li.log;

import com.li.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestGo {
    @Test
    public void test1(){
        //通过配置文件获取ClassPath
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //根据抽象类获取Bean
        UserService user = context.getBean("UserService", UserService.class);
        //调用方法测试
        user.add();

    }
}

 测试结果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1564698.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

MySQL-执行计划、explain关键字查询执行计划以及explain案例分析

一、执行计划 MySQL 的执行计划&#xff08;Execution Plan&#xff09;是数据库查询优化器生成的一种指导性信息&#xff0c;它描述了 MySQL 执行查询时所采用的具体执行策略。执行计划通常由 MySQL 的查询优化器根据查询语句、表结构、索引等信息进行生成&#xff0c;并且用…

基于Docker for Windows部署ChatGPT-Next-Web

基于Docker for Windows部署ChatGPT-Next-Web 项目地址安装Docker for Windows部署项目参数讲解参数示例 运行 项目地址 https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web 安装Docker for Windows 官网地址&#xff1a;https://www.docker.com/ 下拉找到Download 选择W…

Error: TF_DENORMALIZED_QUATERNION: Ignoring transform forchild_frame_id

问题 运行程序出现&#xff1a; Error: TF_DENORMALIZED_QUATERNION: Ignoring transform for child_frame_id “odom” from authority “unknown_publisher” because of an invalid quaternion in the transform (0.0 0.0 0.0 0.707) 主要是四元数没有归一化 Eigen::Quatern…

CentOS 7 下离线安装RabbitMQ教程

CentOS 7 下安装RabbitMQ教程一、做准备&#xff08;VMWare 虚拟机上的 CentOS 7 镜像 上安装的&#xff09; &#xff08;1&#xff09;准备RabbitMQ的安装包&#xff08;rabbitmq-server-3.8.5-1.el7.noarch&#xff09;下载地址mq https://github.com/rabbitmq/rabbitmq-se…

使用C++调用讯飞星火API的详细指南

正文&#xff1a; 科大讯飞是中国领先的人工智能公司&#xff0c;其讯飞星火API为开发者提供了丰富的接口和服务&#xff0c;支持各种语音和语言技术的应用。下面是使用C接入讯飞星火API的步骤和代码示例。 步骤一&#xff1a;注册账号并创建应用 首先&#xff0c;您需要访问科…

leetCode刷题 25.K 个一组翻转链表

目录 1.思路&#xff1a; 2.解题方法&#xff1a; 3.复杂度&#xff1a; 4.Code 题目&#xff1a; 给你链表的头节点 head &#xff0c;每 k 个节点一组进行翻转&#xff0c;请你返回修改后的链表。 k 是一个正整数&#xff0c;它的值小于或等于链表的长度。如果节点总数不…

Medium 级别存储型 XSS 攻击演示(附链接)

环境准备 DVWA 靶场https://eclecticism.blog.csdn.net/article/details/135834194?spm1001.2014.3001.5502 测试 打开页面 输入正常值观察 一样的&#xff0c;用户名做了长度限制 更改后输入 HTML 标签语句测试 一样没有做过滤&#xff0c;接下来试试 JS 恶意代码 发现只…

Linux(05) Debian 系统修改主机名

查看主机名 方法1&#xff1a;hostname hostname 方法2&#xff1a;cat etc/hostname cat /etc/hostname 如果在创建Linux系统的时候忘记修改主机名&#xff0c;可以采用以下的方式来修改主机名称。 修改主机名 注意&#xff0c;在linux中下划线“_”可能是无效的字符&…

zabbix图表时间与服务器时间不一致问题

部署完zabbix后&#xff0c;有时候会发现zabbix服务器的时间明明是对的&#xff0c;但是图标的时间不对&#xff0c;通过以下的配置可以快速解决。 登录zabbix-nginx容器 docker exec -u root -it docker-compose-zabbix-zabbix-web-nginx-mysql-1 bash修改php配置文件 vi /e…

合并两个单链表

归纳编程学习的感悟&#xff0c; 记录奋斗路上的点滴&#xff0c; 希望能帮到一样刻苦的你&#xff01; 如有不足欢迎指正&#xff01; 共同学习交流&#xff01; &#x1f30e;欢迎各位→点赞 &#x1f44d; 收藏⭐ 留言​&#x1f4dd; 但行前路&#xff0c;不负韶华&#…

【Mysql】一文解读【事务】-【基本操作/四大特性/并发事务问题/事务隔离级别】

前言 大家好吖&#xff0c;欢迎来到 YY 滴MySQL系列 &#xff0c;热烈欢迎&#xff01; 本章主要内容面向接触过C Linux的老铁 主要内容含&#xff1a; 欢迎订阅 YY滴C专栏&#xff01;更多干货持续更新&#xff01;以下是传送门&#xff01; YY的《C》专栏YY的《C11》专栏YY的…

使用 FinalShell 进行远程连接(ssh 远程连接 Linux 服务器)

目录 前言 基本使用教程 新建远程连接 连接主机 自定义命令 路由追踪 前言 后端开发&#xff0c;必然需要和服务器打交道&#xff0c;部署应用&#xff0c;排查问题&#xff0c;查看运行日志等等。一般服务器都是集中部署在机房中&#xff0c;也有一些直接是云服务器&am…

Spring Boot接收从前端传过来的数据常用方式以及处理的技巧

一、params 传参 参数是会拼接到url后面的请求 场景规范:url后面的key值<=3个参数的时候,使用params 传参 支持的请求方式:get(正规的是get方式)、post 都行 例如: http://localhost:8080/simpleParam?name=Tom&age=10 在postman里面的体现为 后端接收的接口…

AKF扩展立方体和AKF可用性立方体

很多人知道AKF扩展立方体是从《架构即未来》这本书开始。实际上akfpartners官方写过4篇关于AKF扩展立方体的文章&#xff0c;还有一篇介绍AKF可用性立方体。akfpartners官方在高可用、扩展性方面有很多专业技术文章&#xff0c;建议有空就翻翻看。 AKF扩展立方体和AKF可用性立方…

AAC相关知识

一、AAC音频格式种类有哪些 AAC音频格式是一种由MPEG-4标准定义的有损音频压缩格式。AAC包含两种封装格式 ADIF&#xff08;Audio Data Interchange Format音频数据交换格式&#xff09;和ADTS&#xff08;Audio Data transport Stream音频数据传输流&#xff09;。 ADIF 特点…

最简单的 AAC 音频码流解析程序

最简单的 AAC 音频码流解析程序 最简单的 AAC 音频码流解析程序原理源程序运行结果下载链接参考 最简单的 AAC 音频码流解析程序 参考雷霄骅博士的文章&#xff1a;视音频数据处理入门&#xff1a;AAC音频码流解析 本文中的程序是一个AAC码流解析程序。该程序可以从AAC码流中…

Lafida多目数据集实测

Lafida 数据集 paper&#xff1a;J. Imaging | Free Full-Text | LaFiDa—A Laserscanner Multi-Fisheye Camera Dataset 官网数据&#xff1a;https://www.ipf.kit.edu/english/projekt_cv_szenen.php 官网&#xff1a;KIT-IPF-Software and Datasets - LaFiDa 标定数据下载&…

解析Flutter应用在iOS环境中的性能优化技巧

本文探讨了使用Flutter开发的iOS应用能否上架&#xff0c;以及上架的具体流程。苹果提供了App Store作为正式上架渠道&#xff0c;同时也有TestFlight供开发者进行内测。合规并通过审核后&#xff0c;Flutter应用可以顺利上架。但上架过程可能存在一些挑战&#xff0c;因此可能…

Javascript/Node.JS中如何用多种方式避免属性为空(cannot read property of undefined ERROR)

>>>>>>问题 "cannot read property of undefined" 是一个常见的 JavaScript 错误&#xff0c;包含我在内很多人都会遇到&#xff0c;表示你试图访问一个未定义&#xff08;undefined&#xff09;对象的属性。这通常是因为你在访问一个不存在的对象…

【QT+QGIS跨平台编译】056:【pdal_kazhdan+Qt跨平台编译】(一套代码、一套框架,跨平台编译)

点击查看专栏目录 文章目录 一、pdal_kazhdan介绍二、pdal下载三、文件分析四、pro文件五、编译实践一、pdal_kazhdan介绍 pdal_kazhdan 是 PDAL(Point Data Abstraction Library)相关的 Kazhdan 算法的实现。PDAL 是一个用于处理和分析点云数据的开源库,而 Kazhdan 算法通常…