“This is the 14th day of my participation in the First Challenge 2022.

๐Ÿ‘จ๐ŸŽ“ Author: Bug Bacteria

โœ๏ธ blog: CSDN, Nuggets, etc

๐Ÿ’Œ public account: Magic House of the Circle of the Apes

๐Ÿšซ special statement: original is not easy, reprint please attach the original source link and this article statement, thank you for your cooperation.

๐Ÿ™ Copyright notice: part of the text or pictures in the article may come from the Internet or Baidu Encyclopedia, if there is infringement, please contact bug bacteria processing.

Hi, family. I’m the bug. Here I go again. Today we are going to talk about something, OK, and we will continue the Series of articles on SpringBoot. Hope to help more beginners quickly start!

In the process of reviewing articles, if you think the articles are helpful to you at all, please don’t be too mean with your likes and bravely light up the articles ๐Ÿ‘. Your likes (collect โญ๏ธ+ pay attention to ๐Ÿ‘จ port + message board) are the best encouragement and support for bugs on my creation path. Time does not abandon ๐Ÿƒ๐Ÿปโ™€๏ธ, creation stopped ๐Ÿ’•, refueling ๐Ÿป

One, foreword

In installment, we are focus on the concept of AOP, and some related knowledge points, if you have a friend is not too clear, so don’t worry, by the issue of teaching content, you will have some in-depth recognition for it, after all is pure concept, let not contact or contact with little buddy completely absorbed, is certainly very difficult very difficult task.

In this installment, we’ll take a hands-on look at how to integrate AOP into your project and use it briefly, and in the next installment, I’ll actually show you how to use AOP to implement custom annotations to log the interface business. Okay? Let’s not practice annotating code that is relevant to the hands-on business.

Anyway, let me start teaching in real combat! Please look at the following ~

Second, AOP use

1. Custom annotation class implementation:

package com.example.demo.annotation; import com.example.demo.enums.LogTypeEnum; import java.lang.annotation.*; /** * Custom annotation class @useaop ** @author luoYong * @version 1.0 * @date 2022-01-20 17:29 */ @target (elementType.method) // The target position of the annotation,METHOD means that the annotation scope is on the METHOD; @retention (RetentionPolicy.runtime) @documented @interface UseAop {}Copy the code

To expand:

The @target annotation can be used for one of the following purposes :(the scope of the annotation described)

  • @target (elementtype.type) // For interfaces, classes, enumerations, annotations
  • @target (elementType.field) // Constants for fields and enumerations
  • @ Target (ElementType. METHOD) / / METHOD
  • @target (elementtype.parameter) // The PARAMETER of the method
  • @target (elementtype.constructor) // CONSTRUCTOR
  • @target (elementType.local_variable) // Local variable
  • @ Target (ElementType ANNOTATION_TYPE) / / annotation
  • @ Target (ElementType. PACKAGE) / / PACKAGE

Now that the custom annotations are defined, we need to define another aspect class.

2. Implementation of section class:

