본문 바로가기
Spring

After Returning • Throwing Advice

by Coarti 2023. 8. 7.

[ 이전 글 ] BeforeAdvice

 

BeforeAdvice

[ 이전 글 ] Around Advice ( ver. Spring ) Around Advice ( ver. Spring ) [ 이전 글 ]Aspect Oriented Programming ( 2 ) Aspect Oriented Programming ( 2 ) [ 이전 글 ] Aspect Oriented Programming ( 1 ) Aspect Oriented Programming ( 1 ) Aspect Oriented

cloakinghost.tistory.com

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 {

		// 주 업무 함수의 리턴값이 있을 때
		System.out.println("returnValue : " + returnValue);

		// 현재 호출되고 있는 함수의 이름
		System.out.println("method : " + method.getName());

		// 현재 함수의 파라미터
		for (Object e : args) {
			System.out.println(e);
		}

		// 객체의 정보
		System.out.println(target);
	}

}
package spring.aop.advice;

import org.springframework.aop.ThrowsAdvice;

public class LogAfterThrowingAdvice implements ThrowsAdvice {
	// 구현해야할 함수가 정해질 수가 없기에 기본구현 함수가 없다
	// 예외에 따라 함수의 인자가 달라지기 때문이다
	// Exception의 종류가 많다

	// Before Advice 이후 주 업무에서 발생한 예외를 처리
	public void afterThrowing(IllegalArgumentException e) throws Throwable {
		System.out.println("예외 발생 : " + e.getMessage());
	}
}
package spring.aop.entity;

public class ScoreExam implements Exam {

	private int kor;
	private int eng;
	private int math;
	private int com;

	public ScoreExam() { }

	public ScoreExam(int kor, int eng, int math, int com) {
		this.kor = kor;
		this.eng = eng;
		this.math = math;
		this.com = com;
	}

	public void setKor(int kor) {this.kor = kor;}

	public void setEng(int eng) {this.eng = eng;}

	public void setMath(int math) {this.math = math;}

	public void setCom(int com) {this.com = com;}

	@Override
	public int total() {
		int result = kor + eng + math + com;

// 예외발생 ( Throws Advice 사용 )
		if (kor > 100)
			throw new IllegalArgumentException("점수를 확인해주세요.");
		
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
		
	}
// 이하 생략
}

XML

p:kor="101"로 바꾸면 ThrowingAdvice 실행됨

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
		
	
	<bean id="target" class="spring.aop.entity.ScoreExam" p:kor="1"  p:eng="1" p:math="1" p:com="1"/>
	<bean id="logAroundAdvice" class="spring.aop.advice.LogAroundAdvice"/>
	<bean id="logBeforeAdvice" class="spring.aop.advice.LogBeforeAdvice"/>
	<bean id="logAfterReturningAdvice" class="spring.aop.advice.LogAfterReturningAdvice"/>
	<bean id="logAfterThrowingAdvice" class="spring.aop.advice.LogAfterThrowingAdvice"/>
	<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="target"/>
		<property name="interceptorNames">
			<list>
				<value>logAroundAdvice</value>
				<value>logBeforeAdvice</value>
				<value>logAfterReturningAdvice</value>
				<value>logAfterThrowingAdvice</value>
			</list>
		</property>
	</bean>
	
</beans>
728x90

'Spring' 카테고리의 다른 글

Advisor ( 2 )  (0) 2023.08.08
Advisor ( 1 )  (0) 2023.08.08
BeforeAdvice  (0) 2023.08.07
Around Advice ( ver. Spring )  (0) 2023.08.07
Around Advice ( ver. Java Project )  (0) 2023.08.07