SpringBoot 는 AOP 를 어떻게 우아 하 게 사용 합 니까?

AOP 의 사용 은 이미 매우 광범 위 하 다.여러 가지 유형 에서 공동으로 사용 하 는 방법 을 가로로 자 르 고 특정한 모듈 로 밀봉 하 는 데 뛰어나다.다음은'인터페이스의 방문 시간 기록'기능 장면 으로 springboot 에서 AOP 를 어떻게 사용 하 는 지 살 펴 보 자.
 
spring-boot 의 op 의존 도 를 추가 합 니 다.


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


@Aspect
@Component
public class HttpRequestAspect {

    private static final Logger log = LoggerFactory.getLogger(HttpRequestAspect.class);

    public static long startTime;
    public static long endTime;

    /*@PointCut         ,         */
    /*     */
    @Pointcut("execution(public * com.simons.cn.springbootdemo.controller.*.*(..))")
    /*    */
    public void print() {

    }

    /*@Before              */
    @Before("print()")
    public void before(JoinPoint joinPoint) {
        log.info("    before……");
        startTime = System.currentTimeMillis();
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String requestURI = request.getRequestURI();
        String remoteAddr = request.getRemoteAddr();   //        ip"   "
        String requestMethod = request.getMethod();
        String declaringTypeName = joinPoint.getSignature().getDeclaringTypeName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        log.info("  url=" + requestURI + ",   ip=" + remoteAddr + ",    =" + requestMethod + ",     =" + declaringTypeName + ",   =" + methodName + ",  =" + args);
    }

    /*@After             */
    @After("print()")
    public void after() {
        endTime = System.currentTimeMillis() - startTime;
        log.info("    after……");
    }

    /*@AfterReturning            */
    @AfterReturning(pointcut = "print()", returning = "object")
    public void getAfterReturn(Object object) {
        log.info("      ={}ms", endTime);
        log.info("afterReturning={}", object.toString());
    }
}

1. @PointCut(×××) , @Before、@After、@AfterReturn , @Before(value="print()") , ;

2.execution(public * com.simons.cn.springbootdemo.controller.*.*(..)) :

* , *.* , ..

:


@GetMapping(value = "/index")
@ResponseBody
public String index() {
    ActivitySystemVariable systemVariable = activitySystemVariableMapper.selectByPrimaryKey(258);
    return systemVariable.toString();
}

,ok


2018-06-28 11:21:10.939  INFO 10696 --- [nio-8888-exec-8] c.s.cn.springbootdemo.HttpRequestAspect  :     before……
2018-06-28 11:21:10.939  INFO 10696 --- [nio-8888-exec-8] c.s.cn.springbootdemo.HttpRequestAspect  :   url=/index,   ip=0:0:0:0:0:0:0:1,    =GET,     =com.simons.cn.springbootdemo.controller.IndexController,   =index,  =[Ljava.lang.Object;@1efabee
2018-06-28 11:21:10.951  INFO 10696 --- [nio-8888-exec-8] c.s.cn.springbootdemo.HttpRequestAspect  :     after……
2018-06-28 11:21:10.951  INFO 10696 --- [nio-8888-exec-8] c.s.cn.springbootdemo.HttpRequestAspect  :       =12ms
2018-06-28 11:21:10.951  INFO 10696 --- [nio-8888-exec-8] c.s.cn.springbootdemo.HttpRequestAspect  : afterReturning=ActivitySystemVariable{id=258, name='simonsfan  ', svKey='simons-key', value='simons-value', memo='simonsfan  ', typeCode='', cataLog=0, createTime=2018-06-28 11:18:19.0}

좋은 웹페이지 즐겨찾기