【Spring】基础入门篇(九) 基于XML的SpringAOP配置

本文详细介绍如何通过XML配置方式实现Spring AOP,包括环境搭建、业务层代码编写、通知类设计、AOP配置等步骤,重点讲解切入点、通知类型及环绕通知的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring·基础入门篇(九) 基于XML的SpringAOP配置


1. 环境搭建

基于XML方式配置AOP实现日志系统为案例

第一步:创建普通Maven工程

第二步:导入pom.xml坐标

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.2.RELEASE</version>
</dependency>
<dependency>
     <groupId>org.aspectj</groupId>
     <artifactId>aspectjweaver</artifactId>
     <version>1.8.7</version>
 </dependency>

第三步:创建 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: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/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

此处要导入 aop 的约束

第四步:业务层代码

/**
 * @author: LzCc
 * @blog: https://blog.csdn.net/qq_41744145
 * @description: 账户的业务层接口
 */
public interface IAccountService {
    /**
     * 模拟保存账户
     */
   void saveAccount();
    /**
     * 模拟更新账户
     * @param i
     */
   void updateAccount(int i);
    /**
     * 删除账户
     * @return
     */
   int  deleteAccount();
}
/**
 * @author: LzCc
 * @blog: https://blog.csdn.net/qq_41744145
 * @description: 账户的业务层实现类
 */
public class AccountServiceImpl implements IAccountService{
    @Override
    public void saveAccount() {
        System.out.println("执行了保存");
    }
    @Override
    public void updateAccount(int i) {
        System.out.println("执行了更新"+i);

    }
    @Override
    public int deleteAccount() {
        System.out.println("执行了删除");
        return 0;
    }
}

第五步:抽取公共代码制作成通知

/**
 * @author: LzCc
 * @blog: https://blog.csdn.net/qq_41744145
 * @description: 用于记录日志的工具类,它里面提供了公共的代码
 */
public class Logger {
    /**
     * 前置通知
     */
    public  void beforePrintLog(){
        System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
    }
    /**
     * 后置通知
     */
    public  void afterReturningPrintLog(){
        System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
    }
    /**
     * 异常通知
     */
    public  void afterThrowingPrintLog(){
        System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
    }
    /**
     * 最终通知
     */
    public  void afterPrintLog(){
        System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");
    }

2. 配置AOP

2.1 第一步:把通知类用 bean 标签配置起来

<!-- 配置srping的Ioc,把service对象配置进来-->
<bean id="accountService" class="top.lzchao.service.impl.AccountServiceImpl"></bean>
<!-- 配置Logger类 -->
<bean id="logger" class="top.lzchao.utils.Logger"></bean>

2.2 第二步:使用 aop:config 声明 aop 配置

aop:config:用于声明开始 aop 的配置

<aop:config>
<!-- 配置的代码都写在此处 -->
</aop:config>

2.3 第三步:使用 aop:aspect 配置切面

aop:aspect::用于配置切面。

  • 属性:
    id:给切面提供一个唯一标识。
    ref:引用配置好的通知类 bean 的 id
<aop:aspect id="logAdvice" ref="logger">
<!--配置通知的类型要写在此处-->
</aop:aspect>

2.4 第四步:使用 aop:pointcut 配置切入点表达式

aop:pointcut:用于配置切入点表达式。就是指定对哪些类的哪些方法进行增强

  • 属性:
    expression:用于定义切入点表达式。
    id:用于给切入点表达式提供一个唯一标识
<aop:pointcut id="pt1" expression="execution(* top.lzchao.service.impl.*.*(..))">

</aop:pointcut>

此标签写在aop:aspect标签内部只能当前切面使用。
它还可以写在aop:aspect外面,此时就变成了所有切面可用

2.5 第五步:使用 aop:xxx 配置对应的通知类型

aop:before:用于配置前置通知。指定增强的方法在切入点方法之前执行

aop:after-returning:用于配置后置通知,切入点方法正常执行之后。它和异常通知只能有一个执行

aop:after-throwing:用于配置异常通知,切入点方法执行产生异常后执行。它和后置通知只能执行一个

aop:after:用于配置最终通知,无论切入点方法执行时是否有异常,它都会在其后面执行。

  • 属性:
    method:指定通知中方法的名称。
    pointct:定义切入点表达式
    pointcut-ref:指定切入点表达式的引用
<!-- 配置前置通知:在切入点方法执行之前执行-->
<aop:before method="beforePrintLog" pointcut-ref="pt1" ></aop:before>
<!-- 配置后置通知:在切入点方法正常执行之后值。它和异常通知永远只能执行一个-->
<aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>
<!-- 配置异常通知:在切入点方法执行产生异常之后执行。它和后置通知永远只能执行一个-->
<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>
<!-- 配置最终通知:无论切入点方法是否正常执行它都会在其后面执行-->
<aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>

2.6 还有一种环绕通知 aop:around

aop:around:用于配置环绕通知

  • 属性:
    method:指定通知中方法的名称。
    pointct:定义切入点表达式
    pointcut-ref:指定切入点表达式的引用
  • 说明:
    它是 spring 框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式。
  • 注意:
    通常情况下,环绕通知都是独立使用的
<!-- 配置环绕通知 -->
<aop:around method="aroundPringLog" pointcut-ref="pt1"/>
/**
 * 环绕通知
 *      Spring框架为我们提供了一个接口:ProceedingJoinPoint。该接口有一个方法proceed(),此方法就相当于明确调用切入点方法。
 *      该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用。
 *
 * spring中的环绕通知:
 *      它是spring框架为我们提供的一种可以在代码中手动控制增强方法何时执行的方式。
 */
public Object aroundPringLog(ProceedingJoinPoint pjp){
    Object rtValue = null;
    try{
        Object[] args = pjp.getArgs();//得到方法执行所需的参数
        System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。前置");
        rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)
        System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。后置");
        return rtValue;
    }catch (Throwable t){
        System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。异常");
        throw new RuntimeException(t);
    }finally {
        System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。最终");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值