import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component // 쓰거나 아니면 bean으로 등록
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(); // 다음 method로 진행
} finally {
long finish = System.currentTimeMillis();
long timeMs = finish-start;
System.out.println("End"+joinPoint.toString()+" "+timeMs+"ms");
}
}
}
aop가 있으면 가짜memberService(프록시)를 만들어서 실행하고, joinPoint.proceed()가 실행되면 실제 MemberSerivce가 실행된다.
프록시가 실행되는 걸 확인 할 수 있다.
@Autowired
public MemberController(MemberService memberService) {
this.memberService = memberService;
System.out.println("memberService = "+ memberService.getClass());
}
결과
STARTexecution(MemberService hello.hellospring.SpringConfig.memberService())
Endexecution(MemberService hello.hellospring.SpringConfig.memberService()) 13ms
memberService = class hello.hellospring.service.MemberService$$EnhancerBySpringCGLIB$$71033579
memberService = class hello.hellospring.service.MemberService71033579 -> memberSerivce를 복제해서 코드 조작.
Ref:
*김영한님의 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술 강의