Java学习--spring框架--入门返回通知、异常通知、环绕通知

本文详细介绍了Spring框架中的返回通知、异常通知和环绕通知。首先讲解了配置XML文件的重要性,接着分别阐述了返回通知如何在程序执行完成后获取结果,异常通知如何在程序出错时触发,以及环绕通知如何实现类似动态代理的功能。

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

首先要配置好xml文件

<!--    指定要扫名的包-->
    <context:component-scan base-package="spring.aop.aop"></context:component-scan>
<!--    使AspectJ起着作用:自动为匹配的类生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

1. 返回通知

返回通知可以,必须程序执行完成,可以获取程序运行完成的结果
函数的代码

@Aspect
@Component
public class LoggingAspect {
  /**
     * 在方法正常结束后执行的代码,
     * 返回的通知是可以访问方法的返回值!!!
     */
    @AfterReturning(value = "execution(* spring.aop.aop.ArithmeticCalculator.*(..))",returning = "r")
    public void afterReturning(JoinPoint joinPoint,Object r){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method "+methodName+" "+args+" result is " + r);
    	}
    }

主函数

 public static void main(String[] args) {
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("spring-aop.xml");
        ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) applicationContext.getBean("ArithmeticCalculatorImpl");

        System.out.println("result: "+arithmeticCalculator.add(1,7));
    }

运行的结果

在这里插入图片描述

2. 异常通知

当程序执行出现异常才会触发的通知,也可以指定触发特定异常的类型,只需要在接受的参收设定为特定的异常类型
函数代码

@Aspect
@Component
public class LoggingAspect {
    /**
     * 在目标代码出现异常时会执行代码
     * 可以访问到异常对象
     * 可以指定出现异常时在执行通知代码
     */
    @AfterThrowing(value = "execution(* spring.aop.aop.ArithmeticCalculatorImpl.*(..))",throwing = "exception")
    public void afterThrowing(JoinPoint joinPoint,Exception exception){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method "+methodName+" exception is "+exception);
    }
}

主函数

    public static void main(String[] args) {
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("spring-aop.xml");
        ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) applicationContext.getBean("ArithmeticCalculatorImpl");

        System.out.println("result: "+arithmeticCalculator.div(1,0));
    }

运行结果

在这里插入图片描述

3.环绕通知

环绕通知,类似于动态代理,在不同的地方调用函数,就类似于不同的通知

@Aspect
@Component
public class LoggingAspect {
    /**
     * 环绕通知需要携带ProceedingJoinPoint类型的参数
     * 环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行该目标方法
     * 环绕通知必须有返回值,返回值即为目标方法的返回值
     */
    @Around("execution(* spring.aop.aop.ArithmeticCalculatorImpl.*(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println("Around");
        Object result = null;
        String methodName = proceedingJoinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(proceedingJoinPoint.getArgs());
        try {
//            前置通知
            System.out.println("The method "+methodName+" begins with "+args);
            result = proceedingJoinPoint.proceed();
//            返回通知
            System.out.println("The method "+methodName+" "+args+" result is " + result);
        } catch (Throwable throwable) {
//            异常通知
            System.out.println("The method "+methodName+" exception is "+throwable);
        }
//        后置通知
        System.out.println("The method "+methodName+" ends");
        return result;
    }
}

主函数

    public static void main(String[] args) {
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("spring-aop.xml");
        ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) applicationContext.getBean("ArithmeticCalculatorImpl");

        System.out.println("result: "+arithmeticCalculator.add(1,7));
    }

运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值