ctrl+alt+v
🔸 공통 관심 사항(cross-cutting concern) vs 핵심 관심 사항(core concern)
🔸 모든 메소드의 호출 시간을 측정하고 싶을 경우
🔸 문제
🔸 AOP(Aspect Oriented Programming)
🔸 공통 관심 사항과 핵심 관심 사항 분리
🔸 실제가 아닌 Proxy를 만들어 동작
main/.../hello.hellospring/aop/TimeTraceAop.java
main/.../hello.hellospring 아래에 aop 이름의 package 생성 후 TimeTraceAop.java 파일 생성
🔸 @Aspect 작성 => AOP로 사용하기 위해
🔸 @Around("execution( hello.hellospring..(..))")
어디에 aop적용할지 => hello.hellospring 아래의 모든 파일에 적용할 것
🔸 execute 함수 생성
public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {}
🔹 System.currentTimeMillis(); 작성 후 단축키 ctrl+alt+v로 변수 생성 후 변수명 start로 변경
🔹 System.out.println("START: "+joinPoint.toString());
🔹 try {} finally {}
🔹 (try 내부)joinPoint.proceed(); 작성 후 단축키 ctrl+alt+v로 변수 생성 후 변수명 result로 변경 + return result; . 이후 단축키 ctrl+alt+n로 inline으로 변경
=> 다음 메소드로 진행
🔹 (finally 내부) System.currentTimeMillis(); 작성 후 단축키 ctrl+alt+v로 변수 생성 후 변수명 finish로 변경
🔹 (finally 내부) long timeMs=finish-start;
🔹 (finally 내부) System.out.println("END: "+joinPoint.toString() + " " + timeMs + "ms");
main/.../hello.hellospring/SpringConfig
🔸 스프링 빈 등록
🔸 @Bean
🔸 public TimeTraceAop timeTraceAop() {
return new TimeTraceAop();
}
💡 SpringConfig로 등록하지 않고 TimeTraceAop.java 위에 @Component 작성해 빈 등록할 수도 있음 => 실습에서는 이렇게. But 원래 AOP는 config에 등록하는 것이 보기 좋음