AOP Example - Logging
개발된 어플리케이션 테스트시 오류가 발생한 경우, 해당하는 메소드 로직 내에 입력값 확인을 위해 DEBUG 레벨의 로그를 추가하거나,
System.out.println() 구문을 추가하게 되는데 이로 인해 핵심 비즈니스 로직과 섞이게 되어 코드 복잡도가 증가한다.
따라서 특정 메소드 호출시 전달하는 입력값 확인을 위한 별도 Aspect을 정의하여 활용하면 관련된 메소드 내에 입력값 확인을 위한
로직들을 제외시킬 수 있게 된다.
다음에서는 AOP의 대표적인 툴 중 @AspectJ(Annotation)를 이용하여 Logging Aspect를 생성하고 테스트해 보도록 할 것이다.
Logging Aspect 적용 대상은
UserService
이며, 특정 메소드(update*) 실행 전에 해당 메소드를 실행하기 위해 입력된 인자들의
값을 로그로 남기는 역할을 수행하게 될 것이다.
Configuration
@AspectJ(Annotation)이 적용된 클래스들을 로딩하여 해당 클래스에 정의된 Pointcut, Advice를
실행하기 위해서는 Spring 속성 정의 XML 파일에 다음과 같이 추가해주어야 한다.
Aspect 정의
다음과 같이 Annotation과 함께 구성된
AutoLog
라는
Aspect 클래스를 생성한다. AutoLog는 integration.anyframe.services 패키지 내에 속한 모든 클래스의 메소드 중 메소드명이
update로 시작하는 메소드 실행 전에 해당 메소드 정보와 입력 인자값을 로그로 남기는 역할을 수행한다.
@Aspect
public class AutoLog {
// pointcut : integration.anyframe.services 패키지 내에 속한 모든 클래스의
// 메소드 중 메소드명이 get으로 시작하는 메소드 실행 시점
@Pointcut("execution(public * integration.anyframe.services..update*(..))")
public void updateMethods() {
}
@Before("updateMethods()")
// JoinPoint 정보를 추출하기 위해 Advice의 입력 인자로 JoinPoint를 정의함.
public void autoLog(JoinPoint thisJoinPoint) {
// 해당 joinpoint가 정의된 클래스를 추출한다.
String className = thisJoinPoint.getSignature().getDeclaringTypeName();
// 해당 joinpoint의 입력 인자들을 추출한다.
Object[] args = thisJoinPoint.getArgs();
StringBuffer argSb = new StringBuffer();
argSb.append("\n ****** AutoLog Start!!!! *****");
argSb.append("\n * " + thisJoinPoint.getSignature().toString());
for (int i = 0; i < args.length; i++) {
argSb.append("\n * " + (i + 1) + " arg's value : "
+ args[i].toString());
}
argSb.append("\n ******************************************************");
Log logger = LogFactory.getLog(className);
logger.debug(argSb.toString());
}
}
autoLog() 메소드에서는 JoinPoint라는 객체를 이용하여, 해당 메소드 정보와 입력 인자값을 Target 클래스의
로를 통해 로그를 남기고 있음을 알 수 있다.
Aspect 실행
UserService를 호출하여, 등록된 User 정보를 수정하는 로직으로 구성된
AutoLogAspectTest
클래스를 실행시키면
AutoLog Aspect가 적용되어, 콘솔창을 통해 다음과 같은 형태의 로그를 볼 수 있다.
2008-12-29 21:09:27,875 DEBUG [integration.anyframe.services.aop.service.UserServiceImpl]
****** AutoLog Start!!!! *****
* void integration.anyframe.services.aop.service.UserServiceImpl.updateUser(UserVO)
* 1 arg's value : [userId] woos41 [userName] updgang [password] null [role] null [ssn] null
[slYn] null [birthDay] null [age] 0 [cellPhone] 9876543210 [addr] kamala road -- [email] null
[emailYn] null [imageFile] null [regDate] 2008-12-29 21:09:27.875
******************************************************
2008-12-29 21:09:27,875 DEBUG [integration.anyframe.services.aop.service.UserDAO]
****** AutoLog Start!!!! *****
* void integration.anyframe.services.aop.service.UserDAO.updateUser(UserVO)
* 1 arg's value : [userId] woos41 [userName] updgang [password] null [role] null [ssn] null
[slYn] null [birthDay] null [age] 0 [cellPhone] 9876543210 [addr] kamala road -- [email] null
[emailYn] null [imageFile] null [regDate] 2008-12-29 21:09:27.875
******************************************************
Resources
다운로드
샘플 테스트 코드를 포함하고 있는 anyframe-aoptest-logging-src.zip 파일을 다운받은 후 위에서 제시한 예제 코드를 실행해 볼 수 있다.
| Name |
Download |
| anyframe-aoptest-src.zip |
Download
|