🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
在同一个切面中定义多个通知响应同一个切点函数,执行顺序为声明顺序 ``` package com.zejian.spring.springAop.AspectJ; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; /** * Created by zejian on 2017/2/20. * Blog : http://blog.csdn.net/javazejian [原文地址,请尊重原创] */ @Aspect public class AspectOne { /** * Pointcut定义切点函数 */ @Pointcut("execution(* com.zejian.spring.springAop.dao.UserDao.deleteUser(..))") private void myPointcut(){} @Before("myPointcut()") public void beforeOne(){ System.out.println("前置通知....执行顺序1"); } @Before("myPointcut()") public void beforeTwo(){ System.out.println("前置通知....执行顺序2"); } @AfterReturning(value = "myPointcut()") public void AfterReturningThree(){ System.out.println("后置通知....执行顺序3"); } @AfterReturning(value = "myPointcut()") public void AfterReturningFour(){ System.out.println("后置通知....执行顺序4"); } } output====> 前置通知....执行顺序1 前置通知....执行顺序2 delete user...... 后置通知....执行顺序3 后置通知....执行顺序4 ``` 如果在不同的切面中定义多个通知响应同一个切点,进入时则优先级高的切面类中的通知函数优先执行,退出时则最后执行,如下定义AspectOne类和AspectTwo类并实现org.springframework.core.Ordered 接口,该接口用于控制切面类的优先级,同时重写getOrder方法,定制返回值,返回值\(int 类型\)越小优先级越大。其中AspectOne返回值为0,AspectTwo的返回值为3,显然AspectOne优先级高于AspectTwo ``` /** * Created by zejian on 2017/2/20. * Blog : http://blog.csdn.net/javazejian [原文地址,请尊重原创] */ @Aspect public class AspectOne implements Ordered { /** * Pointcut定义切点函数 */ @Pointcut("execution(* com.zejian.spring.springAop.dao.UserDao.deleteUser(..))") private void myPointcut(){} @Before("myPointcut()") public void beforeOne(){ System.out.println("前置通知..AspectOne..执行顺序1"); } @Before("myPointcut()") public void beforeTwo(){ System.out.println("前置通知..AspectOne..执行顺序2"); } @AfterReturning(value = "myPointcut()") public void AfterReturningThree(){ System.out.println("后置通知..AspectOne..执行顺序3"); } @AfterReturning(value = "myPointcut()") public void AfterReturningFour(){ System.out.println("后置通知..AspectOne..执行顺序4"); } /** * 定义优先级,值越低,优先级越高 * @return */ @Override public int getOrder() { return 0; } } //切面类 AspectTwo.java @Aspect public class AspectTwo implements Ordered { /** * Pointcut定义切点函数 */ @Pointcut("execution(* com.zejian.spring.springAop.dao.UserDao.deleteUser(..))") private void myPointcut(){} @Before("myPointcut()") public void beforeOne(){ System.out.println("前置通知....执行顺序1--AspectTwo"); } @Before("myPointcut()") public void beforeTwo(){ System.out.println("前置通知....执行顺序2--AspectTwo"); } @AfterReturning(value = "myPointcut()") public void AfterReturningThree(){ System.out.println("后置通知....执行顺序3--AspectTwo"); } @AfterReturning(value = "myPointcut()") public void AfterReturningFour(){ System.out.println("后置通知....执行顺序4--AspectTwo"); } /** * 定义优先级,值越低,优先级越高 * @return */ @Override public int getOrder() { return 2; } } output====> 前置通知..AspectOne..执行顺序1 前置通知..AspectOne..执行顺序2 前置通知....执行顺序1--AspectTwo 前置通知....执行顺序2--AspectTwo delete user ...... 后置通知....执行顺序4--AspectTwo 后置通知....执行顺序3--AspectTwo 后置通知..AspectOne..执行顺序4 后置通知..AspectOne..执行顺序3 ```