12、Spring之基于xml的AOP
阅读本文前,建议先阅读Spring之基于注解的AOP
12.1、环境搭建
创建名为spring_aop_xml的新module,过程参考9.1节
12.1.1、配置打包方式和依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.rain</groupId>
<artifactId>spring_aop_xml</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!-- Spring-IOC的依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<!-- spring-AOP的依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.1</version>
</dependency>
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
12.1.2、创建Calculator接口及实现类

package org.rain.spring.aop.xml;
/**
* @author liaojy
* @date 2023/8/18 - 8:09
*/
public interface Calculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}

package org.rain.spring.aop.xml;
import org.springframework.stereotype.Component;
/**
* @author liaojy
* @date 2023/8/18 - 8:11
*/
// @Component注解:保证这个目标类能够放入IOC容器
@Component
public class CalculatorImpl implements Calculator {
public int add(int i, int j) {
int result = i + j;
System.out.println("方法内部 result = " + result);
return result;
}
public int sub(int i, int j) {
int result = i - j;
System.out.println("方法内部 result = " + result);
return result;
}
public int mul(int i, int j) {
int result = i * j;
System.out.println("方法内部 result = " + result);
return result;
}
public int div(int i, int j) {
int result = i / j;
System.out.println("方法内部 result = " + result);
return result;
}
}
12.1.3、创建切面类LoggerAspect

package org.rain.spring.aop.xml;
import org.springframework.stereotype.Component;
/**
* @author liaojy
* @date 2023/8/18 - 8:15
*/
// @Component注解:保证这个目标类能够放入IOC容器
@Component
public class LoggerAspect {
}
12.1.4、创建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"
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">
<!--
对指定的package进行扫描,将使用组件注解的类的对象(本示例是目标对象和切面对象),交给spring的ioc容器来管理
-->
<context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
</beans>
12.2、前置通知的使用
12.2.1、基本示例
12.2.1.1、定义前置通知的功能

public void beforeMethod(){
System.out.println("LoggerAspect,前置通知");
}
12.2.1.2、配置前置通知到切面

<aop:config>
<!--
aop:aspect标签:将ioc容器中的bean对象设置为切面,效果等同于@Aspect注解
ref属性:引用IOC容器中(要设置为切面的)bean的id
-->
<aop:aspect ref="loggerAspect">
<!--
aop:before标签:将方法设置为该切面的前置通知(方法),效果等同于@Before注解
method属性:指定(要设置为前置通知的)方法的名称
pointcut属性:设置该前置通知的切入点表达式
-->
<aop:before method="beforeMethod" pointcut="execution(* org.rain.spring.aop.xml.CalculatorImpl.*(..))"></aop:before>
</aop:aspect>
</aop:config>
12.2.1.3、测试使用效果

由控制台日志可知,切面类的前置通知(方法),通过切入点表达式,作用到了目标方法的连接点上
package org.rain.spring.aop.test;
import org.junit.Test;
import org.rain.spring.aop.xml.Calculator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author liaojy
* @date 2023/8/18 - 19:07
*/
public class AOPTest {
@Test
public void testAOPByXML(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-aopByxml.xml");
// 注意:这里不能直接获取目标对象来使用;因为使用了AOP之后,IOC容器中就只有对应目标对象的代理对象;
// 如果强行获取目标对象,则报错:NoSuchBeanDefinitionException
//Calculator calculator = ioc.getBean(CalculatorImpl.class);
// 虽然不知道代理对象的类名,但可以通过代理对象和目标对象共同实现的接口类型来从ioc容器中获取代理对象
Calculator calculator = ioc.getBean(Calculator.class);
// 只能通过代理对象来访问目标对象中的方法
calculator.div(1,1);
}
}
12.2.2、高级示例
12.2.2.1、改进前置通知的功能

该示例中(前置)通知方法引入了连接点参数,通过连接点参数,可以动态获取(切入点表达式)对应的目标方法的名称和参数列表
// joinPoint参数:可以获取(通过切入点表达式定位出的)连接点的相关信息
public void beforeMethod(JoinPoint joinPoint){
// 获取连接点所对应目标方法的名称
String methodName = joinPoint.getSignature().getName();
// 获取连接点所对应目标方法的参数列表
Object[] args = joinPoint.getArgs();
System.out.println("LoggerAspect-->前置通知,方法名:"+methodName+",参数:"+ Arrays.toString(args));
}
12.2.2.2、测试使用效果

