(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

相关文章

Spring Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4