์ฌ์ฉ์์ ์๊ตฌ์ ๋ฐ๋ผ kor, eng, math, com ๋ค ๊ณผ๋ชฉ์ ์ด์ ์ ๊ณ์ฐํด ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ด ์๋ค๊ณ ํด๋ณด์. ํ๋ก๊ทธ๋จ์ด ๋๋ฌด ๋๋ฆฌ๋ค๋ ํผ๋๋ฐฑ์ด ์์ ๋ ๊ฐ๋ฐ์๋ค์ ์คํ์ ์๊ฐ์ด ์ผ๋ง๋ ๊ฑธ๋ฆฌ๋์ง ์ ํ์๊ฐ ์์ ๊ฒ์ด๊ณ , ๊ทธ๋ฌ๊ธฐ ์ํด ๊ณ์ฐํ๋ ์์ค๋ฅผ ๋ผ์๋ฃ๊ฒ ๋์๋ค. ์ด๋ ์ฌ์ฉ์์ ์๊ตฌ์ฌํญ๊ณผ๋ ๋ณ๊ฐ๋ก ๊ฐ๋ฐ์๋ค์ด ํ์์ ์ํด ์ฝ์ ํ ๋ถ๋ถ์ด๋ค. (=>๋ณด์กฐ ์ ๋ฌด)
** ์์ค ๊ตฌ์ฑ
Exam์ NewlecExam์ ์ธํฐํ์ด์ค
Program.java
public class Program {
public static void main(String[] args) {
Exam exam = new NewlecExam(1,1,1,1);
System.out.printf("total is %d\n", exam.total());
}
}
NewlecExam.java
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;
}
@Override
public int total() {
//////////////๋ณด์กฐ ์
๋ฌด//////////////
long start = System.currentTimeMillis();
//////////////๋ณด์กฐ ์
๋ฌด//////////////
////////์ฌ์ฉ์๊ฐ ์ํ ์
๋ฌด(์ฃผ ์
๋ฌด)////////
int result = kor + eng + math + com;
////////์ฌ์ฉ์๊ฐ ์ํ ์
๋ฌด(์ฃผ ์
๋ฌด)////////
//////////////๋ณด์กฐ ์
๋ฌด//////////////
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
String message = (end - start) + "์๊ฐ์ด ๊ฑธ๋ ธ์ต๋๋ค.";
System.out.println(message);
//////////////๋ณด์กฐ ์
๋ฌด//////////////
return result;
}
}
์ดํฉ ๊ฐ์ ๋ฆฌํดํ๋ total() ๋ฉ์๋ ์์์ ์คํ ์๋๋ฅผ ๊ณ์ฐํ๋ ์ฝ๋๋ฅผ ๊ปด๋์๋ค. ์ด๋ ๊ฒ ํ๋ฉด ํด๋น ๊ณ๋ค๋ฆฌ ์ฝ๋, ๋ณด์กฐ ์ ๋ฌด๋ฅผ ์ง์ฐ๊ฑฐ๋ ์ฝ์ ํ ๋ ๋ชจ๋ ๋ฉ์๋ ์์์ ์ฃผ์ ์ฒ๋ฆฌ๋ฅผ ํ๋ค๊ฐ ์ง์ฐ๋ ์ฒ๋ฆฌ๋ฅผ ํด์ผํ๊ธฐ ๋๋ฌธ์ ๋ฒ๊ฑฐ๋กญ๋ค. ์๋ฅผ ๋ค์ด ํ๊ท ์ ๋ฐํํ๋ avg() ํจ์ ์์์๋ ์ด๋ฐ ์ฒ๋ฆฌ๋ฅผ ์ผ์ผ์ด ํด์ผ ํ๋ค. ์ด๋ฌํ ๋ฒ๊ฑฐ๋ก์์ ์ค์ด๊ธฐ ์ํด AOP๋ฅผ ์ฌ์ฉํ๊ณ , ์๋ฐ์์๋ AOP๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด proxy๋ฅผ ์ ๊ณตํ๊ณ ์๋ค.
์์ค ์ฝ๋๋ฅผ ๋ค์์ฒ๋ผ ์์ ํ์ฌ AOP ๊ตฌํ์ ํด๋ณด๋๋ก ํ๊ฒ ๋ค.
Program.java
public class Program {
public static void main(String[] args) {
Exam exam = new NewlecExam(1,1,1,1);
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);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
String message = (end - start) + "์๊ฐ์ด ๊ฑธ๋ ธ์ต๋๋ค.";
System.out.println(message);
return result;
}
}
);
//proxy๋ ๊ฐ์ง exam์ด๋ค.
//ํ์ง๋ง exam ๊ฐ์ฒด์ ๋ฎ์์๊ธฐ์ ์ฌ์ฉ์๊ฐ ๋๋ผ๊ธฐ์๋ exam์ ์ฐ๋ ๊ฒ๊ณผ ๋๊ฐ์ด ๋๊ปด์ง๋ค.
System.out.printf("total is %d\n", proxy.total());
System.out.printf("avg is %d\n", proxy.total());
}
}
NewlecExam.java
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;
}
@Override
public int total() {
////////์ฌ์ฉ์๊ฐ ์ํ ์
๋ฌด(์ฃผ ์
๋ฌด)////////
int result = kor + eng + math + com;
////////์ฌ์ฉ์๊ฐ ์ํ ์
๋ฌด(์ฃผ ์
๋ฌด)////////
return result;
}
@Override
public float avg() {
////////์ฌ์ฉ์๊ฐ ์ํ ์
๋ฌด(์ฃผ ์
๋ฌด)////////
float result = total() / 4.0f;
////////์ฌ์ฉ์๊ฐ ์ํ ์
๋ฌด(์ฃผ ์
๋ฌด)////////
return result;
}

