The simple implementation of Spring boot learning--AOP programming

Source: Internet
Author: User
Tags aop object object

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

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.