프로그래밍 공부/Spring

[Spring] After Returning / Throwing Advice

valid_ming 2021. 5. 5. 14:54

 

뉴렉처 강사님의 동영상 강의를 정리한 글입니다.

 

 

After Returning

 

package spring.aop.advice;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class LogAfterReturningAdvice implements AfterReturningAdvice{

	@Override
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("returnvalue: "+returnValue+", method: "+method.getName());
	}

}

 

실행결과

 

 

Throwing Advice

 

국어 점수가 100점을 넘을 때 예외 발생시킴

package spring.aop.advice;

import org.springframework.aop.ThrowsAdvice;

//오류에 따라 함수의 인자가 달라지기 때문에 구현해야할 함수가 정해지지 않음 
public class LogAfterThrowingAdvice implements ThrowsAdvice{
	public void afterThrowing(IllegalArgumentException e) throws Throwable{
		System.out.println("예외가 발생하였습니다. : "+e.getMessage());
	}
}

 

실행결과