The use of the spring boot base for unified processing of request logs using AOP

Source: Internet
Author: User

Aspect-oriented (AOP) Aspect oriented programming is a programming paradigm that is independent of language and is a programming idea, which is one of the two core of spring. Do not know can see the net of the video tutorial. Here are the key codes that have been compiled after the individual learning video.
Adding AOP Dependencies

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

Create a processing file, add @Aspect annotations on the class, and then add the class to the spring container with @Component

Package com.xu.demo_springboot_01.aspect;Import Org.aspectj.lang.JoinPoint;Import org.aspectj.lang.annotation.*;Import Org.slf4j.Logger;Spring comes with a log frameImport Org.slf4j.LoggerFactory;Import org.springframework.stereotype.Component;Import Org.springframework.web.context.request.RequestContextHolder;Import org.springframework.web.context.request.ServletRequestAttributes;Import Javax.servlet.http.HttpServletRequest;@Aspect@ComponentBring this class into the spring container.PublicClassHttpaspect {PrivateFinalstatic Logger Logger = Loggerfactory.getlogger (Httpaspect.class);parameter is the class name that is currently used@Pointcut ("Execution (Public * com.xu.demo_springboot_01.controller.) girlcontroller.* (..)) ")The method to be processed, package name + class name + method namePublicvoidCut () {}@Before ("Cut ()")Perform the following methods before calling the method @Pointcut the callout abovePublicvoidDobefore (Joinpoint joinpoint) {Used to get the class method servletrequestattributes attributes = (servletrequestattributes) Requestcontextholder.getrequestattributes (); HttpServletRequest request = Attributes.getrequest ();URL Logger.info ("URL ={}", Request.getrequesturi ());Method Logger.info ("Method={}", Request.getmethod ());IP logger.info ("Ip={}", Request.getremoteaddr ());Class Method Logger.info ("Class_method={}", Joinpoint.getsignature (). Getdeclaringtypename () +‘.‘ + joinpoint.getsignature (). GetName ());//get class Name and class method //parameter Logger.info ( " args={} ", Joinpoint.getargs ()); }  @After ( "cut ()") // The public void doafter () {logger.info ( "----Doafter-----------");}  @AfterReturning (returning =  "obj", pointcut =  "cut ()") //executed after calling the method labeled above @Pointcut. Used to get the return value public void  Doafterreturning (Object obj) {logger.info ( "response={}", obj.tostring ());}} 

PS: Guide Package to note that the Guide pack should be noted that the guide pack should be noted ~
注://记录日志变量的方法logger.info("url ={}",request.getRequestURI());//逗号后面跟要记录的变量//记录日志常量logger.info("url");

The use of the spring boot base for unified processing of request logs using AOP

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.