Spring Cloud Spring Boot mybatis distributed microservice cloud architecture (forty) using AOP to uniformly process web request logs (1)

AOP is the abbreviation of Aspect Oriented Programming, which means: Aspect-oriented programming, a technology to achieve unified maintenance of program functions through pre-compilation and runtime dynamic agents. AOP is an important content in the Spring framework. It defines an entry point for the existing program, and then cuts into different execution contents before and after it. For example, the common ones are: open database connection/close database connection, open transaction/close transaction, Logging, etc. Based on AOP, the original program logic will not be destroyed, so it can well isolate each part of the business logic, thereby reducing the coupling between the various parts of the business logic, improving the reusability of the program, and improving the efficiency of development. .

The following mainly talks about two contents, one is how to introduce the Aop function in Spring Boot, and the other is how to use Aop as a facet to uniformly process the logs of web requests.

Ready to work

Because the web request needs to be cut to record the log, the web module is introduced first, and a simple hello request processing is created.

  • pom.xmlimport web module
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

     

  • Implement a simple request processing: return "hello xxx" by passing in the name parameter.
    
    @RestController
    public class HelloController {
    
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        @ResponseBody
        public String hello(@RequestParam String name) {
            return "Hello " + name;
        }
    
    }

    Next, we can perform aspect logging on the above /hello request.

    Introduce AOP dependencies

    Introducing AOP in Spring Boot is the same as introducing other modules. It is very simple. You only need pom.xmlto add the following dependencies to it:

    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    After the introduction of the AOP dependency package is completed, generally no other configuration is required. Maybe people who have used the annotation configuration method in Spring will ask whether it needs to be added in the main class of the program @EnableAspectJAutoProxyto enable it, but it is not actually required.

    You can see the default configuration properties of AOP below, where the spring.aop.autoproperties are enabled by default, that is to say, as long as AOP dependencies are introduced, the default has been increased @EnableAspectJAutoProxy.

    
    # AOP
    spring.aop.auto=true # Add @EnableAspectJAutoProxy.
    spring.aop.proxy-target-class=false # Whether subclass-based (CGLIB) proxies are to be created (true) as
     opposed to standard Java interface-based proxies (false).
    

    When we need to use CGLIB to implement AOP, we need to configure spring.aop.proxy-target-class=trueit, otherwise the standard Java implementation is used by default.

  • source code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325856469&siteId=291194637