AOP实现方式-P20,21,22

news2025/7/19 11:21:18

 项目的包:

 pom依赖导入有关aop的包:

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
    </dependencies>

代理类交给spring来实现。


UserService:

package com.Li.service;

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

UserServiceImpl:

package com.Li.service;

public class UserServiceImpl 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("查询了一个用户!");
    }
}

log:

package com.Li.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;
//前置日志
public class log implements MethodBeforeAdvice {
    //method: 要执行的目标对象的方法
    //args:参数
    //target:目标对象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}

AfterLog:

package com.Li.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;
//后置日志
public class AfterLog implements AfterReturningAdvice {

    //returnValue: 返回值.由于是AfterReturning,所以有返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
    }
}

方式一:(使用spring的接口实现动态代理类)

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.UserServiceImpl"/>
    <bean id="log" class="com.Li.log.log"/>
    <bean id="afterLog" class="com.Li.log.AfterLog"/>

    <!--方式一:使用原生Spring API接口-->
    <!--配置aop:需要导入aop的约束-->
    <aop:config>
        <!--切入点:expression:表达式,execution(要执行的位置!)在那个类下区执行pointcut,就是用表达式定位。.*表示该类下的所有方法-->
        <aop:pointcut id="pointcut" expression="execution(* com.Li.service.UserServiceImpl.*(..))"/>

        <!--执行环绕增加!
            将log切入类切入到id为pointcut里
            将afterLog切入类切入到id为pointcut里
        -->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

MyTest:(测试)(不变)

import com.Li.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是接口:注意点
        UserService userService = context.getBean("userService", UserService.class);

        userService.add();


    }
}


方式二(主要是切面定义)

增加一个类:

DiyPointCut:

package com.Li.diy;

public class DiyPointCut {

    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.UserServiceImpl"/>
    <bean id="log" class="com.Li.log.log"/>
    <bean id="afterLog" class="com.Li.log.AfterLog"/>

    <!--方式一:使用原生Spring API接口-->
    <!--配置aop:需要导入aop的约束-->
    <!--<aop:config>-->
    <!--    &lt;!&ndash;切入点:expression:表达式,execution(要执行的位置!)在那个类下区执行pointcut,就是用表达式定位。.*表示该类下的所有方法&ndash;&gt;-->
    <!--    <aop:pointcut id="pointcut" expression="execution(* com.Li.service.UserServiceImpl.*(..))"/>-->

    <!--    &lt;!&ndash;执行环绕增加!-->
    <!--        将log切入类切入到id为pointcut里-->
    <!--        将afterLog切入类切入到id为pointcut里-->
    <!--    &ndash;&gt;-->
    <!--    <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>-->
    <!--    <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>-->
    <!--</aop:config>-->



    <!--方式二:自定义类-->
    <bean id="diy" class="com.Li.diy.DiyPointCut"/>

    <aop:config>
        <!--自定义切面,ref 要引用的类-->
        <!--切面,就是类-->
        <aop:aspect ref="diy">
            <!--切入点-->
            <aop:pointcut id="point" expression="execution(* com.Li.service.UserServiceImpl.*(..))"/>
            <!--通知(就是方法)-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>



</beans>

测试(代码不变直接测试):

 


利用注解开发:

 

AnnotationPointCut:

package com.Li.diy;

//方式三:使用注解方式实现AOP

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect//标记这个类是一个切面
public class AnnotationPointCut {

    @Before("execution(* com.Li.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("=====方法执行前=====");
    }

    @After("execution(* com.Li.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("=====方法执行后=====");
    }

}

 applicationContext.xml:

