-
[김영한 스프링] 19. AOPSpring/스프링 입문 2023. 4. 1. 23:42
시간 측정 로직을 추가할 곳이 많으면 많을수록 유지보수 불가능
package hello.hellospring.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class TimeTraceAop {
@Around("execution(* hello.hellospring..*(..))")
public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
System.out.println("START: " + joinPoint.toString());
try {
return joinPoint.proceed();
} finally {
long finish = System.currentTimeMillis();
long timeMs = finish - start;
System.out.println("END: " + joinPoint.toString() + " " + timeMs + "ms");
}
}
}hello.hellospring/aop/TimeTraceAop 생성
@Aspect
- AOP 사용하도록 함
@Component
- config파일에 Bean을 등록하지 않아도 사용 가능하도록 함
@Around()
- 괄호안에 해당하는 메소드의 실행 전, 후에 동작
joinPoint.proceed()
- 비지니스 메소드 호출
App 실행 시 시간 체크
회원 등록과 조회 시 시간 체크
기존에는 Controller에서 Service를 호출
프록시 Service라는 가짜 Service를 생성시켜서 Controller가 프록시 Service를 호출 후 실제 Service를 호출
'Spring > 스프링 입문' 카테고리의 다른 글
[김영한 스프링] 18. 스프링 데이터 JPA (0) 2023.04.01 [김영한 스프링] 17. JPA (0) 2023.03.31 [김영한 스프링] 16. 스프링 JdbcTemplate (0) 2023.03.30 [김영한 스프링] 15. 스프링 통합 테스트 (0) 2023.03.30 [김영한 스프링] 14. JDBC (0) 2023.03.29