12.3、切入点表达式的复用
切入点表达的详细语法,请参考11.3.2节
12.3.1、配置公共的切入点表达式

<!--
aop:pointcut标签:用于声明公共的切入点表达式,效果等同于@Pointcut注解
id属性:设置该公共切入点表达式的唯一标识
expression属性:设置切入点表达式
-->
<aop:pointcut id="pointcutOne" expression="execution(* org.rain.spring.aop.xml.CalculatorImpl.*(..))"/>
12.3.2、使用公共的切入点表达式

<!--
aop:before标签:将方法设置为该切面的前置通知(方法),效果等同于@Before注解
method属性:指定(要设置为前置通知的)方法的名称
pointcut属性:设置该前置通知的切入点表达式
pointcut-ref属性:通过id引用对应的公共切入点表达式
-->
<!--<aop:before method="beforeMethod" pointcut="execution(* org.rain.spring.aop.xml.CalculatorImpl.*(..))"></aop:before>-->
<aop:before method="beforeMethod" pointcut-ref="pointcutOne"></aop:before>
12.4、其他通知的使用
12.4.1、后置通知
12.4.1.1、定义后置通知的功能

public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("LoggerAspect-->后置通知,方法名:"+methodName+",参数:"+ Arrays.toString(args));
}
12.4.1.2、配置后置通知到切面

<!--
aop:after标签:将方法设置为该切面的后置通知(方法),效果等同于@After注解
-->
<aop:after method="afterMethod" pointcut-ref="pointcutOne"></aop:after>
12.4.1.3、测试使用效果

由控制台日志可知,后置通知在目标对象方法的finally子句中执行(一般用于释放资源)
@Test
public void testAOPByXML(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-aopByxml.xml");
// 虽然不知道代理对象的类名,但可以通过代理对象和目标对象共同实现的接口类型来从ioc容器中获取代理对象
Calculator calculator = ioc.getBean(Calculator.class);
// 只能通过代理对象来访问目标对象中的方法
calculator.div(1,0);
}
12.4.2、返回通知
12.4.2.1、定义返回通知的功能

public void afterReturningMethod(JoinPoint joinPoint,Object result){
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("LoggerAspect-->返回通知,方法名:"+methodName+",结果:"+ result);
}
12.4.2.2、配置返回通知到切面

<!--
aop:after-returning标签:将方法设置为该切面的返回通知(方法),效果等同于@AfterReturning注解
returning属性:指定返回通知(方法)中的某个参数(名),用于接收目标对象方法的返回值
-->
<aop:after-returning method="afterReturningMethod" pointcut-ref="pointcutOne" returning="result"></aop:after-returning>
12.4.2.3、测试使用效果

返回通知在目标对象方法的返回值之后执行
@Test
public void testAOPByXML(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-aopByxml.xml");
// 虽然不知道代理对象的类名,但可以通过代理对象和目标对象共同实现的接口类型来从ioc容器中获取代理对象
Calculator calculator = ioc.getBean(Calculator.class);
// 只能通过代理对象来访问目标对象中的方法
calculator.div(1,1);
}
12.4.3、异常通知
12.4.3.1、定义异常通知的功能

public void afterThrowingMethod(JoinPoint joinPoint,Exception ex){
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("LoggerAspect-->异常通知,方法名:"+methodName+",异常:"+ ex);
}
12.4.3.2、配置异常通知到切面

<!--
aop:after-throwing标签:将方法设置为该切面的异常通知(方法),效果等同于@AfterThrowing注解
throwing属性:指定异常通知(方法)中的某个参数(名),用于接收目标对象方法出现的异常
-->
<aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointcutOne" throwing="ex" ></aop:after-throwing>
12.4.3.3、测试使用效果

由控制台日志可知,异常通知在目标对象方法的catch子句中执行
@Test
public void testAOPByXML(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-aopByxml.xml");
// 虽然不知道代理对象的类名,但可以通过代理对象和目标对象共同实现的接口类型来从ioc容器中获取代理对象
Calculator calculator = ioc.getBean(Calculator.class);
// 只能通过代理对象来访问目标对象中的方法
calculator.div(1,0);
}
12.5、环绕通知
12.5.1、定义环绕通知的功能

