AOP(Aspect Oriented Programming)
우리가 메소드의 성능을 검사하거나 트랜잭션 처리(무수히 많은 try-catch), 예외반환 등 핵심관심사항 & 공통관심사항을 쉽게 보기 위해 나온 것
1. "root-context.xml" 설정 파일에 추가
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
2. Aspect Class를 <bean>으로 등록
<context:component-scan base-package="com.ssafy.board.aop" />
3. AOP 전용 Class를 만들고 Annotation 설정
@Aspect : Aspect Class 선언
@Before("pointcut")
@AfterReturning(pointcut="",returning="")
@AfterThrowing(pointcut="", throwing="")
@After("pointcut")
@Around("pointcut")
더보기
package com.ssafy.board.aop;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
@Component
@Aspect
public class LoggingAspect {
private Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
@Before(value = "execution(* com.ssafy.board.model..Board*.*(..))")
public void loggin(JoinPoint joinPoint) {
logger.debug("before call method : {} ", joinPoint.getSignature());
logger.debug("메서드 선언부 : {} 전달 파라미터 : {}", joinPoint.getSignature(), Arrays.toString(joinPoint.getArgs()));
}
@Around(value = "execution(* com.ssafy.board.model..Board*.*(..))")
public Object executionTime(ProceedingJoinPoint joinPoint) throws Throwable {
logger.debug("around call method : {} ", joinPoint.getSignature());
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Object proceed = joinPoint.proceed();
stopWatch.stop();
logger.debug("summary : {}", stopWatch.shortSummary());
logger.debug("totalTime : {}", stopWatch.getTotalTimeMillis());
logger.debug("pretty : {}", stopWatch.prettyPrint());
return proceed;
}
@AfterReturning(value = "execution(* com.ssafy.board.model..Board*.list*(..))", returning = "obj")
public void afterReturningMethod(JoinPoint joinPoint, Object obj) {
logger.debug("afterReturning call method : {} ", joinPoint.getSignature());
logger.debug("return value : {}", obj);
}
@AfterThrowing(value = "execution(* com.ssafy.board.model..Board*.list*(..))", throwing = "exception")
public void afterThrowingMethod(JoinPoint joinPoint, Exception exception) {
logger.debug("afterThrowing call method : {}", joinPoint.getSignature());
logger.debug("exception : {}", exception);
}
@After(value = "execution(* com.ssafy.board.model..Board*.list*(..))")
public void afterMethod(JoinPoint joinPoint) {
logger.debug("after call method : {}", joinPoint.getSignature());
}
}
'Backend > Spring' 카테고리의 다른 글
[Spring] Spring MVC 패턴 적용 (0) | 2022.10.21 |
---|---|
[Spring] Spring UTF8 한글 설정 (0) | 2022.10.21 |
[Spring] Interceptor에 대해 (0) | 2022.10.21 |
[Spring] FileUpload, FileDownload에 대해 (0) | 2022.10.21 |
[Spring] Spring에 대해 (0) | 2022.10.18 |