XML配置aop实例

直接上代码,首先我的结构目录是:

 

 

 1、新建一个MathAspect.java

public class MathAspect {

    //前置通知
    public void logBefore(JoinPoint joinPoint) {//joinPoint 可以用来获取方法参数以及方法名
        Object[] args = joinPoint.getArgs();
        System.out.println("" + joinPoint.getSignature().getName() + "除法运行前。。参数列表是:{" + Arrays.asList(args) + "}");
    }

    //后置通知
    public void logAfter(JoinPoint joinPoint) {
        System.out.println("" + joinPoint.getSignature().getName() + "除法运行结束。。");
    }

    //返回通知
    public void logReturn(JoinPoint joinPoint, int result) {//返回结果必须与xml文件中returning同名,即returning="result"
        System.out.println("" + joinPoint.getSignature().getName() + "除法正常返回。。返回结果是:{" + result + "}");
    }

    //异常通知
    public void logException(JoinPoint joinPoint, Exception e) {//异常信息必须与xml文件中的throwing同名,即throwing="e"
        System.out.println("" + joinPoint.getSignature().getName() + "除法异常。。。异常是:{" + e + "}");
    }
}

2、新建需要增强的类:

public class MathCalculator {

    public int div(int i, int j) {
        System.out.println("MathCalculator...div...");
        return i / j;
    }
}

3、aop配置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 http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--定义切面类-->
    <bean class="com.aoptest.aop.MathAspect" name="aspect"/>

    <!--定义目标类-->
    <bean class="com.aoptest.aop.MathCalculator" id="calculator"/>

    <!--aop配置-->
    <aop:config>
        <!--定义切点,其中匹配的是com.aoptest.aop.MathCalculator类下面所有参数返回值任意的方法-->
        <aop:pointcut id="pointcut" expression="execution(* com.aoptest.aop.MathCalculator.*(..))"/>
        <aop:aspect ref="aspect">
            <!--前置通知,method必须与切面类中的方法名相同(下同),并指定切点-->
            <aop:before method="logBefore" pointcut-ref="pointcut"/>
            <!--后置通知-->
            <aop:after method="logAfter" pointcut-ref="pointcut"/>
            <!--返回通知,返回值名与方法返回值名相同-->
            <aop:after-returning method="logReturn" pointcut-ref="pointcut" returning="result"/>
            <!--异常通知,异常名与方法返回的异常名相同-->
            <aop:after-throwing method="logException" pointcut-ref="pointcut" throwing="e"/>
        </aop:aspect>
    </aop:config>
</beans>

4、测试类

public class MathTest {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        MathCalculator calculator = (MathCalculator) context.getBean("calculator");


//        calculator.div(10, 1);
        calculator.div(10, 0);
    }
}

正常输出结果:

div除法运行前。。参数列表是:{[10, 1]}
MathCalculator...div...
div除法运行结束。。
div除法正常返回。。返回结果是:{10}

异常输出结果:

div除法运行前。。参数列表是:{[10, 0]}
MathCalculator...div...
Exception in thread "main" java.lang.ArithmeticException: / by zero
div除法运行结束。。
    at com.aoptest.aop.MathCalculator.div(MathCalculator.java:11)
div除法异常。。。异常是:{java.lang.ArithmeticException: / by zero}

 

posted @ 2020-07-15 21:32  杯莫停、  阅读(446)  评论(0)    收藏  举报