spring_(18)Spring _返回通知&异常通知&环绕通知

例子程序

基本结构

在这里插入图片描述

ArithmeticCalculator.java

package com.spring.aop;

public interface ArithmeticCalculator {

    int add(int i, int j);
    int sub(int i, int j);

    int mul(int i, int j);
    int div(int i, int j);


}

ArithmeticCalculatorImpl.java

package com.spring.aop;

import org.springframework.stereotype.Component;

@Component("arithmeticCalculatorImpl")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

    @Override
    public int add(int i, int j) {

        int result = i+j;

        return result;
    }

    @Override
    public int sub(int i, int j) {

        int result = i-j;

        return result;
    }

    @Override
    public int mul(int i, int j) {

        int result = i*j;

        return result;
    }

    @Override
    public int div(int i, int j) {

        int result = i/j;

        return result;
    }
}

LogginAspect.java

package com.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class LogginAspect {

    /**
     * 在com.spring.aop.ArithmeticCalculator 接口的每一个实现类的方法开始之前执行一段代码
     */
    @Before("execution(public int com.spring.aop.ArithmeticCalculator.*(..))")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("The method "+methodName+" begins with "+Arrays.asList(args));
    }

    /**
     * 在方法之后执行的代码。无论是否出现异常
     * @param joinPoint
     */
    @After("execution(public int com.spring.aop.ArithmeticCalculator.*(..))")
    public void afterMethod(JoinPoint joinPoint){

        String methodName = joinPoint.getSignature().getName();

        System.out.println("The method "+methodName+" ends ");
    }

    /**
     * 在方法正常结束后执行的代码
     * 返回通知是可以访问到方法的返回值的
     * @param joinPoint
     */
    @AfterReturning(value = "execution(public int com.spring.aop.ArithmeticCalculator.*(..))",
                    returning = "result")
    public void afterReturning(JoinPoint joinPoint,Object result){

        String methodName = joinPoint.getSignature().getName();

        System.out.println("The method "+methodName+" ends with "+ result);
    }

    /**
     * 在目标方法出现异常时会执行的代码。
     * 可以访问到异常对象;且可以指定在出现特定异常时再执行通知代码
     * @param joinPoint
     * @param ex
     */
    @AfterThrowing(value = "execution(public int com.spring.aop.ArithmeticCalculator.*(..))",
                    throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Exception ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method "+methodName+" occurs exception "+ ex);
    }

    /**
     * 环绕通知需要携带ProceedingJoinPoint类型的参数。
     * 环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法
     * 且环绕通知必须有返回值,返回值即为目标方法的返回值
     */
    @Around("execution(public int com.spring.aop.ArithmeticCalculator.*(..))")
    public Object aroundMethod(ProceedingJoinPoint pjd){

        Object result = null;
        String methodName = pjd.getSignature().getName();

        //执行目标方法
        try{
            //前置通知
            System.out.println("环绕The method "+methodName+" begins with "+Arrays.asList(pjd.getArgs()));
            result = pjd.proceed();
            //返回通知
            System.out.println("环绕The method "+methodName+" ends with "+result);
        }catch (Throwable e){
            //异常通知
            System.out.println("环绕The method "+methodName+" occurs exception:" + e);
            throw new RuntimeException(e);
        }
        //后置通知
        System.out.println("环绕The method  "+methodName+" ends");
        return result;
    }
}

applicationContext.xml

<?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">

    <!--配置自动扫描的包-->
    <context:component-scan base-package="com.spring.aop"></context:component-scan>

    <!--配置自动为匹配aspectJ注解的Java类生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

Main.java

package com.spring.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args){

        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("arithmeticCalculatorImpl");

        int result = arithmeticCalculator.add(1,2);
        System.out.println("result:"+result);

        result = arithmeticCalculator.div(1000,2);
        System.out.println("result:"+result);
    }
}

运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42036647/article/details/84567303