Course Spring annotation driven learning note AOP


It refers to the programming method that dynamically cuts a piece of code into the specified location during the program running;
1. Import aop module; Spring AOP: (spring aspects)
2. Define a business logic class (MathCalculator); print the log when the business logic is running (before the method, after the method is running, exception occurs to the method, xxx)
3. Define a log aspects class: the methods in the aspects class need to dynamically sense where MathCalculator.div runs and then execute;

Notification method:
Pre notification (@ Before): logStart: run before the target method (div) runs
 Post notification (@ After): logEnd: runs after the target method (div) runs (whether the method ends normally or abnormally)
Return notification (@ AfterReturning): logReturn: run after the target method (div) returns normally
 Exception notification (@ AfterThrowing): logException: run after an exception occurs in the target method (div)
Around notification (@ Around): dynamic agent, manually pushing the target method to run (joinpoint. Succeeded())

4. Mark when and where the target method of the tangent class runs (notification annotation);
5. Add the aspect class and business logic class (the class of the target method) to the container;
6. You must tell Spring which class is a tangent class (add an annotation to the tangent class: @ Aspect)
[7] Add @ EnableAspectJAutoProxy [enable annotation based aop mode] to the configuration class
Many @ EnableXXX in Spring


AOP development

Add business logic class

public class MathCalculator {
	public int div(int i,int j){
		return i/j;	


Add tangent class

public class LogAspects {
	//Extract common pointcut expressions
	//1. This kind of reference
	//2. Other tangent references
	@Pointcut("execution(public int com.atguigu.aop.MathCalculator.*(..))")
	public void pointCut(){};
	//@Before before the target method; pointcut expression (specifies which method to cut in)
	public void logStart(JoinPoint joinPoint){
		Object[] args = joinPoint.getArgs();
		System.out.println(""+joinPoint.getSignature().getName()+"function...@Before:The parameter list is:{"+Arrays.asList(args)+"}");
	public void logEnd(JoinPoint joinPoint){
	//JoinPoint must appear first in the parameter table
	public void logReturn(JoinPoint joinPoint,Object result){
		System.out.println(""+joinPoint.getSignature().getName()+"Normal return...@AfterReturning:Operation result:{"+result+"}");
	public void logException(JoinPoint joinPoint,Exception exception){
		System.out.println(""+joinPoint.getSignature().getName()+"Abnormal... Exception information:{"+exception+"}");


Add configuration class

public class MainConfigOfAOP {
	//Add business logic class to container
	public MathCalculator calculator(){
		return new MathCalculator();

	//Tangent class added to container
	public LogAspects logAspects(){
		return new LogAspects();

Test class

public void test01() {
	AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(

	//1. Don't create objects yourself
	MathCalculator mathCalculator1 = new MathCalculator();
	mathCalculator1.div(1, 1);
	MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);
        mathCalculator.div(1, 10);

Operation result



Three steps to implement AOP
1) Add business logic components and Aspect classes to the container; tell Spring which is the Aspect class (@ Aspect)
2) Annotate each notification method on the tangent class to tell Spring when and where to run (pointcut expression)
3) Enable annotation based aop mode; @ EnableAspectJAutoProxy

188 original articles published, 20 praised, 20000 visited+
Private letter follow

Tags: Spring Programming calculator

Posted on Sun, 09 Feb 2020 23:20:02 -0500 by reddymade