First, it should be understood that AOP is a programming paradigm and a programming idea that is not related to specific computer programming languages, so it is not just Java, but other programming languages like. NET have AOP implementations. The idea of AOP is to separate the general logic from the business logic.
This article will briefly explain the application of AOP in spring boot through an example of an HTTP request, with the following steps:
The first step is to add a dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-aop</artifactid></dependency>
The second step is to create the class for the AOP method
Package*;//Define yourselfImportOrg.aspectj.lang.JoinPoint;Importorg.aspectj.lang.annotation.*;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.stereotype.Component;ImportOrg.springframework.web.context.request.RequestContextHolder;Importorg.springframework.web.context.request.ServletRequestAttributes;Importjavax.servlet.http.HttpServletRequest, @Aspect @component Public classHttpaspect {//using Org.slf4j.Logger, this is how spring implements the log Private Final StaticLogger Logger = Loggerfactory.getlogger (httpaspect.class); /*** Define an AOP scan path * The first annotation only scans the Aoptest method*/ //@Pointcut ("Execution (Public * com.aston.reader.controller.StudentController.aopTest ())")@Pointcut ("Execution (Public * com.aston.reader.controller.studentcontroller.* ())") Public voidlog () {}/*** Log logging at the beginning of an HTTP request*/@Before ("Log ()") Public voidDobefore (Joinpoint joinpoint) {servletrequestattributes attributes=(servletrequestattributes) requestcontextholder.getrequestattributes (); HttpServletRequest Request=attributes.getrequest (); //URLLogger.info ("url={}", Request.getrequesturi ()); //MethodLogger.info ("method={}", Request.getmethod ()); //IPLogger.info ("ip={}", Request.getremoteaddr ()); //class MethodLogger.info ("class={} and Method name = {}", Joinpoint.getsignature (). Getdeclaringtypename (), Joinpoint.getsignature (). GetName ()); //ParametersLogger.info ("Parameter ={}", Joinpoint.getargs ()); } /*** Log logs at the end of an HTTP request*/@After ("Log ()") Public voidDoafter () {servletrequestattributes attributes=(servletrequestattributes) requestcontextholder.getrequestattributes (); HttpServletRequest Request=attributes.getrequest (); Logger.info ("url = {} End of execution", Request.getrequesturl ()); } /*** Get back content *@paramObject*/@AfterReturning (Returning= "Object", Pointcut = "log ()") Public voidDoafterreturn (Object object) {Logger.info ("Response={}", object.tostring ()); }}
Step three, create the test method
Package // Define yourself import org.springframework.web.bind.annotation.*; @RestControllerpublicclass Studentcontroller { = "/aoptest") public String aoptest () { return "AOP Test success!" ; }}
Fourth step, start the program, access the Http://127.0.0.1:8080/aoptest to view the results.
The simple implementation of Spring boot learning--AOP programming