Spring基于XML配置AOP

简介: Spring 的 AOP 功能是基于 AspectJ 实现的,支持使用 XML 方式定义 AOP 切面。


一、概述



Spring 项目使用 AOP 功能需要定义三个部分:切面、切点和通知。


二、AOP 使用



Spring 基于 XML 配置 AOP 的方式不会侵入源码,但需要维护更多的配置文件。


1. 定义切面


引用 Spring 管理的 Bean,使用  来定义切面。

<beans>
    <bean id="demoAspect" class="...DemoAspect"/>
    <aop:config>
        <aop:aspect ref="demoAspect">
            ......
        </aop:aspect>
    </aop:config>
</beans>


2. 定义切点


在切面内使用 来定义切点,然后在通知中使用 pointcut-ref 来指定切点。

切点表达式用来匹配切入的目标类和方法。目标类只能是 Spring 容器管理的类,切面只能切入 Bean 中的方法。

<beans>
    <bean id="demoAspect" class="...DemoAspect"/>
    <aop:config>
        <aop:aspect ref="demoAspect">
            <aop:pointcut id="myPointcut" expression="execution(* cn.codeartist.spring.aop.xml.*.*(..))"/>
            <aop:before pointcut-ref="myPointcut" method="doBefore"/>
        </aop:aspect>
    </aop:config>
</beans>

切点表达式也可以在定义通知的时候指定,而不需要使用 标签。

<beans>
    <bean id="demoAspect" class="...DemoAspect"/>
    <aop:config>
        <aop:aspect ref="demoAspect">
            <aop:before pointcut="execution(* cn.codeartist.spring.aop.xml.*.*(..))" method="doBefore"/>
        </aop:aspect>
    </aop:config>
</beans>


3. 定义通知


定义通知的时候需要指定切点,通知的类型决定了切入的节点。

微信图片02.png

在切面里使用通知标签中的 method 属性来绑定方法。

public class DemoAspect {
    public void doBefore(JoinPoint joinPoint) {
        // do something
    }
    public void doAfter(JoinPoint joinPoint) {
        // do something
    }
    public void doAfterReturning(JoinPoint joinPoint) {
        // do something
    }
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        // do something
        Object proceed = joinPoint.proceed();
        // do something
        return proceed;
    }
    public void doAfterThrowing(JoinPoint joinPoint) {
        // do something
    }
}


前置通知

使用 定义前置通知,在方法执行前添加操作。

<aop:config>
    <aop:aspect ref="demoAspect">
        <aop:pointcut id="myPointcut" expression="execution(* cn.codeartist.spring.aop.xml.*.*(..))"/>
        <aop:before pointcut-ref="myPointcut" method="doBefore"/>
    </aop:aspect>
</aop:config>


后置通知

使用 注解定义后置通知,在方法正常返回时执行,方法抛异常不执行。

<aop:config>
    <aop:aspect ref="demoAspect">
        <aop:pointcut id="myPointcut" expression="execution(* cn.codeartist.spring.aop.xml.*.*(..))"/>
        <aop:after-returning pointcut-ref="myPointcut" method="doAfterReturning"/>
    </aop:aspect>
</aop:config>


环绕通知

使用 注解定义环绕通知,切入方法前后,相当于拦截器的功能,可以捕获异常处理。

环绕通知的切入点参数为 ProceedingJoinPoint,并且需要手动调用 proceed() 来执行切入点方法的逻辑。

<aop:config>
    <aop:aspect ref="demoAspect">
        <aop:pointcut id="myPointcut" expression="execution(* cn.codeartist.spring.aop.xml.*.*(..))"/>
        <aop:around pointcut-ref="myPointcut" method="doAround"/>
    </aop:aspect>
</aop:config>


最终通知

使用 注解定义最终通知,在方法退出时执行,无论是正常退出还是异常退出。

<aop:config>
    <aop:aspect ref="demoAspect">
        <aop:pointcut id="myPointcut" expression="execution(* cn.codeartist.spring.aop.xml.*.*(..))"/>
        <aop:after pointcut-ref="myPointcut" method="doAfter"/>
    </aop:aspect>
