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