The execution order of the pointcuts of different annotations in the slice is: preprocessing, postprocessing, return processing / exception processing, which is well understood. However, if multiple tangent classes are defined and there are the same processing steps in them, if the processing order is not specified manually, they will execute in alphabetical order. If the execution order is specified, they will execute in the specified order. Let's explain how to specify the execution order of the same processing phase in the two ways of using AOP. Take the following service layer code for example:
package cn.jingpengchong.calculator.service; import org.springframework.stereotype.Service; @Service public class CalculatorService implements ICalculatorService { public int div(int a, int b) { return a/b; } }
Annotation method specifies execution order
In this way, the @ Order() annotation needs to be added in front of the tangent class. The smaller the parameter value in brackets, the earlier the execution.
1. Create a new module "cn.jingpengchong.aspect", and then create files "ArgsAspect.java" and "MethodAspect.java":
package cn.jingpengchong.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; //Specify second execution @Order(2) @Aspect @Component public class ArgsAspect { @Before("execution(public int cn.jingpengchong.calculator.service.CalculatorService.*(..))") public void before(JoinPoint jp) { Object[] args = jp.getArgs(); Signature signature = jp.getSignature(); String name = signature.getName(); System.out.println("Journal: The " + name + " method args:[" + args[0] + "," + args[1] + "]"); } }
package cn.jingpengchong.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; //Specify first execution @Order(1) @Aspect @Component public class MethodAspect { @Before("execution(public int cn.jingpengchong.calculator.service.CalculatorService.*(..))") public void before(JoinPoint jp) { Signature signature = jp.getSignature(); String name = signature.getName(); System.out.println("Journal: The " + name + " method begins"); } }
2. Automatic proxy is specified in spring's xml configuration file:
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
3. Write test class:
package cn.jingpengchong.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.jingpengchong.calculator.service.ICalculatorService; public class Test { public static void main(String[] args) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml"); ICalculatorService calculatorService = applicationContext.getBean(ICalculatorService.class); System.out.println(calculatorService.div(6, 2)); applicationContext.close(); } }
The operation results are as follows:
xml configuration mode specifies execution order
In this way, you need to add the order attribute to the < AOP: aspect > tag. The smaller the attribute value of the order attribute is, the earlier it is executed.
1. Create a new module "cn.jingpengchong.aspect", and then create files "ArgsAspect.java" and "MethodAspect.java":
package cn.jingpengchong.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; public class ArgsAspect { public void before(JoinPoint jp) { Object[] args = jp.getArgs(); Signature signature = jp.getSignature(); String name = signature.getName(); System.out.println("Journal: The " + name + " method args:[" + args[0] + "," + args[1] + "]"); } }
package cn.jingpengchong.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; public class MethodAspect { public void before(JoinPoint jp) { Signature signature = jp.getSignature(); String name = signature.getName(); System.out.println("Journal: The " + name + " method begins"); } }
2. Configure facets in spring's xml configuration file:
<bean id = "argsAspect" class = "cn.jingpengchong.aspect.ArgsAspect"/> <bean id = "methodAspect" class = "cn.jingpengchong.aspect.MethodAspect"/> <aop:config> <aop:pointcut expression="execution(public int cn.jingpengchong.calculator.service.CalculatorService.*(..))" id="pointCut"/> <aop:aspect ref="argsAspect" order="2"> <aop:before method="before" pointcut-ref="pointCut"/> </aop:aspect> <aop:aspect ref="methodAspect" order="1"> <aop:before method="before" pointcut-ref="pointCut"/> </aop:aspect> </aop:config>
3. Write test class:
package cn.jingpengchong.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.jingpengchong.calculator.service.ICalculatorService; public class Test { public static void main(String[] args) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml"); ICalculatorService calculatorService = applicationContext.getBean(ICalculatorService.class); System.out.println(calculatorService.div(6, 2)); applicationContext.close(); } }
The operation results are as follows: