* 인프런 김영한님의 '스프링 입문' 강의를 듣고 작성한 내용입니다! (https://inf.run/txXC)
- 만약 모든 메소드의 호출 시간을 측정하고 싶다면?
메소드 시작에 time, 마지막에 time을 찍고 그 둘의 차이를 출력하면 된다
=> 모든 메소드에 코드를 일일히 넣어줘야 한다
- 메소드가 구현하는 것들(db에 넣고 데이터 처리하는 등의 핵심 비즈니스 로직) => 핵심 관심 사항
- 시간 측정과 같은 것들 => 공통 관심 사항
* AOP가 핵심 관심 사항과 공통 관심 사항을 분리해준다
* 앞에서 말했듯이 정형화된 controller, repository, service등은 스프링 빈에 등록시 스캔 방법을 쓰면 되지만, aop 같은건 SpringConfig에서 직접 @Bean으로 등록한다
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); // 어떤 method를 call한지 알 수 있다 | |
try { | |
return joinPoint.proceed(); | |
} finally { | |
long finish = System.currentTimeMillis(); | |
long timeMs = finish - start; | |
System.out.println("END : " + joinPoint.toString() + " " + timeMs + "ms"); | |
} | |
} | |
} |
- @Around annotation에 어디서 쓸건지 적어줘야한다 (현재 코드에서는 hello.hellospring 하위에 다 적용)
- 프록시(가짜 service를 만들어서)를 사용한다고 설명해주셨는데 완벽하게 이해하지는 못했다
'개발 > Spring' 카테고리의 다른 글
Springboot + Mysql + JPA 로 간단한 프로젝트 1 (0) | 2021.07.15 |
---|---|
스프링부트 console에 color 적용하기, 로그 찍기 (0) | 2021.07.14 |
[스프링 입문] 스프링 회원관리 예제 - 7. 스프링 DB 접근 기술 (0) | 2021.06.30 |
[스프링 입문] 스프링 회원관리 예제 - 6. 웹 MVC 개발 (0) | 2021.06.30 |
[스프링 입문] 스프링 회원관리 예제 - 5. 스프링 빈과 의존관계 (0) | 2021.06.30 |