【Spring】使用xml配置AOP

1.前言

在之前的学习中,都是使用注解的方式进行AOP的配置.其实使用xml配置文件也可以配置AOP.

2.xml配置AOP

xml配置AOP方法如下:

  1. 添加相关依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.29</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.7</version>
        </dependency>
    </dependencies>    
    
  2. 相关bean放到Spring容器中

    @Service
    public class StudentService {
    
        public void insert(){
            System.out.println("StudentService中的insert方法");
        }
    }
    
  3. 创建切面类注入到Spring中,我这里使用的是@Component注解,也可以在配置文件中使用Bean标签

    @Component
    public class Aspect {
        @Pointcut("execution(* com.example.service..*.*(..)")
        public void pt(){
            System.out.println("");
        }
        public void methodBefore(JoinPoint joinPoint){
            Object[] args = joinPoint.getArgs();
            Object target = joinPoint.getTarget();
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    
            System.out.println("Before");
        }
    }
    
  4. 在配置文件中开启组件扫描(因为我在将相应的Bean注入到Spring中时,使用的是注解,如果使用Bean标签,这一步可以省略)

    <context:component-scan base-package="com.example">
    </context:component-scan>
    
  5. 在配置文件中配置AOP,将切面类(StudentService)中的methodBefore方法设置为前置通知

        <aop:config>
            <!--定义切面-->
            <aop:pointcut id="pt" expression="execution(* com.example.service..*.*(..))"/>
    
            <!--配置切面-->
            <aop:aspect ref="aspect">
                <!--配置通知类型-->
                <!-- <aop:before method="methodBefore" pointcut-ref="pt"/> -->
                <aop:before method="methodBefore" pointcut="com.example.aspect.Aspect.pt()"/>
            </aop:aspect>
        </aop:config>
    

    配置通知类型中有两种写法,一种是用pointcut-ref属性,值是定义切面时的id,另一种是使用pointcut属性,需要指定切点方法的全类名

运行结果:
在这里插入图片描述
可以看到成功将StudentService中的methodBefore方法设置为前置通知了

接下来讲一下复杂的通知如何配置,如下:

@AfterReturning(value = "point()",returning = "ret")
public void methodAfterReturning(JoinPoint joinPoint, Object ret){
	// 方法体
}
@AfterThrowing(value = "point()",throwing = "e")
public void methodAfterThrowing(JoinPoint joinPoint,Throwable e){
	// 方法体
}

@AfterReturning和@AfterThrowing是有两个参数的

以@AfterReturning为例,在切面类中添加对应的普通方法:

@Component
public class Aspect {
    @Pointcut("execution(* com.example.service..*.*(..))")
    public void pt(){
        System.out.println("");
    }
    public void methodBefore(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        Object target = joinPoint.getTarget();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        System.out.println("Before");
    }    
    public void methodAfterReturning(JoinPoint joinPoint, Object ret){
        System.out.println("AfterReturning: "+ ret);
    }
}
<aop:aspect ref="aspect">
    <!--配置通知类型-->
    <!-- <aop:before method="methodBefore" pointcut-ref="pt"/> -->
    <aop:before method="methodBefore" pointcut="com.example.aspect.Aspect.pt()"/>
    <aop:after-returning method="methodAfterReturning" pointcut-ref="pt" returning="ret"/>
</aop:aspect>

需要注意在设置AOP时,标签中有returning这样一个属性

运行结果:

在这里插入图片描述

3. 总结

xml配置AOP的重要步骤:

  1. 定义切面类,定义切点.
  2. 将目标类和切面类添加到Spring容器中(注解或Bean标签),如果是注解方式,需要添加组件扫描
  3. 在配置文件中配置AOP

在实际开发中,用注解配置AOP比较多,xml配置AOP了解即可

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
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 ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

比奇堡的天没有云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值