내용

글번호 586
작성자 heojk
작성일 2017-02-17 21:35:01
제목 [실습][AOP] 사원정보를 상세조회한 후 조회한 사원의 아이디를 로그로 출력하세요.
내용 1. pom.xml에 aspectj 의존성 추가
		
			org.aspectj
			aspectjweaver
			1.7.4
		
2. application-config.xml에 추가 * Namespace 탭에서 aop 체크해야 합니다.
	
3. Logger 클래스 작성
package kr.co.javaspecialist.hr.aop;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import kr.co.javaspecialist.hr.model.EmpVO;

@Aspect
@Component
public class Logger {

	@AfterReturning(value="execution(* kr.co..*.getEmpDetails(..))", 
			        returning="emp")
	public void info(EmpVO emp) {
		System.out.println(emp.toString());
	}
}
4. Aspect 클래스 컴포넌트 스캔 포함
	
5. 모든 biz() 실행 시간을 출력하세요(Logger.java에 추가)
	@Around(value="execution(* kr.co..model.*.*(..))")
	public Object timeCheck(ProceedingJoinPoint joinPoint) {
		String name = joinPoint.getSignature().getName(); //메서드 이름
		Object result = null;
		try {
			long start = System.nanoTime();
			result = joinPoint.proceed(); //biz() 로직 실행
			long end = System.nanoTime();
			System.out.println(name + " : " + (end-start));
		}catch(Throwable e) {
			System.out.println(e.getMessage());
		}
		return result;
	}