(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 Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Java中的工具类命名方法

《Java中的工具类命名方法》:本文主要介绍Java中的工具类究竟如何命名,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java中的工具类究竟如何命名?先来几个例子几种命名方式的比较到底如何命名 ?总结Java中的工具类究竟如何命名?先来几个例子JD

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

SpringBoot整合OpenFeign的完整指南

《SpringBoot整合OpenFeign的完整指南》OpenFeign是由Netflix开发的一个声明式Web服务客户端,它使得编写HTTP客户端变得更加简单,本文为大家介绍了SpringBoot... 目录什么是OpenFeign环境准备创建 Spring Boot 项目添加依赖启用 OpenFeig

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