AOP 코드 구현하기
package Spring.aop.entity;
public class NewlecExam implements Exam{
private int kor;
private int eng;
private int math;
private int com;
public NewlecExam() {
}
public NewlecExam(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 getKor(){
return kor;
}
public void setEng(int eng){
this.eng = eng;
}
public void getEng(){
return eng;
}
public void setMath(int math){
this.math = math;
}
public void getMath(){
return math;
}
public void setCom(int com){
this.com = com;
}
public void getCom(){
return com;
}
@Override
public int total(){
int result = kor+eng+math+com;
return result;
}
@Override
public avg(){
float result = total() / 4.0f;
return result;
}
@Override
public String toString(){
return "NewlecExam [kor=" + kor + ", eng="
+ eng + ", math=" + math + ",com = com" + com];
}
}
package spring.aop;
import Spring.aop.entity.Exam;
import spring.aop.entity.NewlecExam;
public class Program{
public static void main (String[] args)
{
Exam exam = new NewlecExam(1,1,1,1);
/*
Exam proxy = Proxy.newProxyInstance(
loader
, interfaces
, h) 양식으로 기입한다.
*/
Exam proxy = (Exam)Proxy.newProxyInstance(
NewlecExam.class.getClassLoader()
, new Class[]{Exam.class}
, new InvocationHandler(){
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
long start = System.currentTimeMillis();
/*-----------------------*/
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", proxy.total());
System.out.printf("total is %d \n", exam.total());
}
}
실행결과
201ms 시간이 걸렸습니다.
total is 4
total is 4