    <!--方式三-->
    <bean id="annotationPointCut" class="com.Li.diy.AnnotationPointCut"/>
    <!--开启注解支持-->
    <aop:aspectj-autoproxy/>

直接测试

 

三种方式都可以实现AOP。第一种全面,第二种便于理解,第三种方便。你可以自己选择自己喜欢的方式。

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

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

相关文章

【PyTorch】Training Model

文章目录七、Training Model1、模型训练2、GPU训练2.1 .cuda()2.2 .to(device)2.3 Google Colab3、模型验证七、Training Model 1、模型训练 以CIFAR10数据集为例&#xff1a; import torchvision from torch.utils.data import DataLoader from torch.utils.tensorboard im…

【算法】2022第五届“传智杯”全国大学生计算机大赛(练习赛)

【参考&#xff1a;第五届“传智杯”全国大学生计算机大赛&#xff08;练习赛&#xff09; - 洛谷 | 计算机科学教育新生态】 练习赛满分程序&#xff08;多语言&#xff09;&#xff1a;https://www.luogu.com.cn/paste/fi60s4yu CPU一秒大概运行 10810^8108 次&#xff0c;…

年产10万吨环氧树脂车间工艺设计

目 录 摘 要 1 ABSTRACT 2 1 绪论 3 1.1环氧树脂的基本性质 3 1.2 环氧树脂的特点和用途 3 1.3环氧树脂发展的历史、现状及趋势 3 1.3.1环氧树脂的发展历史 4 1.3.2环氧树脂的生产现状 4 1.3.3 环氧树脂的发展趋势 5 1.4本设计的目的、意义及内容 5 1.4.1本设计的目的 5 1.4.2…

Matlab顶级期刊配色工具Rggsci

颜色搭配是一件非常让人头疼的事情。 一方面&#xff0c;如果忽视了配色&#xff0c;就好像是做菜没放盐&#xff0c;总会感觉少些味道。 另一方面&#xff0c;如果太注重配色&#xff0c;又感觉不是很有必要&#xff0c;毕竟数据结果好看才是第一位的。 想要平衡两者&#…

18.4 嵌入式指针概念及范例、内存池改进版

一&#xff1a;嵌入式指针&#xff08;embedded pointer&#xff09; 1、嵌入式指针概念 一般应用在内存池相关的代码中&#xff0c;成功使用嵌入式指针有个前提条件&#xff1a;&#xff08;类A对象的sizeof必须不小于4字节&#xff09; 嵌入式指针工作原理&#xff1a;借用…

文华财经期货K线多周期画线技术,多重短线技术共振通道线指标公式——多周期主图自动画线

期货指标公式是通过数学逻辑角度计算而来&#xff0c;仅是期货分析环节中的一个辅助工具。期货市场具有不确定性和不可预测性的&#xff0c;请正常对待和使用指标公式! 期货指标公式信号本身就有滞后性&#xff0c;周期越大&#xff0c;滞后性越久。指标公式不是100%稳赚的工具…

cocos2dx创建工程并在androidstudio平台编译

本文主要是通过androidstudio进行编译运行cocos2dx工程。 前置条件&#xff1a; 1&#xff1a;androidstudio已经下载并安装。 2&#xff1a;cocos2dx已经下载并打开。 这里androidstudio使用2021.3.1版本&#xff0c;cocos2dx使用4.0版本。 第一步&#xff0c;首先安装py…

Hive之数据类型和视图

Hive系列 第八章 数据类型和视图 8.1 数据类型 8.1.1 原子数据类型 &#xff08;其实上图中有一点错误&#xff0c;大家可以找找看&#xff09; 说明&#xff1a; 1、Hive 支持日期类型(老版本不支持)&#xff0c;在 Hive 里日期一般都是用字符串来表示的&#xff0c;而常用…

STC 51单片机40——汇编语言 串口 接收与发送

实际运行&#xff0c;正常 ; 仿真时&#xff0c;单步运行&#xff0c;记得设置虚拟串口数据【仿真有问题&#xff0c;虚拟串口助手工作不正常&#xff01;】 ORG 0000H MOV TMOD ,#20H ;定时器1&#xff0c;工作方式2&#xff0c;8位重装载 MOV TH1,#0FDH ; 波特率…

智慧酒店解决方案-最新全套文件

智慧酒店解决方案-最新全套文件一、建设背景为什么要建设智慧酒店一、智慧酒店功能亮点 &#xff1a;二、智慧酒店八大特色&#xff1a;二、建设思路三、建设方案四、获取 - 智慧酒店全套最新解决方案合集一、建设背景 为什么要建设智慧酒店 一、智慧酒店功能亮点 &#xff1…

mysql-8.0.31-macos12-x86_64记录

常用的命令 停止MySQL服务 : sudo /usr/local/mysql/support-files/mysql.server stop 启动MySQL服务 : sudo /usr/local/mysql/support-files/mysql.server start 重启MySQL服务 : sudo /usr/local/mysql/support-files/mysql.server restart 修改mysql密码 关闭mysql服务…

Qt5开发从入门到精通——第十二篇二节(Qt5 事件处理及实例——多线程控制、互斥量、信号量、线程等待与唤醒)

提示&#xff1a;欢迎小伙伴的点评✨✨&#xff0c;相互学习c/c应用开发。&#x1f373;&#x1f373;&#x1f373; 博主&#x1f9d1;&#x1f9d1; 本着开源的精神交流Qt开发的经验、将持续更新续章&#xff0c;为社区贡献博主自身的开源精神&#x1f469;‍&#x1f680; 文…

【C语言数据结构】带头节点与不带头节点的单链表头插法对比

前言 近期在学习STM32代码框架的过程中&#xff0c;老师使用链表来注册设备&#xff0c;发现使用了不带头节点的单链表&#xff0c;注册时使用头插法。之前在本专题整理学习过带头节点的单链表&#xff0c;因此本文整理对比一下两种方式的头插法区别&#xff0c;具体实现在次&…

html表白代码

目录一.引言二.表白效果展示1.惊喜表白2.烟花表白3.玫瑰花表白4.心形表白5.心加文字6.炫酷的特效一.引言 我们可以用一下好看的网页来表白&#xff0c;下面就有我觉得很有趣的表白代码。评论直接找我要源码也行。 下载整套表白文件 二.表白效果展示 1.惊喜表白 2.烟花表白 源码…

【TS】泛型以及多个泛型参数

泛型 给函数或者属性定义类型的时候&#xff0c;类型是固定的&#xff0c;当业务发生变动时可能不好维护&#xff0c;例如&#xff1a;函数类型固定为string,后续需求更改不好维护&#xff0c;比如需要传入number类型&#xff0c;那么这个函数就不适用了 function add( val :…

数学题类英语作文

最近我看到过这样一道英语作文题&#xff0c;这类英语作文题很少见&#xff0c;但也有必要讲一讲怎么写。 简化题意&#xff1a;帮Peter完成一下一道题&#xff1a; f(x)ax2−(a6)x3ln⁡xf(x)ax^2-(a6)x3\ln xf(x)ax2−(a6)x3lnx &#xff08;1&#xff09;讨论当a1a1a1时&am…

CMake中file的使用

CMake中的file命令用于文件操作&#xff0c;其文件格式如下&#xff1a;此命令专用于需要访问文件系统的文件和路径操作 Readingfile(READ <filename> <variable>[OFFSET <offset>] [LIMIT <max-in>] [HEX])file(STRINGS <filename> <variab…

Java8-新特性及Lambda表达式

1、Java8新特性内容概述 1.1、简介 Java 8(又称为jdk1.8)是Java语言开发的一个主要版本 Java 8是oracle公司于2014年3月发布&#xff0c;可以看成是自Java 5以来最具革命性的版本。Java 8为Java语言、编译器、类库、开发工具与JVM带来了大量新特性 1.2、新特性思维导图总结 1.…

JS中数组随机排序实现(原地算法sort/shuffle算法)

&#x1f431;个人主页&#xff1a;不叫猫先生 &#x1f64b;‍♂️作者简介&#xff1a;专注于前端领域各种技术&#xff0c;热衷分享&#xff0c;期待你的关注。 &#x1f4ab;系列专栏&#xff1a;vue3从入门到精通 &#x1f4dd;个人签名&#xff1a;不破不立 目录一、原地…

代码随想录刷题|LeetCode 70. 爬楼梯(进阶) 322. 零钱兑换 279.完全平方数 139.单词拆分

目录 70. 爬楼梯 &#xff08;进阶&#xff09; 思路 爬楼梯 1或2步爬楼梯 多步爬楼梯 322. 零钱兑换 思考 1、确定dp数组及其含义 2、确定递推公式 3、初始化dp数组 4、确定遍历顺序 零钱兑换 先遍历物品&#xff0c;再遍历背包 先遍历背包&#xff0c;再遍历物品 279.完全平方…