There is the concept of flying in the sky, there must be the realization of landing

  • Concept + code implementation is the feature of this article, the tutorial will cover complete graphic tutorials, code cases
  • The end of the article supporting self-test interview questions, learn more solid technology self-test
  • The concept ten times is not as good as the code once, my friend, I hope you can type all the code cases in the article once

Big brother big sister happy New Year, like forwarding do not less

SpringBoot graphic series tutorials technical outline

Teacher Deer’s Java notes

SpringBoot illustrated tutorial series article directory

  1. SpringBoot图文教程1 “concept + case mind mapping” “basics”
  2. Use “logback” and “log4j” to log.
  3. SpringBoot graphic tutorial 3 – “‘ first love ‘complex” integrated Jsp
  4. 4 – SpringBoot implementation file upload and download

preface

One is IOC, and the other is Aop. Aop is very easy to use in Spring. You can write Aop code in BOTH XML and annotations.

The use of AOP in SpringBoot doesn’t really change much, as SpringBoot is really just a further encapsulation of Spring and SpringMVC, Therefore, AOP aspect programming in the Spring framework is also supported in SpringBoot, but only annotated aspect programming is provided in SpringBoot for rapid development.

Use of Annotated AOP by SpringBoot

The rest of this article will be performed in the following demo. Go to Git repository: gitee.com/bingqilinpe…

1. Introduce dependencies

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-aop</artifactId>

</dependency>

Copy the code

2. Aop related annotations

/ * *

    @AspectUsed on a class to indicate that the class is an aspect

    @BeforeUsed on a method to indicate that the method is a pre-notification method

    @AfterUsed on a method means that the method is a post-notification method

    @AroundUsed on a method means that the method is a wrap method

    @AroundUsed on a method means that the method is a wrap method

    @order(number) used in a class, the smaller the number, the earlier the entry

* * /


/ * *

Surround, pre, post all exist

Go into the surround first, in the front, out of the front, out of the surround, in the back, out of the back

* * /


Copy the code

Lead plane

/ * *

@AspectUsed on a class to indicate that the class is an aspect

@ComponentUsed to tell SpringBoot that creating an object for the current class is equivalent to@Service

* /


@Aspect

@Component

public class MyAspect {

    The argument to the annotation used to indicate that the method is a pre-notification method is the pointcut expression

    @Before("execution(* com.lu.service.*.*(..) )")

    public void before(JoinPoint joinPoint){

        System.out.println("Advance notice");

//JoinPoint can use this object to get information about the object to be accessed

        joinPoint.getTarget();// Target object

        joinPoint.getSignature();// Method signature

        joinPoint.getArgs();// Method parameters

    }

}

Copy the code

The rear section

@Aspect

@Component

public class MyAspect {

    @After("execution(* com.lu.service.*.*(..) )")

    public void before(JoinPoint joinPoint){

        System.out.println("Post notification");

        joinPoint.getTarget();// Target object

        joinPoint.getSignature();// Method signature

        joinPoint.getArgs();// Method parameters

    }

}

Copy the code

Note: Neither pre-notification nor post-notification returns a value, and the method argument is JoinPoint

Around the plane

@Aspect

@Component

public class MyAspect {

    @Around("execution(* com.lu.service.*.*(..) )")

    public Object before(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        System.out.println("Enter surround notification");

        proceedingJoinPoint.getTarget();// Target object

        proceedingJoinPoint.getSignature();// Method signature

        proceedingJoinPoint.getArgs();// Method parameters

        Object proceed = proceedingJoinPoint.proceed();// This step must exist

        System.out.println("Return to surround notification after target method executes");

        return proceed;// Return the target method return value

    }

}

Copy the code

Note: There is a return value for the surround notification and the parameter is ProceedingJoinPoint

  • If the release is not performed, the target method is not executed
  • Once released, the return value of the target method must be returned, otherwise the caller cannot accept the returned data

conclusion

The above is the simple use of AOP in SpringBoot, today in fact, just a simple test of THE Api of AOP, about the application of AOP, such as: logging information through AOP, operating redis cache through AOP and other practical applications, will be written in the subsequent article.

Congratulations on completing this chapter. A round of applause! If this article is helpful to you, please help to like, comment, retweet, this is very important to the author, thank you.

Let’s review the learning objectives of this article again

  • Grasp the use of AOP in SpringBoot

To learn more about SpringBoot, stay tuned for this series of tutorials.

Below, I have prepared some self-test interview questions and project cases for my friend Meng. I hope you can become a hot iron and consolidate your knowledge.

Test your answers to the previous interview questions

Meet exam highlights gitee.com/bingqilinpe…

Self-test interview questions (see next section for answers)

  • What are the common annotations for annotated Aop

The last self-test project to achieve small case answers

See code cloud warehouse gitee.com/bingqilinpe…

Self-test implementation project short case (see next issue for answers)

This demand:

Practice article demo

Ask for attention, ask for likes, ask for retweets

Welcome to pay attention to my public account: Teacher Lu’s Java notes, will update Java technology graphic tutorials and video tutorials in the long term, Java learning experience, Java interview experience and Java actual combat development experience.