[original by HAVENT] AOP custom annotation using Spring Boot

Posted by ted_chou12 on Fri, 03 Jan 2020 06:47:33 +0100

For Spring Boot reference AOP, please refer to the previous article: [original by HAVENT] use AOP of Spring Boot to record execution time log globally

Next we will use the custom annotation:

1. Define an annotation interface object first

package com.havent.demo.aop.target;

import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface HHWebLog {
    String value() default "Auto log start...";
    String level() default "info";
    String path() default "";
}

 

2. Create a new tangent point in HHWebLogAspect.java and associate the annotation interface

    @Pointcut(value = "@annotation(com.havent.demo.aop.target.HHWebLog)")
    public void webLog(){}

 

3. Create pre notification injection method

    @Before(pointcut = "webLog()")
    public void deBefore(JoinPoint joinPoint) throws Throwable {
        // Receive request, record request content
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // Record the request
        System.out.println("HH URL : " + request.getRequestURL().toString());
        System.out.println("HH HTTP_METHOD : " + request.getMethod());
        System.out.println("HH IP : " + request.getRemoteAddr());
        System.out.println("HH CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        System.out.println("HH ARGS : " + Arrays.toString(joinPoint.getArgs()));

        //logger.trace("");
    }

To get the parameters in the annotation, the code is as follows

    @Before("@annotation(hhWebLog)")
    public void deBefore(JoinPoint joinPoint, HHWebLog hhWebLog) throws Throwable {
        // Receive request, record request content
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // Record the request
        System.out.println("HH URL : " + request.getRequestURL().toString());
        System.out.println("HH HTTP_METHOD : " + request.getMethod());
        System.out.println("HH IP : " + request.getRemoteAddr());
        System.out.println("HH CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        System.out.println("HH ARGS : " + Arrays.toString(joinPoint.getArgs()));

        System.out.println("HH targe:" + hhWebLog.path());
        //logger.trace("");
    }

 

4. Create a return notification injection method

    @AfterReturning(pointcut = "webLog()", returning = "result")
    public void doAfterReturning(Object result) throws Throwable {
        // After processing the request, return the content
        System.out.println("HH Return value of method : " + result);

        //logger.trace("");
    }

 

5. Create post exception injection method

    //Post exception notification
    @AfterThrowing("webLog()")
    public void throwss(JoinPoint jp){
        System.out.println("HH Execute on method exception.....");

        //logger.trace("");
    }

 

6. Create post notification injection method

    //After the final notification, the final enhancement, whether it is to throw an exception or to exit normally, will execute
    @After("webLog()")
    public void after(JoinPoint jp){
        System.out.println("HH Method final execution.....");
    }

 

The complete HHWebLogAspect.java is as follows

package com.havent.demo.aop.aspect;

import com.havent.demo.aop.target.HHWebLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;


@Aspect
@Component
public class HHWebLogAspect {

    @Pointcut(value = "@annotation(com.havent.demo.aop.target.HHWebLog)")
    public void webLog(){}

    @Before("@annotation(hhWebLog)")
    public void deBefore(JoinPoint joinPoint, HHWebLog hhWebLog) throws Throwable {
        // Receive request, record request content
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // Record the request
        System.out.println("HH URL : " + request.getRequestURL().toString());
        System.out.println("HH HTTP_METHOD : " + request.getMethod());
        System.out.println("HH IP : " + request.getRemoteAddr());
        System.out.println("HH CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        System.out.println("HH ARGS : " + Arrays.toString(joinPoint.getArgs()));

        System.out.println("HH targe:" + hhWebLog.path());
        //logger.trace("");
    }

    @AfterReturning(pointcut = "webLog()", returning = "result")
    public void doAfterReturning(Object result) throws Throwable {
        // After processing the request, return the content
        System.out.println("HH Return value of method : " + result);

        //logger.trace("");
    }

    //Post exception notification
    @AfterThrowing("webLog()")
    public void throwss(JoinPoint jp){
        System.out.println("HH Execute on method exception.....");

        //logger.trace("");
    }

    //After the final notification, the final enhancement, whether it is to throw an exception or to exit normally, will execute
    @After("webLog()")
    public void after(JoinPoint jp){
        System.out.println("HH Method final execution.....");
    }


}

 

7. External registration annotation code

package com.havent.demo.controller;

import com.havent.demo.aop.target.HHWebLog;
import com.havent.demo.logger.service.KafkaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @HHWebLog(level = "error", path = "/test")
    @RequestMapping("/test")
    public String test() {
        return "test";
    }

}

 

The effect of visiting http://localhost:8081/test is as follows:

Topics: Java Spring