Spring源码学习(十六)--基于xml配置方式进行Aop测试

AOP有三种实现方式,这里介绍的是基于xml配置方式

  1. 基于xml配置方式
  2. Spring纯注解配置aop
  3. 动态代理的方式实现AOP

使用案例进程测试

1. 目标对象

在这里插入图片描述

package com.xizi.service;

public interface UserService {
    public  void add(String name, int age);
    public  void update(String name, int age);
}

在这里插入图片描述

package com.xizi.service;

public class UserServiceImpl implements UserService {
    public void add(String name, int age) {
        System.out.println("增加了一个用户111111111");
    }


    public void update(String name, int age) {
        System.out.println("更新了一个用户11111111111111");
        System.out.println("==抛出异常==:" + 1 / 0);
    }
}

2. 切面类

在这里插入图片描述

自定义切面类

package com.xizi.diy;
import org.aspectj.lang.ProceedingJoinPoint;

public class DiyPointCut {
    /**
     * 前置增强
     */
    public void beforeAdvice(String name, int age) {
        System.out.println("==前置增强,name:" + name + ",age:" + age);
    }

    /**
     * 后置异常增强
     */
    public void afterExceptionAdvice(String name, int age) {
        System.out.println("==后置异常增强,name:" + name + ",age:" + age);
    }

    /**
     * 后置返回增强
     */
    public void afterReturningAdvice(String name, int age) {
        System.out.println("==后置返回增强,name:" + name + ",age:" + age);
    }

    /**
     * 后置最终增强
     */
    public void afterAdvice(String name, int age) {
        System.out.println("==后置最终增强,name:" + name + ",age:" + age);
    }

    //环绕增强
    public Object roundAdvice(ProceedingJoinPoint p, String name, int age) {
        System.out.println("==环绕增强开始,name:" + name + ",age:" + age);
        Object o = null;
        try {
            o = p.proceed();
            Object[] args = p.getArgs();
            if (null != args) {
                for (int i = 0; i < args.length; i++) {
                    System.out.println("==环绕增强参数值:" + args[i]);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("==环绕增强结束,name:" + name + ",age:" + age);
        return o;
    }
}

3. 配置文件

在这里插入图片描述

  1. 使用表达式来配置切入点,expression="execution(* com.xizi.service..(…)) and args(name,age),具体格式如下execution(<访问修饰符>空格<类全名>.<方法名>(<参数>)<异常>)
<?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
                https://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/aop
                https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--方式二:自定义类-->

    <!-- 目标对象 -->
    <bean id="userService" class="com.xizi.service.UserServiceImpl"/>
    <!-- 自定义切面类-->
    <bean id="diy" class="com.xizi.diy.DiyPointCut"/>

    <!--AOP配置-->
        <aop:config proxy-target-class="true">
            <!--自定义切面 ref要引用的类-->
            <aop:aspect ref="diy" order="0">
                <!--切入点-->
                <aop:pointcut id="pointcut" expression="execution(* com.xizi.service.*.*(..)) and args(name,age)"/>
                <!--通知-->
                <!--前置增强,在切入点选择的方法之前执行-->
                <aop:before method="beforeAdvice" pointcut-ref="pointcut" arg-names="name,age"/>
                <!--后置异常增强,在切入点选择的方法抛出异常时执行-->
                <aop:after-throwing method="afterExceptionAdvice" pointcut-ref="pointcut" arg-names="name,age"/>
                <!--后置返回增强,在切入点选择的方法正常返回时执行-->
                <aop:after-returning method="afterReturningAdvice" pointcut-ref="pointcut" arg-names="name,age"/>
                <!--后置最终增强,在切入点选择的方法返回时执行,不管是正常返回还是抛出异常都执行-->
                <aop:after method="afterAdvice" pointcut-ref="pointcut" arg-names="name,age"/>
                <aop:around method="roundAdvice" pointcut-ref="pointcut" arg-names="p,name,age"/>
            </aop:aspect>
        </aop:config>

</beans>

4. 测试类及结果

测试这个不带异常的方法

    public static void main(String[] args) {
        ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是接口
        UserService userService = (UserService) context.getBean("userService");
        userService.add("戏子",123);
    }

在这里插入图片描述

测试这个带异常的方法

    public static void main(String[] args) {
        ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是接口
        UserService userService = (UserService) context.getBean("userService");
        userService.update("戏子",123);
    }

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值