04-基于XML配置方式的AOP

基于XML配置方式的AOP

实现步骤

第一步: 定义目标类VipService

package com.powernode.spring6.service;
// 目标类
public class VipService {
    public void add(){// 目标方法
        System.out.println("保存vip信息。");
    }
}

第二步: 定义切面类TimerAspect负责计时,在切面类编写通知方法即我们要对目标对象增强的代码

public class TimerAspect {   
    public void time(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 前环绕
        long begin = System.currentTimeMillis();
        // 执行目标方法
        proceedingJoinPoint.proceed();
        // 后环绕
        long end = System.currentTimeMillis();
        System.out.println("耗时"+(end - begin)+"毫秒");
    }
}

第三步 :在spring配置文件使用标签进行配置,需要引入context和aop的命名空间和约束文件

  • 第一步: 首先将目标类和切面类注册为组件纳入Spring容器中管理
  • 第二步: 使用aop:aspect标签的ref属性告诉Spring容器中的哪个类是切面类
  • 第三步: 使用aop:pointcut标签的expression属性声明切点表达式,每个切点表达式都有一个id属性便与通知引用
  • 第四步: 使用aop:around标签声明一个环绕通知,method属性指定切面类的通知方法,pointcut-ref属性指定切点表达式即通知作用哪个目标对象的目标方法
<?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 http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--将目标类和切面类纳入Spring管理-->
    <bean id="vipService" class="com.powernode.spring6.service.VipService"/>
    <bean id="timerAspect" class="com.powernode.spring6.service.TimerAspect"/>

    <!--aop配置-->
    <aop:config>
        <!--通用的切点表达式-->
        <aop:pointcut id="mypointcut" expression="execution(* com.powernode.spring6.service.VipService.*(..))"/>
        <!--切面类=通知 + 切点-->
        <aop:aspect ref="timerAspect">
            <!--环绕通知-->
            <aop:around method="time" pointcut-ref="mypointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

第四步: 执行目标对象的目标方法,测试切面类中的通知方法的执行顺序

public class AOPTest3 {
    @Test
    public void testAOPXml(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-aop-xml.xml");
        VipService vipService = applicationContext.getBean("vipService", VipService.class);
        vipService.add();
    }
}
保存vip信息
耗时4毫秒
在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、付费专栏及课程。

余额充值