์ฃผ ์ ๋ฌด ์ ๋ค์ ์ง์ ์จ์๋ ๋ณด์กฐ ์ ๋ฌด๋ฅผ ๋นผ๊ณ proxy๋ก ์ฒ๋ฆฌํ์๋ค. ๋ง์ฝ ๋ณด์กฐ ์ ๋ฌด๊ฐ ํ์๊ฐ ์์ด์ง๋ฉด ์์ค ์ฝ๋๋ฅผ proxy.total()์์ exam.total()์ผ๋ก ์์ ํ๋ฉด ๋๋ฏ๋ก ์ด์ ๋ณด๋ค ํจ์ฌ ๊ฐํธํด์ก๋ค. ํ์ง๋ง ์ด๋ ๊ฒ ์๋ฐ๋ก๋ง AOP ๊ตฌํ์ ํ๋ ๊ฒ์๋ ์ฌ์ ํ ๋ฒ๊ฑฐ๋ก์์ด ์๊ธฐ ๋๋ฌธ์ ์คํ๋ง์ ์ด์ฉํ์ฌ ๋ณด๋ค ํธ๋ฆฌํ๊ฒ AOP ๊ตฌํ์ ํ ์ ์๊ฒ ๋์๋ค.
๋ด๋ ์ฒ ์คํ๋ง ํ๋ ์์ํฌ ๊ฐ์๋ฅผ ๋ฃ๊ณ ์ ๋ฆฌํ ๊ฒ์๊ธ์ ๋๋ค.
'๐ปStudy > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring MVC (0) | 2021.02.14 |
---|---|
Spring์ผ๋ก AOP ๊ตฌํ (Around Advice) (0) | 2021.02.13 |
AOP(Aspect Oriented Programming)์ ๊ฐ๋ (0) | 2021.02.13 |
XML Configuration์ Java Configuration์ผ๋ก ๋ณ๊ฒฝํ๊ธฐ (0) | 2021.02.13 |
์ด๋ ธํ ์ด์ ์ ์ด์ฉํ ๊ฐ์ฒด ์์ฑ(@Component) (0) | 2021.02.12 |