SpringBoot 는 @ Aspect 를 이용 하여 AOP 를 실현 합 니 다.

3451 단어 자바
pom 의존

    commons-collections
    commons-collections
    3.2


    org.aspectj
    aspectjrt
    1.9.2


    org.springframework.boot
    spring-boot-starter-aop
    2.1.4



사용자 정의 주석
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogRecord {
    String logRecord() default "";
}

Aspect 절단면 작성
import com.yunzhitx.mall.admin.controller.core.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@Aspect
public class LogRecordAdvice {
    /**
    *  、       
    */
    @Before("@annotation(logRecord)")
    public void before(JoinPoint point, LogRecord logRecord){
        System.out.println("    ");
    }

    @AfterReturning(returning = "obj",pointcut="@annotation(logRecord)")
    public void after(JoinPoint point,Object obj,LogRecord logRecord){
        // obj        (        ), logRecord         
        System.out.println("    ");
        String record = logRecord.logRecord();
        log.info(record);
    }

    @AfterThrowing("@annotation(logRecord)")
    public void afterThrowing(JoinPoint point,LogRecord logRecord){

    }


    /**
    * 、Pointcut   ,   com.yunzhitx.community.controller.template.topic       
    */
    @Pointcut("within(com.yunzhitx.community.controller.template.topic..*)")
    public void adminValidatePointcut(){
    }

    @Before("adminValidatePointcut()")
    public void adminValidateBefore(){
       System.out.println("Pointcut   ,    ");
    }

    /**
     * Pointcut + annotation        
     */
    @Pointcut("@annotation(com.yunzhitx.community.controller.log.LogRecord)")
    public void logPointCut() {
    }

    /**
     *         
     *
     * @param joinPoint   
     */
    @AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, Object jsonResult) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null) {
            //         
            LogRecord log= method.getAnnotation(LogRecord.class);
            //   ...
        }
    }


}

주해 방식, 강화 방법 에 사용자 정의 주 해 를 달 아야 합 니 다.
@RestController
@RequestMapping("api/v1/item/itemCategory")
public class ItemCategoryRest  {

    @PostMapping("add")
    @LogRecord(logRecord = "      ")
    public InvokeResult add(@RequestBody @Valid ItemCategoryCreateCommand command) {
        ItemCategory itemCategory = ItemCategoryAssembler.toItemCategory(command);
      //  itemCategoryService.addItemCategory(itemCategory);
        return InvokeResult.success(itemCategory.getId());
    }
}

좋은 웹페이지 즐겨찾기