실전 개발자를 위한 Spring Framework Day19 실습 (2회차)
1. 메소드 수행시간 확인 Aspect 만들기
- MeasuringAspect.java 작성
package kr.co.company.hello.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MeasuringAspect {
@Around("execution(* kr.co.company.hello.service.*Service.*(..))") //~Service 클래스의 모든 메소드
public Object measuringMethodRunningTime(ProceedingJoinPoint joinpoint) throws Throwable{
long start = System.currentTimeMillis();
try {
return joinpoint.proceed();//타겟메서드 실행
} finally {
long result = System.currentTimeMillis() - start;
String targetMethodName = joinpoint.getSignature().getName();
System.out.println(targetMethodName + " running time is " + result);
}
}
}
- BbsService.java에 메서드 추가
public void testService(){
System.out.println("target invoked...");
}
- BbsController.java에 메서드 수정
@GetMapping("/")
public String index(){
bbsService.testService();
return "index";
}
- http://localhost:8080/hello-web/bbs/ 접속
testService
target invoked...
testService running time is 19