</aop:config>


异常通知

使用 注解定义异常通知,在方法抛出异常时执行。

<aop:config>
    <aop:aspect ref="demoAspect">
        <aop:pointcut id="myPointcut" expression="execution(* cn.codeartist.spring.aop.xml.*.*(..))"/>
        <aop:after-throwing pointcut-ref="myPointcut" method="doAfterThrowing"/>
    </aop:aspect>
</aop:config>


4. 通过 Advisor 实现


使用 Advisor 能以编程的方式创建切面,需要实现通知的 API 来定义通知的类型。

比起使用注解定义切点,这种方式指定切点表达式更灵活。

<beans>
    <bean id="beforeAdvice" class="...BeforeAdvice"/>
    <aop:config>
        <aop:advisor pointcut="execution(* cn.codeartist.spring.aop.xml.*.*(..))" advice-ref="beforeAdvice"/>
    </aop:config>
</beans>


三、附录



1. 常用配置

配置 描述
<aop:config> 配置 AOP 功能
<aop:aspect> 定义切面类
<aop:pointcut> 定义切点,指定切点表达式
<aop:before> 定义前置通知
<aop:after-returning> 定义后置通知
<aop:around> 定义环绕通知
<aop:after> 定义最终通知
<aop:after-throwing> 定义异常通知
<aop:advisor> 使用 Advisor 方式创建切面


2. 示例代码


Gitee 仓库:

https://gitee.com/code_artist/spring

项目模块:

spring-aop

示例路径:

cn.codeartist.spring.aop.xml


目录
相关文章
|
12天前
|
安全 Java 数据安全/隐私保护
Spring Security 6.x 一文快速搞懂配置原理
本文主要对整个Spring Security配置过程做一定的剖析,希望可以对学习Spring Sercurity框架的同学所有帮助。
46 5
Spring Security 6.x 一文快速搞懂配置原理
|
4天前
|
运维 Java 测试技术
Spring运维之boo项目表现层测试加载测试的专用配置属性以及在JUnit中启动web服务器发送虚拟请求
Spring运维之boo项目表现层测试加载测试的专用配置属性以及在JUnit中启动web服务器发送虚拟请求
10 3
|
8天前
|
消息中间件 Java Kafka
集成Kafka到Spring Boot项目中的步骤和配置
集成Kafka到Spring Boot项目中的步骤和配置
38 7
|
8天前
|
druid Java 关系型数据库
在Spring Boot中集成Druid实现多数据源有两种常用的方式:使用Spring Boot的自动配置和手动配置。
在Spring Boot中集成Druid实现多数据源有两种常用的方式:使用Spring Boot的自动配置和手动配置。
72 5
|
10天前
|
XML Java 数据格式
Spring Boot自动配置是通过`@EnableAutoConfiguration`注解启用的
【6月更文挑战第18天】Spring Boot的`@EnableAutoConfiguration`启动自动配置,基于类路径扫描和条件注解(如@ConditionalOnClass)选择性应用配置。当检测到特定依赖时,自动配置模块会将对应的bean添加到应用上下文,简化了XML或Java配置。只需添加依赖,即可自动配置功能。
18 4
|
19小时前
|
XML 数据格式
XML配置Servlet文件,不使用注解配置路径的方法
XML配置Servlet文件,不使用注解配置路径的方法
|
19小时前
|
监控 前端开发 Java
Spring Boot中的拦截器配置
Spring Boot中的拦截器配置
|
19小时前
|
Java 机器人 程序员
Spring Boot中的模板引擎选择与配置
Spring Boot中的模板引擎选择与配置
|
19小时前
|
消息中间件 Java RocketMQ
教程:Spring Boot整合RocketMQ的配置与优化
教程:Spring Boot整合RocketMQ的配置与优化
|
1天前
|
XML Java 数据格式
SpringMVC的XML配置解析-spring18
SpringMVC的XML配置解析-spring18