• Spring入门第二十一课


    切面优先级

    先看代码:

    package logan.study.aop.impl;
    
    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);
        
    
    }
    package logan.study.aop.impl;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
    
        @Override
        public int add(int i, int j) {
            // TODO Auto-generated method stub
            int result = i + j;
            return result;
        }
    
        @Override
        public int sub(int i, int j) {
            // TODO Auto-generated method stub
            int result = i - j;
            return result;
        }
    
        @Override
        public int mul(int i, int j) {
            // TODO Auto-generated method stub
            int result = i * j;
            return result;
        }
    
        @Override
        public int div(int i, int j) {
            // TODO Auto-generated method stub
            int result = i / j;
            return result;
        }
    
    }
    package logan.study.aop.impl;
    
    import java.util.Arrays;
    import java.util.List;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    //把这个类声明为一个切面:需要把该类放入到IOC容器中,在声明为一个切面
    @Order(2)
    @Aspect
    @Component
    public class LoggingAspect {
        
        /**
         * 环绕通知需要携带ProceedingJoinPoint类型的参数
         * 环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法
         * 且环绕通知必须有返回值,返回值即为目标方法的返回值
         */
        
        @Around("execution(public int logan.study.aop.impl.ArithmeticCalculator.*(int, int))")
        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 occurs exception:" + e);
                
            }
            //后置通知
            System.out.println("The method " + methodName + "ends");
            return result;
        }
    
    }
    package logan.study.aop.impl;
    
    import java.util.Arrays;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    //可以
    @Order(1)
    @Aspect
    @Component
    public class VlidationAspect {
        @Before("execution(public int logan.study.aop.impl.ArithmeticCalculator.*(int, int))")
        public void validateArgs(JoinPoint joinPoint){
            System.out.println("validate: " + Arrays.asList(joinPoint.getArgs()));
        }
    
    }
    <?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-4.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    
        <context:component-scan base-package="logan.study.aop.impl"></context:component-scan>
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
        
    
    </beans>
    package logan.study.aop.impl;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Main {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            //1.创建Spring的IOC容器
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            //2.从IOC容器里面获取bean实例
            ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);
            //使用Bean
            int result = arithmeticCalculator.add(3, 6);
            System.out.println(result);
            
            result = arithmeticCalculator.div(3, 0);
            System.out.println(result);
    
        }
    
    }

    返回结果:

    五月 28, 2017 7:03:16 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Sun May 28 07:03:16 CST 2017]; root of context hierarchy
    五月 28, 2017 7:03:16 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [applicationContext.xml]
    validate: [3, 6]
    The methodadd begins with [3, 6]
    The methodadd ends with 9
    The method addends
    9
    validate: [3, 0]
    The methoddiv begins with [3, 0]
    The method occurs exception:java.lang.ArithmeticException: / by zero
    The method divends
    Exception in thread "main" org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract int logan.study.aop.impl.ArithmeticCalculator.div(int,int)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:227)
        at com.sun.proxy.$Proxy9.div(Unknown Source)
        at logan.study.aop.impl.Main.main(Main.java:18)

    可以看到我们这里有两个切面,但是哪个切面先执行?哪个切面后执行?

    我们可以在切面上加@Order(1),这样来规定哪个先执行,哪个后执行,数字越小,优先级越高,越先执行。

  • 相关阅读:
    [BZOJ3745][Coci2015]Norma
    [OJ#15]TR #2 画心
    [BZOJ3585][BZOJ3339]mex
    [OJ#63]树句节够提
    [LOJ#2255][BZOJ5017][Snoi2017]炸弹
    [LOJ#525]「LibreOJ β Round #4」多项式
    【字符编码】Java字符编码详细解答及问题探讨
    【Java基础】序列化与反序列化深入分析
    【目录】JVM目录
    【知识积累】随机数生成的几种方法
  • 原文地址:https://www.cnblogs.com/LoganChen/p/6914956.html
Copyright © 2020-2023  润新知