spring boot 프로젝트 설정 AOP 로그 절단면

때때로 spring boot 프로젝트 는 controller 류 의 요청 기록 을 감청 하려 면 op 방식 으로 처리 할 수 있 습 니 다.
maven 의존 도입
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-aopartifactId>
        dependency>

AOP 절단면 설정 클래스
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 *     
 */
@Aspect
@Component
public class LogAspect {
    private final static Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(public * cn.test.controller.*.*(..))")
    public void webLog() {
    }

    @Before("webLog()")
    public void deBefore(JoinPoint joinPoint) throws Throwable {
        //      ,      
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        //        
        logger.info("USERNAME : " + username);
        logger.info("IP : " + request.getRemoteAddr());
        logger.info("URL : " + request.getRequestURL().toString() + "   HTTP_METHOD : " + request.getMethod());
        logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        logger.info("INPUT ARGS : " + Arrays.toString(joinPoint.getArgs()));
    }

    /*@AfterReturning(returning = "ret", pointcut = "webLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        //      ,    
        logger.info("       : " + ret);
    }

    //      
    @AfterThrowing("webLog()")
    public void throwss(JoinPoint jp) {
        logger.info("       .....");
    }

    //      ,final  ,                 
    @After("webLog()")
    public void after(JoinPoint jp) {
        logger.info("      .....");
    }

    //    ,    ,   MethodInterceptor
    @Around("webLog()")
    public Object arround(ProceedingJoinPoint pjp) {
        logger.info("    start.....");
        try {
            Object o = pjp.proceed();
            logger.info("    proceed,    :" + o);
            return o;
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
    }*/
}

좋은 웹페이지 즐겨찾기