package com.example.demo.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; ** @author luoYong * @version 1.0 * @date 2022/1/24 15:32 */ @aspect @component public class UseAopAspect {/** * define the Pointcut @pointcut; Insert code at annotation location * * @annotation denotes all methods that annotate an annotation * value fills in the project directory of your custom annotation */ @pointcut ("@annotation() Com. Example. The demo. The annotation. UseAop) ") public void printWord () {} / * * * id a front-facing enhancement method, BeforeAdvice */ @before ("printWord()") public void BeforeAdvice () {system.out.println ("-- execute 3: @before --"); } /** * final enhancements */ @afteradvice () public void afterAdvice() {system.out.println () @After---"); } / @afterreturning ("printWord()") public void afterReturningAdvice() { System.out.println("-- execute 5: @afterreturning --"); } /** * calls the notification after the target method throws an exception. * / @ AfterThrowing (printWord "()") public void afterAdviceThrowingAdvice () {System. Out. Println (" -- -- -- to perform 6: @AfterThrowing---"); } /** * surround enhancement, MethodInterceptor */ @around ("printWord()") public Object aroundAdvice(ProceedingJoinPoint ProceedingJoinPoint) { System.out.println("-- execute 1: @around --"); try { Object result = proceedingJoinPoint.proceed(); // The interface returns no value if the result is not returned. return result; } catch (Throwable t) { t.printStackTrace(); } system.out.println ("-- execute 2: @around: error "); return new Object(); }}Copy the code

Now that we’ve created the surface, the important part is to define a random interface method, then modify it with the annotations we just defined, and see what happens to the console.

3. Add a controller

Add a controller for test access. Let’s take UserController as an example. You can create one of these, you can write an empty method or you can return a string.

For example, I just took the existing query user list interface to do a test ah. See below:

package com.example.demo.controller; import com.example.demo.annotation.UseAop; import com.example.demo.entity.UserEntity; import com.example.demo.vo.UserInfoVo; import com.example.demo.dao.UserMapper; import com.example.demo.model.QueryUserInfoModel; import com.example.demo.service.UserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; /** * User management dispenser */ @RestController@RequestMapping("/user") @API (tags = "user management module ",description =" user management module ") public class UserController { @Autowired private UserService userService; @useaop / @getmapping ("/get-users1") @apiOperation (value = "db1 Public List<UserEntity> getUserList() {return userService.getUsers(); }}Copy the code

As you can see, I just added the custom annotation @useaop to my getUserList() method, right? Do you remember to take it, and then add custom annotations versus no custom annotations, and compare them, and see if it’s going to print out to the cut? Let’s wait and see

  • Here is a screenshot of the console print without custom annotations:

  • Here is a screenshot of the console print with custom annotations added:

What do you see in the result above? The advice in the section class has been executed and printed out. Note that if all the advice is given, you can see that there is a sequential order of advice, but generally the five types of advice are not used together at the same time. A common approach is to use @afterreturning in projects, such as logging interface calls (e.g. Get the caller account, call interface parameters, caller IP, call time, call return data, call operation type, etc.), is to call the notification to get after the successful execution of the target method, this operation is generally more. There is also the authority check, that is, before calling the target method to cut in, and then obtain the account information of the caller for the authority check to determine whether the user has the authority to interface access.

Basically these are the common gameplay, and we will talk about more in the future, or if you have better gameplay, please let me know in the comments section below, learn from each other and make progress together.

. .

OK, that’s all for this episode. If you have any questions, feel free to comment in the comments section. See you next time.

Three, the past popular recommendation

  • Springboot series (15) : AOP implements custom annotations for business logging! Have you ever played?

  • Springboot series (14) : Redis Zero-based teaching, you deserve it!

  • Springboot Series (thirteen) : How to project integrated Swagger online interface documentation, will you?

  • Springboot series (12) : How to code to send email reminders, have you written?

  • . .

If you want to learn more, you can pay attention to the bug bug column “SpringBoot Zero-based Introduction”, from scratch, from zero to one! Hope I can help you.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

โ˜˜๏ธ Be who you want to be, there is no time limit, you can start whenever you want,

๐Ÿ€ You can change from now on, you can also stay the same, this thing, there are no rules to speak of, you can live the most wonderful yourself.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

โ€‹

๐Ÿ’“ If this article is helpful to you, please leave a like! (# ^. ^ #);

๐Ÿ’ if you like the article shared by bug fungus, please give bug fungus a point of concern! (เน‘ ‘แด— โ€ต เน‘);

๐Ÿ’— if you have any questions about the article, please also leave a message at the end of the article or add a group [QQ communication group :708072830];

๐Ÿ’ž In view of the limited personal experience, all views and technical research points, if you have any objection, please directly reply to participate in the discussion (do not post offensive comments, thank you);

๐Ÿ’• copyright notice: original is not easy, reprint please attach the original source link and this article statement, all rights reserved, piracy will investigate!! thank you