Use of SPRINGBOOT9--AOP (this example shows the unified processing Web request log)

Source: Internet
Author: User
Tags aop

AOP is the abbreviation of Aspect oriented programming, which means: face-cutting programming, through the pre-compilation method and run-time dynamic agent implementation of the unified maintenance of the program functions of a technology.

AOP is an important part of the spring framework by defining an entry point for an existing program and then cutting into different executions before and after it, such as the usual:

Open database connections/Close database connections, open transactions/close transactions, log logs, and so on. AOP does not break the original program logic, so it can be very good to isolate the various parts of the business logic,

Thus, the coupling degree between the parts of business logic is reduced, the reusability of the program is improved, and the efficiency of the development is improved.

Preparatory work:

Introduced in POM:

<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>

* Unified processing of Web request logs using AOP in Spring boot
* Use @aspect Annotations to define a Java class as a slice class
* Use @pointcut to define a pointcut, which can be a regular expression, such as all functions under a package in the following example, or it can be an annotation, and so on.
* Cut-in content in different positions as needed
* Use @before to cut into content at the beginning of the Pointcut
* Use @after to cut into content at the end of the Pointcut
* Use @afterreturning to cut into content after pointcut return content (can be used to process return values to do some processing)
* Use @around to cut into the content before and after the pointcut, and control when to execute the Pointcut's own content
* The processing logic used by @afterthrowing to handle the exception that occurs when the cut-in content section is thrown

/***
* In the Weblogaspect section, the tangent head and the two independent functions of Dobefore and doafterreturning are respectively implemented
* If we want to count the processing time of the request, we need to record the time at Dobefore and the doafterreturning
* The time spent on request processing is calculated from the current time and the time recorded at the beginning.
* So can we define a member variable in the Weblogaspect slice to be accessed with Dobefore and doafterreturning?
* Is there a sync problem?
* It is true that there are synchronization problems in defining the basic types directly here, so we can introduce threadlocal objects
*/

/***
* Optimization: The precedence of AOP facets
* Due to the implementation of AOP, the program is well decoupled, but also brings some problems, such as: We may be on the web layer to do multiple facets, verify the user, check the header information, and so on, this time will often encounter the processing order of the aspect of the problem.
* So, we need to define the priority of each facet, and we need @order (i) annotations to identify the priority of the slices. The smaller the value of I, the higher the priority. Suppose we still have a tangent that Checknameaspect used to verify that the name must be Didi,
* We set @order (10) for it, and the above weblogaspect is set to @order (5), so Weblogaspect has a higher priority, this time the order of execution is this:
* @order (5) is prioritized in @before and the contents of @order (10) are executed.
* The contents of @order (10) are prioritized in @after and @afterreturning, and the contents of @order (5) are executed.
* So we can summarize this:
* Operation before pointcut, by order value from small to large execution
* Operation after pointcut, by order value from large to small execution
*/

Use of SPRINGBOOT9--AOP (this example shows the unified processing Web request log)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.