728x90
AOP 적용
AOP: Aspect Oriented Programming
공통 관심 사항(cross-cutting concern) vs 핵심 관심 사항(core concern) 분리
시간 측정 AOP 등록
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");
}
}
}
@Around("execution(* hello.hellospring..*(..))") 코드의 의미는 hello.hellospring 하위의 파일들에 모두 적용하라는 의미
회원 목록 요청 결과
AOP 적용 전 의존관계
단지 helloController가 memberService를 호출하는 관계였다가, AOP를 적용하면 아래와 같이 된다.
스프링 컨테이너의 동작방식: 스프링 빈을 등록 시 가짜 스프링빈을 등록하고 가짜 스프링빈이 끝나면 joinPoint.proceed()를 실행하면 그때서야 실제 memberService를 호출해준다.
이걸 실제로 확인하기 위해서는 MemberController에 아래 코드를 추가한 후 실행하면 된다.
@Autowired
public MemberController(MemberService memberService) {
..
System.out.println("memberService = " + memberService.getClass()); // 클래스 확인
}
AOP 적용 후 전체 그림
- 이러한 방식을 스프링에서는 프록시 방식의 AOP라고 한다.
728x90
'스프링 부트 > 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술' 카테고리의 다른 글
17. 회원 웹 기능 - 조회 (0) | 2023.03.13 |
---|---|
16. 회원 웹 기능 - 등록 (0) | 2023.03.13 |
24. AOP가 필요한 상황 (0) | 2023.03.12 |
18~23. 스프링 DB 접근 기술 (0) | 2023.03.11 |
15. 회원 웹 기능 - 홈 화면 추가 (0) | 2023.03.11 |