(Spring笔记)AspectJ前置通知——@Before切面开发

2024-03-26 07:50

本文主要是介绍(Spring笔记)AspectJ前置通知——@Before切面开发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一、AspectJ开发环境

        (1)pom依赖

        (2)Spring配置文件

二、前置通知切面开发

        (1)前置通知业务接口

        (2)前置通知业务接口实现类

        (3)前置通知切面类

        (4)applicationContext.xml配置文件

三、junitl测试

        (1)运行testBefore01

        (2)运行testBefore02

四、总结


 本章学习源码Github地址:https://github.com/GuiZhouAndroid/MySpringAllProject/tree/master/SpringDemo09_AspectJ/src/main/java/com/dhrj/java/zsitking/before


一、AspectJ开发环境

        (1)pom依赖

    <dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><!-- Spring依赖配置 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.20</version></dependency><!-- AspectJ依赖配置 --><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.3.20</version></dependency></dependencies><build><resources><!-- 扫描加载java目录下指定后缀资源 --><resource><directory>src/main/java</directory><includes><include>**/*.xml</include><include>**/*.properties</include></includes></resource><!-- 扫描加载resources目录下指定后缀资源 --><resource><directory>src/main/resources</directory><includes><include>**/*.xml</include><include>**/*.properties</include></includes></resource></resources></build>

        (2)Spring配置文件

<?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"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"></beans>

实现步骤:

        (1)创建业务接口

        (2)创建业务实现

        (3)创建切面类,实现切面方法

        (4)在Spring的applicationContext.xml配置文件中进行切面绑定——>IOC+AspectJ内置代理模式(默认JDK动态代理+CGLib子类代理)


二、前置通知切面开发

        在目标方法执行前切入切面功能,在切面方法中不可以获得目标方法的返回值,只能得到目标方法的签名和参数(JoinPoint)——>修饰符类型+返回值类型+方法名+形式参数

        (1)前置通知业务接口

//前置通知业务接口
public interface BeforeService {//前置通知方法void beforeMethod();//我的信息String myInfo(String name, int age);
}

        (2)前置通知业务接口实现类

//前置通知业务接口实现
@Service //Spring的IOC注解式创建业务逻辑层实例
public class BeforeServiceImpl implements BeforeService {@Overridepublic void beforeMethod() {System.out.println("前置通知业务接口实现");}@Overridepublic String myInfo(String name, int age) {return "我的个人信息 = 姓名:" + name + ",年龄:" + age;}
}

        (3)前置通知切面类

前置通知中创建切面方法的规范

        (1)访问修饰符权限是public

        (2)方法的返回值是void

        (3)方法名称是自定义

        (4)可以没有方法形式参数,如果有,必为JoinPoint类型

        (5)必须使用@Before注解来声明切入的时机是前切功能和切入点

