Spring AOP execution order of [the] Spring

The execution order of different section of annotation entry points: pre-processing, post-processing, returns to the processing / exception handling, is well understood in this order. However, if a plurality of facets defined classes, and there are aspects of the same process, if not manually specify processing order, they will be performed in alphabetical order, the execution order if specified, will be performed in the specified order. Here to explain how to use AOP are two ways to specify the order of execution of the same processing chain. Or in the following process layer service code as an 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;
	}
}

Annotations specify execution order

This approach requires the front section together @Order class () annotations, brackets parameter value is smaller, the first run.

1, the new module "cn.jingpengchong.aspect", and then a new "ArgsAspect.java" and "MethodAspect.java" file:
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;

//指定第二个执行
@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("日志: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;

//指定第一个执行
@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("日志:The " + name + " method begins");
	}
}
2, spring xml configuration file to specify automatic proxy:
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
3, write test classes:
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();
	}
}

Results are as follows:
Here Insert Picture Description

xml configuration specified execution order

This approach requires the <aop: aspect> tag a smaller order attribute property value, the property order, the first run.

1, the new module "cn.jingpengchong.aspect", and then a new "ArgsAspect.java" and "MethodAspect.java" file:
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("日志: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("日志:The " + name + " method begins");
	}
}
2, spring xml configuration file Configuration section:
<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 classes:
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();
	}
}

Results are as follows:
Here Insert Picture Description

Published 128 original articles · won praise 17 · views 2734

Guess you like

Origin blog.csdn.net/qq_43705275/article/details/104181197