环绕通知和动态代理的形式,非常相似
/**
* 环绕通知(方法)使用的参数是ProceedingJoinPoint类型
* 环绕通知(方法)的返回值,必须和目标对象方法的返回值一致
*/
public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint){
String methodName = proceedingJoinPoint.getSignature().getName();
Object[] args = proceedingJoinPoint.getArgs();
Object result = null;
try {
System.out.println("LoggerAspect-->环绕前置通知,方法名:"+methodName+",参数:"+ Arrays.toString(args));
// 表示目标对象方法的执行
result = proceedingJoinPoint.proceed();
System.out.println("LoggerAspect-->环绕返回通知,方法名:"+methodName+",结果:"+ result);
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("LoggerAspect-->环绕异常通知,方法名:"+methodName+",异常:"+ throwable);
}finally {
System.out.println("LoggerAspect-->环绕后置通知,方法名:"+methodName+",参数:"+ Arrays.toString(args));
}
return result;
}
12.5.2、配置环绕通知到切面

<!--
aop:around标签:将方法设置为该切面的环绕通知(方法),效果等同于@Around注解
-->
<aop:around method="aroundMethod" pointcut-ref="pointcutOne"></aop:around>
12.5.3、测试使用效果

注意:因为环绕通知包括了其他四种通知,所以一般要么配置其他四种通知,要么只配置环绕通知;
本示例为了展示效果才同时配置
@Test
public void testAOPByXML(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-aopByxml.xml");
// 虽然不知道代理对象的类名,但可以通过代理对象和目标对象共同实现的接口类型来从ioc容器中获取代理对象
Calculator calculator = ioc.getBean(Calculator.class);
// 只能通过代理对象来访问目标对象中的方法
calculator.div(1,1);
}
12.6、切面的优先级
12.6.1、创建其他切面类ValidateAspect

package org.rain.spring.aop.xml;
import org.springframework.stereotype.Component;
/**
* @author liaojy
* @date 2023/8/20 - 23:59
*/
// @Component注解:保证这个目标类能够放入IOC容器
@Component
public class ValidateAspect {
}
12.6.2、定义前置通知的功能

public void beforeMethod(){
System.out.println("ValidateAspect-->前置通知");
}
12.6.3、配置前置通知到切面

<aop:aspect ref="validateAspect">
<aop:before method="beforeMethod" pointcut-ref="pointcutOne"></aop:before>
</aop:aspect>
12.6.4、测试使用效果

由控制台日志可知,ValidateAspect切面的前置通知方法生效了,但执行顺序在LoggerAspect切面的前置通知方法的后面
@Test
public void testAOPByXML(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-aopByxml.xml");
// 虽然不知道代理对象的类名,但可以通过代理对象和目标对象共同实现的接口类型来从ioc容器中获取代理对象
Calculator calculator = ioc.getBean(Calculator.class);
// 只能通过代理对象来访问目标对象中的方法
calculator.div(1,1);
}
12.6.5、调整切面的优先级

<!--
aop:aspect标签的order属性:用于设置切面的优先级,value属性值越小,优先级越高,默认值为Integer的最大值
-->
<aop:aspect ref="validateAspect" order="2023">
<aop:before method="beforeMethod" pointcut-ref="pointcutOne"></aop:before>
</aop:aspect>
12.6.6、测试调整后的效果