//前置通知使用AspectJ实现AOP功能,此类为切面类,包含各种切面方法
@Aspect //交给AspectJ的框架去识别切面类
@Component //切面实例注册加载到spring容器中
public class BeforeAspectJ {/*** 各种切面都在此类中进行开发* (1)访问修饰符权限是public* (2)方法的返回值是void* (3)方法名称是自定义* (4)可以没有方法形式参数,如果有,必为JoinPoint类型* (5)必须使用@Before注解来声明切入的时机是前切功能和切入点*//*** ...before.impl包下BeforeServiceImpl类中返回值是void类型并且公开的任意形参的任意方法都织入以下AOP功能*/@Before(value = "execution(public void com.dhrj.java.zsitking.before.impl.BeforeServiceImpl.*(..))")//切入点表达式,此处为前置通知public void myBeforeMethod() {System.out.println("我是切面类中的AOP,前置通知呀~");}/*** ...before.impl包下任意类中返回值是String类型并且形参顺序为(String,int)公开的任意方法都织入以下AOP功能*/@Before(value = "execution(public String com.dhrj.java.zsitking.before.impl.*.*(String,int))")//切入点表达式,此处为前置通知public void myInfoBefore() {System.out.println("我是切面类中的AOP,个人信息呀~");}/*** ...before.impl包下任意类中任意返回值类型并且公开的任意形参的任意方法都织入以下AOP功能*/@Before(value = "execution(public * com.dhrj.java.zsitking.before.impl.*.*(..))")//切入点表达式,此处为前置通知public void myBeforeAll01() {System.out.println("我是切面类中的AOP,....before.impl包下任意公开类中任意方法的任意参数呀~");}/*** ...before本包及其子包中任意类下任意返回值类型并且任意形参的任意方法都织入以下AOP功能*/@Before(value = "execution( * com.dhrj.java.zsitking.before..*.*(..))")//切入点表达式,此处为前置通知public void myBeforeAll02() {System.out.println("我是切面类中的AOP,....before包下以及多个子包下全部类中的方法呀~");}/*** 项目工程路径下全部中和方法都能都织入以下AOP功能——>不推荐使用,扫描效率极低,影响程序执行性能*/@Before(value = "execution( * *(..))")//切入点表达式,此处为前置通知public void myBeforeAll03() {System.out.println("我是切面类中的AOP,....项目工程路径下全部中和方法呀~");}/*** ...before本包任意类下任意返回值类型并且任意形参的任意方法都织入以下AOP功能——>获取目标方法的签名和参数*/@Before(value = "execution( * com.dhrj.java.zsitking.before.*.*(..))")public void myBeforeJoinPointArgs(JoinPoint jp) {System.out.println("目标方法的签名:" + jp.getSignature());System.out.println("目标方法的参数:" + Arrays.toString(jp.getArgs()));}
}

        (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: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"><!--基于注解的访问要添加包扫描--><context:component-scan base-package="com.dhrj.java.zsitking.before"/><!--业务实现绑定AOP,默认是JDK动态代理,取时必须使用接口类型--><aop:aspectj-autoproxy/><!-- <aop:aspectj-autoproxy proxy-target-class="true"/>设置为CGLib子类代理,可以使用接口和实现类接-->
</beans>

三、junitl测试

//前置通知测试类
public class BeforeTest {private BeforeService beforeService;@Before //junitpublic void before() {ApplicationContext ac = new ClassPathXmlApplicationContext("before/applicationContext.xml");beforeService = (BeforeService) ac.getBean("beforeServiceImpl");System.out.println("绑定切面后的对象类型:" + beforeService.getClass());}@Testpublic void testBefore01() {beforeService.beforeMethod();}@Testpublic void testBefore02() {System.out.println(beforeService.myInfo("张松", 24));}
}

        (1)运行testBefore01

        (2)运行testBefore02


四、总结

仅自己学习记录,如有错误,敬请谅解~,谢谢~~~

这篇关于(Spring笔记)AspectJ前置通知——@Before切面开发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/847794

相关文章

Java五子棋之坐标校正

上篇针对了Java项目中的解构思维,在这篇内容中我们不妨从整体项目中拆解拿出一个非常重要的五子棋逻辑实现:坐标校正,我们如何使漫无目的鼠标点击变得有序化和可控化呢? 目录 一、从鼠标监听到获取坐标 1.MouseListener和MouseAdapter 2.mousePressed方法 二、坐标校正的具体实现方法 1.关于fillOval方法 2.坐标获取 3.坐标转换 4.坐

Spring Cloud:构建分布式系统的利器

引言 在当今的云计算和微服务架构时代,构建高效、可靠的分布式系统成为软件开发的重要任务。Spring Cloud 提供了一套完整的解决方案,帮助开发者快速构建分布式系统中的一些常见模式(例如配置管理、服务发现、断路器等)。本文将探讨 Spring Cloud 的定义、核心组件、应用场景以及未来的发展趋势。 什么是 Spring Cloud Spring Cloud 是一个基于 Spring

Tolua使用笔记(上)

目录   1.准备工作 2.运行例子 01.HelloWorld:在C#中,创建和销毁Lua虚拟机 和 简单调用。 02.ScriptsFromFile:在C#中,对一个lua文件的执行调用 03.CallLuaFunction:在C#中,对lua函数的操作 04.AccessingLuaVariables:在C#中,对lua变量的操作 05.LuaCoroutine:在Lua中,

AssetBundle学习笔记

AssetBundle是unity自定义的资源格式,通过调用引擎的资源打包接口对资源进行打包成.assetbundle格式的资源包。本文介绍了AssetBundle的生成,使用,加载,卸载以及Unity资源更新的一个基本步骤。 目录 1.定义: 2.AssetBundle的生成: 1)设置AssetBundle包的属性——通过编辑器界面 补充:分组策略 2)调用引擎接口API

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

java8的新特性之一(Java Lambda表达式)

1:Java8的新特性 Lambda 表达式: 允许以更简洁的方式表示匿名函数(或称为闭包)。可以将Lambda表达式作为参数传递给方法或赋值给函数式接口类型的变量。 Stream API: 提供了一种处理集合数据的流式处理方式,支持函数式编程风格。 允许以声明性方式处理数据集合(如List、Set等)。提供了一系列操作,如map、filter、reduce等,以支持复杂的查询和转

Java面试八股之怎么通过Java程序判断JVM是32位还是64位

怎么通过Java程序判断JVM是32位还是64位 可以通过Java程序内部检查系统属性来判断当前运行的JVM是32位还是64位。以下是一个简单的方法: public class JvmBitCheck {public static void main(String[] args) {String arch = System.getProperty("os.arch");String dataM

详细分析Springmvc中的@ModelAttribute基本知识(附Demo)

目录 前言1. 注解用法1.1 方法参数1.2 方法1.3 类 2. 注解场景2.1 表单参数2.2 AJAX请求2.3 文件上传 3. 实战4. 总结 前言 将请求参数绑定到模型对象上,或者在请求处理之前添加模型属性 可以在方法参数、方法或者类上使用 一般适用这几种场景: 表单处理:通过 @ModelAttribute 将表单数据绑定到模型对象上预处理逻辑:在请求处理之前

eclipse运行springboot项目,找不到主类

解决办法尝试了很多种,下载sts压缩包行不通。最后解决办法如图: help--->Eclipse Marketplace--->Popular--->找到Spring Tools 3---->Installed。

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001