Spring

스프링 Part2 AOP 강좌 06강 _ After Returning / Throwing 어드바이스 구현하기

지각생 2022. 1. 2. 04:25
728x90

                      before 실행

주업무 실행(정상 작동)        주업무 실행(오류)

afterReturning실행              afterThrowing실행

 

setting.xml

<bean id="target" class="spring.aop.entity.NewlecExam" p:kor="99" p:eng="1" p:math="1" p:com="1"/>

kor값이 100을 넘지 않으므로 오류 발생하지 않고 afterReturning정상 출력

setting.xml

<bean id="target" class="spring.aop.entity.NewlecExam" p:kor="101" p:eng="1" p:math="1" p:com="1"/>

kor값이 101로 100을 넘었으므로 

NewlecExam.java

total()메서드에 의해 오류 발생하여  afterThrowing실행

 


 

package spring.aop;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import spring.aop.entity.Exam;
import spring.aop.entity.NewlecExam;
import spring.di.NewlecDiConfig;

public class Program {
public static void main(String[] args) {
ApplicationContext context
//=new AnnotationConfigApplicationContext(NewlecDiConfig.class);
= new ClassPathXmlApplicationContext("spring/aop/setting.xml");
Exam exam = (Exam)context.getBean("exam");
System.out.printf("total is %d\n",exam.total());
System.out.printf("avg is %f\n",exam.avg());
}
}




<?xml version="1.0" encoding="UTF-8"?>

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.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.NewlecExam" p:kor="99" 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="exam" 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>




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+", method:"+method.getName());
}
}





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());
}
}





package spring.aop.entity;

public class NewlecExam implements Exam {

private int kor;

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

public NewlecExam() {
// TODO Auto-generated constructor stub
}

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

public int getKor() {
return kor;
}

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

public int getEng() {
return eng;
}

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

public int getMath() {
return math;
}

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

public int getCom() {
return com;
}

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

@Override
public int total() {
int result = kor+eng+math+com;
if(kor>100)
throw new IllegalArgumentException("유효하지 않은 국어점수");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}

@Override
public float avg() {
float result = total()/4.0f;
return result;
}

@Override
public String toString() {
return "NewlecExam [kor=" + kor + ", eng=" + eng + ", math=" + math + ", com=" + com + "]";
}

}


https://www.youtube.com/watch?v=rClfDKEiEA0&list=PLq8wAnVUcTFUHYMzoV2RoFoY2HDTKru3T&index=23 

 

728x90