由控制台日志可知,ValidateAspect切面的前置通知方法的执行顺序,在LoggerAspect切面的前置通知方法的前面
这是因为ValidateAspect切面的Order属性值已设为2023,要小于LoggerAspect切面所使用的默认值(Integer的最大值2147483647)
12、Spring之基于xml的AOP的更多相关文章
- Spring中基于xml的AOP
1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...
- spring的基于xml的AOP配置案例和切入点表达式的一些写法
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- 基于XML的AOP配置
创建spring的配置文件并导入约束 此处要导入aop的约束 <?xml version="1.0" encoding="UTF-8"?> < ...
- Spring 框架的概述以及Spring中基于XML的IOC配置
Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...
- Spring学习--基于 XML 的配置声明切面
正常情况下 , 基于注解的生命要优先于基于 XML 的声明. 通过 AspectJ 注解 , 切面可以与 AspectJ 兼容 , 而基于 XML 的配置则是 Spring 专有的.由于 Aspect ...
- 【Spring】基于@Aspect的AOP配置
Spring AOP面向切面编程,可以用来配置事务.做日志.权限验证.在用户请求时做一些处理等等.用@Aspect做一个切面,就可以直接实现. · 本例演示一个基于@Aspect的小demo 1. ...
- Spring AspectJ基于注解的AOP实现
对于AOP这种编程思想,很多框架都进行了实现.Spring就是其中之一,可以完成面向切面编程.然而,AspectJ也实现了AOP的功能,且实现方式更为简捷,使用更加方便,而且还支持注解式开发.所以,S ...
- 基于 XML 的 AOP 配置(1)
本文连接:https://www.cnblogs.com/qzhc/p/11969734.html 接下来我将用一个很简单的实例 1. 环境搭建 1.1. 第一步:准备必要的代码 业务层代码: Acc ...
- Spring框架——基于XML/注解开发
IoC的实现方式有两种:XML配置文件.基于注解. MVC开发模式: Controller层 Service层 Repository层 Controller层调用Service,Service调用Re ...
- 阶段3 2.Spring_10.Spring中事务控制_1 基于XML的AOP实现事务控制
新建项目 首先把依赖复制进来 aop必须引入.aspectjweaver 复制src下的所有内容 复制到我们的新项目里面 factory文件夹删掉 删除后测试类必然就报错 配置文件 beanFacto ...
随机推荐
- 2022-05-30:给定一个n*2的二维数组,表示有n个任务。 一个信息是任务能够开始做的时间,另一个信息是任务的结束期限,后者一定大于前者,且数值上都是正数, 你作为单线程的人,不能并行处理任务,
2022-05-30:给定一个n*2的二维数组,表示有n个任务. 一个信息是任务能够开始做的时间,另一个信息是任务的结束期限,后者一定大于前者,且数值上都是正数, 你作为单线程的人,不能并行处理任务, ...
- vue全家桶进阶之路14:常用属性和方法
Vue2中常用的属性和方法: 属性 el:用于指定Vue实例挂载的元素,可以是CSS选择器.HTML元素或Vue组件. data:用于存储Vue实例的响应式数据,也可以是一个函数,返回一个对象,用于提 ...
- rust 中 str 与 String; &str &String
String String 类型的数据和基本类型不同,基本类型的长度是固定的,所以可以在栈上分配,而String类型是变长的,所以需要在堆上分配,所以String 类型实际上是一个指向堆的指针.他的结 ...
- 洛谷P3368 【模板】树状数组 2-(区间修改,单点查询)
题目描述 如题,已知一个数列,你需要进行下面两种操作: 将某区间每一个数加上 x: 求出某一个数的值. 输入格式 第一行包含两个整数 N.M,分别表示该数列数字的个数和操作的总个数. 第二行包含 N ...
- {"status":-1,"statusText":"ERR_CONNECT_FAILED"}
今日使用weex 的stream 遇到一个极坑,也极傻的问题 一.steam.fetch 下面是我使用steam.fetch调用后台接口都截图 二.页面测试 奇怪的是,我借同事是手机来进行测试,有一个 ...
- 快速上手kettle(二)小试牛刀
目录 一 .前言 二 .两个小目标 三. kettle核心概念介绍 3.1 转换 3.1.1 步骤(Step) 3.1.2 跳(Hop) 3.1.3 元素据 3.1.4 数据类型 3.1.5 并发执行 ...
- Java中如何中断线程
在Java中,可以使用以下方法中断线程: 1. 使用`interrupt()`方法:每个线程对象都有一个`interrupt()`方法,用于中断该线程.当调用线程的`interrupt()`方法时,它 ...
- Spring Boot实现高质量的CRUD-3
(续前文) 7.Service接口类 Service类提供业务的实现逻辑,其调用Dao类的方法进行数据存取,并为Controller类提供方法.类似于Dao的接口类,服务层使用接口类,便于代码 ...
- @Retention元注解的使用
@Retention注解标记其他的注解用于指明标记的注解保留策略:先看Java SE 8中@Target是如何声明的: package java.lang.annotation; public enu ...
- linux 服务器上查看日志的几种方式
1.tail命令:tail -f filename 可以动态的查看日志的输出内容. 查看文件的最后几行,默认是10行,但是可以通过-n 参数来指定要查看的行数. 例如tail -n ...