需结合上篇的AOP五种通知方式
XML配置AOP_java

LogAspectXml.java

package org.javaboy.aop;

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

public class LogAspectXml {
    public void before(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println(name+"方法开始执行了~");
    }

    /**
     * 后置通知,在目标方法执行之后执行
     * @param joinPoint
     */
    public void after(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println(name+"方法执行结束了...a");
    }

    /**
     * 返回通知,可以在该方法中获取目标方法的返回值,如果目标方法的返回值为void,则收到null
     * @param joinPoint
     * @param r 返回的参数名称 和这里方法的参数名一一对应
     */
    public void returning(JoinPoint joinPoint,Integer r){
        String name = joinPoint.getSignature().getName();
        System.out.println(name+"方法返回通知:"+r);
    }

    /**
     * 异常通知,当目标方法抛出异常时,该方法会触发
     * @param joinPoint
     * @param e 异常参数,和方法的参数名一一对应,注意异常的类型
     */
    public void afterThrowing(JoinPoint joinPoint,Exception e){
        String name = joinPoint.getSignature().getName();
        System.out.println(name+"方法异常通知:"+e.getMessage());
    }

    /**
     * 环绕通知,环绕通知是上面四种方法的集大成者,环绕通知的核心类似于在反射中执行方法
     * @param pjp
     * @return
     */
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        //这个方法有点类似于method.invoke 方法,我们可以在这个方法的前后分别添加日志,相当于前置/后置通知
        Object proceed = pjp.proceed(new Object[]{5,5});
        return proceed;
    }
}

resources下的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: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/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean class="org.javaboy.aop.LogAspectXml" name="logAspect"/>
    <bean class="org.javaboy.aop.service.MyCalculatorImpl" id="myCalculator"/>
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* org.javaboy.aop.service.*.*(..))"/>
        <aop:aspect ref="logAspect">
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
            <aop:after-returning method="returning" pointcut-ref="pointcut" returning="r"/>
            <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
            <aop:around method="around" pointcut-ref="pointcut"/>

        </aop:aspect>
    </aop:config>
</beans>

MainXml.java

package org.javaboy.aop;

import org.javaboy.aop.service.MyCalculator;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author 邓雪松 (づ ̄ 3 ̄)づ)
 * @create 2021-10-28-20-14
 */
public class MainXml {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        MyCalculator calculator = ctx.getBean(MyCalculator.class);
        calculator.add(6,7);
        calculator.min(6,7);
    }
}

完 ~