Spring AOP XML配置适用于旧版项目,需声明aop命名空间、定义切面Bean、用配置pointcut与各类advice,并启用proxy-target-class=true以支持CGLIB代理。

Spring AOP 的 XML 配置方式在较老的 Spring 项目(如 Spring 3.x 或早期 4.x)中常用,虽然现在主流用注解(@Aspect、@Before 等),但理解 XML 配置有助于阅读遗留代码或满足特定部署约束。
基础依赖与命名空间声明
确保项目引入了 spring-aop 和 aspectjweaver 依赖,并在 Spring 配置文件(如 applicationContext.xml)顶部声明 AOP 命名空间:
<?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">登录后复制
定义切面类(普通 Java 类)
不需要实现任何接口或继承类,只需提供通知方法:
public class LoggingAspect {
public void beforeAdvice() {
System.out.println("【前置通知】方法执行前记录日志");
}
public void afterReturningAdvice(Object result) {
System.out.println("【返回通知】方法返回值:" + result);
}
}登录后复制
然后在 XML 中将其声明为一个 bean:
<bean id="loggingAspect" class="com.example.LoggingAspect"/>
登录后复制
配置切点(pointcut)和通知(advice)
使用 <config></config> 定义切面逻辑。常见组合如下:
标签: java app proxy win 配置文件 动态代理
还木有评论哦,快来抢沙发吧~