Spring基于XML配置AOP

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

理解 AOP 概念参阅:

Spring的AOP和动态代理

基于注解配置参阅:

Spring基于注解配置AOP

一、概述

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

二、AOP 使用

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

1. 定义切面

引用 Spring 管理的 Bean,使用 <aop:aspect> 来定义切面。

<beans>

    <bean id="demoAspect" class="...DemoAspect"/>

    <aop:config>
        <aop:aspect ref="demoAspect">
            ......
        </aop:aspect>
    </aop:config>

</beans>

2. 定义切点

在切面内使用 <aop:pointcut> 来定义切点,然后在通知中使用 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>

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

<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. 定义通知

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

在切面里使用通知标签中的 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:before> 定义前置通知,在方法执行前添加操作。

<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:after-returning> 注解定义后置通知,在方法正常返回时执行,方法抛异常不执行。

<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>

环绕通知

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

环绕通知的切入点参数为 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:after> 注解定义最终通知,在方法退出时执行,无论是正常退出还是异常退出。

<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:after-throwing> 注解定义异常通知,在方法抛出异常时执行。

<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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring中,基于XML配置AOP的步骤如下: 1. 添加依赖并准备环境。 2. 创建通知类和接口,并实现方法。 3. 配置spring.xml文件。 4. 执行测试。 具体步骤如下: 1. 创建通知类和接口,并实现方法。 2. 在spring.xml文件中配置AOP。 3. 使用aop:config标签开始AOP配置。 4. 使用aop:aspect标签配置切面,其中id属性是给切面提供一个唯一标识,ref属性是指定通知类bean的Id。 5. 在aop:aspect标签内部使用对应标签来配置通知的类型,例如使用aop:before标签配置前置通知。 6. 在aop:before标签中,使用method属性指定Logger类中哪个方法是前置通知,使用pointcut属性指定切入点表达式,该表达式指定了对业务层中哪些方法进行增强。 7. 切入点表达式的写法可以使用关键字execution和具体的表达式来指定方法的访问修饰符、返回值、包名、类名和方法名等。 8. 在通知类中添加环绕通知方法,使用ProceedingJoinPoint接口作为环绕通知的方法参数,该接口提供了proceed()方法,用于明确调用切入点方法。 9. 在环绕通知方法中,可以通过proceedingJoinPoint.proceed(args)调用切入点方法,并在方法执行前后进行相应的操作。 10. 最后,执行测试来验证AOP配置的效果。 以上是基于XML配置AOP的步骤。通过配置AOP,可以在指定的切入点方法执行前后,或者在方法执行过程中进行增强操作。 #### 引用[.reference_title] - *1* *3* [Spring之基于XML配置AOP](https://blog.csdn.net/qq_38628046/article/details/108065715)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Spring基于XMLAOP配置](https://blog.csdn.net/weixin_45430616/article/details/104101117)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值