본문 바로가기
Spring

Around Advice ( ver. Java Project )

by Coarti 2023. 8. 7.

[ 이전 글 ] Aspect Oriented Programming ( 1 )

 

Aspect Oriented Programming ( 1 )

Aspect Oriented Programming 관점 지향 프로그래밍 ( 방법론 ) OOP 사용자가 요구한 업무내용(요청)을 분석하고 이를 기반의 로직을 만듬 개발자 관리자가 프로그램구현 혹은 테스트를 위한코드 개발자

cloakinghost.tistory.com

 

 

Java Proejct Version

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() {
//		long start = System.currentTimeMillis();
		
		int result = kor + eng + math + com;
		
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		long end = System.currentTimeMillis();
		
//		String message = (end - start) + "ms 시간이 걸렸습니다.";
//		System.out.println(message);
		return result;
		
	}

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

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

 

package spring.aop;

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

import spring.aop.entity.Exam;
import spring.aop.entity.ScoreExam;

public class Program {

	public static void main(String[] args) {
		// 업무로직을 가진 클래스 + 보조업무

		Exam exam = new ScoreExam(1, 1, 1, 1);

		// 사실상 복사본 ( 가짜를 만들기 위해 재료를 전달 )
		// Proxy.newProxyInstance(ClassLoader loader, Class<?> interfaces InvocationHandler h)
		Exam proxy = (Exam) Proxy.newProxyInstance(ScoreExam.class.getClassLoader(), 
				new Class[] { Exam.class }, new InvocationHandler() {

					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						long start = System.currentTimeMillis();

						// 실제 업무를 담당하는 부분을 호출하는 부분
						// method.invoke(obj, args) : 업무객체, 호출한 메소드의 파라미터
						Object result = method.invoke(exam, args);
						
						long end = System.currentTimeMillis();
						String message = (end - start) + "ms 시간이 걸렸습니다.";
						
						System.out.println(message);
						return result;
					}
				});

		System.out.printf("total is %d\n", exam.total());
		System.out.printf("avg is %.2f\n", exam.avg());
//		System.out.printf("total is %d\n", proxy.total());
//		System.out.printf("avg is %d\n", proxy.avg());

	}

}

원본코드는 수정하지 않고 Proxy로 따로 빼내어 테스트환경을 만들었다

728x90

'Spring' 카테고리의 다른 글

BeforeAdvice  (0) 2023.08.07
Around Advice ( ver. Spring )  (0) 2023.08.07
Aspect Oriented Programming  (0) 2023.08.07
Java Configuration  (0) 2023.08.07
@Component 분석 ( 3 )  (0) 2023.08.07