AOP(2) - Aop Proxy
Proxy๋?

Proxy๋ ์ฌ์ ์ ์๋ฏธ๋ก "๋๋ฆฌ์ธ"์ด๋ผ๋ ๋ป์ด๋ค. java์์์ ํ๋ก์๋ ๋๋ฆฌ๋ฅผ ์ํํ๋ ํด๋์ค๋ผ ์๊ฐํ ์ ์๋ค.
์ฆ, Proxy๋ Client๊ฐ ์ฌ์ฉํ๋ ค๊ณ ํ๋ ์ค์ ๋์์ธ ๊ฒ์ฒ๋ผ ์์ฅ์ ํด์ ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๋ฐ์์ค๋ค. Proxy๋ฅผ ํตํด ์ต์ข ์ ์ผ๋ก ์์ฒญ์ ์์๋ฐ์ ์ฒ๋ฆฌํ๋ ์ค์ ์ค๋ธ์ ํธ๋ฅผ ํ๊ฒ(target) or ์ค์ฒด(real subject)๋ผ ๋ถ๋ฅธ๋ค.
์ค์ ํ๊ฒ์ด ๋ด๋นํ๋ ์ญํ ์์ฒญ์ ๋์ ๋ฐ์์ ์์ฒญ ์ด์ , ์ดํ์ ๋ํ ์ถ๊ฐ์ ์ธ ๋ก์ง์ ํ ์ ์๋ ๊ฐ์ฒด์ด๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์ค์ ํ๊ฒ์ด ๋ด๋นํ๋ ์ญํ ์ ๋ํด์ ๊ด์ฌํ์ง ์์ผ๋ฉด์ ์ถ๊ฐ์ ์ธ ์ญํ ์ ํ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
Proxy์ ํน์ง
target๊ณผ ๊ฐ์ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํ
proxy๊ฐ target์ ์ ์ดํ ์ ์๋ ์์น์ ์๋ค.
AOP Proxy

์คํ๋ง์์ ํจ์ ํธ์ถ์๋ ์ฃผ์ ์ ๋ฌด๊ฐ ์๋ ๋ณด์กฐ ์ ๋ฌด(๊ณตํต๊ธฐ๋ฅ)์ ํ๋ก์์ ๋งก๊ธฐ๊ณ , ํ๋ก์๋ ๋ด๋ถ์ ์ผ๋ก ์ด๋ฌํ ๋ณด์กฐ์ ๋ฌด(๊ณตํต ๊ธฐ๋ฅ)์ ์ฒ๋ฆฌํ๋ค.
ํ๋ก์์ ํธ์ถ ๋ฐ ์ฒ๋ฆฌ์์
Proxy ํธ์ถ
๋ณด์กฐ ์ ๋ฌด ์ฒ๋ฆฌ
Proxy ์ฒ๋ฆฌ ํจ์๊ฐ ์ค์ ๊ตฌํ ํจ์ ํธ์ถ ๋ฐ ์ฃผ ์ ๋ฌด ์ฒ๋ฆฌ
Proxyํจ์๊ฐ ๋๋จธ์ง ๋ณด์กฐ ์ ๋ฌด ์ฒ๋ฆฌ
์ฒ๋ฆฌ ์๋ฃ ํ, ํธ์ถํจ์๋ก ๋ฐํ
์ฝ๋๋ฅผ ๋ณด๋ฉด์ ์ดํด๋ณด์.
์๋ฅผ๋ค์ด, ๊ณ์ฐ๊ธฐ๋ฅผ ๊ตฌํํ ๋, ๊ฐ ํจ์๊ฐ ์ฒ๋ฆฌ๋๋ ์๊ฐ์ ์๊ณ ์ถ๋ค๋ฉด?
public interface Calculator {
public int add(int x, int y);
public int subtract(int x, int y);
public int multiply(int x, int y);
public int divide(int x, int y);
}
public class myCalculator implements Calculator {
@Override
public int add(int x, int y) {
// ๋ณด์กฐ ์
๋ฌด (์๊ฐ ์ธก์ ์์ & ๋ก๊ทธ ์ถ๋ ฅ)
Log log = LogFactory.getLog(this.getClass());
StopWatch sw = new StopWatch();
sw.start();
log.info(โTimer Beginโ);
// ์ฃผ ์
๋ฌด (๋ง์
์ฐ์ฐ)
int sum = x + y;
// ๋ณด์กฐ ์
๋ฌด (์๊ฐ ์ธก์ ๋ & ์ธก์ ์๊ฐ ๋ก๊ทธ ์ถ๋ ฅ)
sw.stop();
log.info(โTimer Stop โ Elapsed Time : โ+ sw.getTotalTimeMillis());
return sum;
}
}
์ฌ๊ธฐ์ ๋ณด์กฐ ์ ๋ฌด(์๊ฐ ์ธก์ )์ด ๋ค๋ฅธ method์์๋ ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉด Proxy๋ฅผ ์ด์ฉํ์ฌ ๋ถ๋ฆฌํ ์ ์๋ค.
์ฆ, ์ฃผ์ ๋ฌด(์ฐ์ฐ)๊ณผ ๋ณด์กฐ์ ๋ฌด(์๊ฐ ์ธก์ )๋ฅผ ๋ถ๋ฆฌ(Cross Cutting) ํ๊ณ ๋ณด์กฐ ์ ๋ฌด๋ฅผ Proxy๊ฐ ํ๋ฉด๋๋ค.
// ๋ณด์กฐ ์
๋ฌด๋ฅผ ์ฒ๋ฆฌํ ํ๋ก์ ํด๋์ค ์ ์
public class LogPrintHandler implements InvocationHandler {
private Object target; // ๊ฐ์ฒด์ ๋ํ ์ ๋ณด
public LogPrintHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Log log = LogFactory.getLog(this.getClass());
StopWatch sw = new StopWatch();
sw.start();
log.info(โTimer Beginโ);
int result = (int) method.invoke(target, args); // (3) ์ฃผ์
๋ฌด๋ฅผ invoke ํจ์๋ฅผ ํตํด ํธ์ถ
sw.stop();
log.info(โTimer Stop โ Elapsed Time : โ+ sw.getTotalTimeMillis());
return result;
}
}
InvocationHandler ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๊ฐ์ฒด๋ invoke ๋ฉ์๋๋ฅผ ๊ตฌํํด์ผํ๋ค. ํด๋น ๊ฐ์ฒด์ ์ํ์ฌ ์์ฒญ ๋ฐ์ ๋ฉ์๋๋ฅผ reflection api๋ฅผ ์ฌ์ฉํ์ฌ ์ค์ ํ๊ฒ์ด ๋๋ ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ํธ์ถํด์ค๋ค.
public static void main(String[] args) {
Calculator cal = new myCalculator();
//(1) ์ค์ ๊ฐ์ฒด๋ฅผ ํธ๋ค๋ฌ๋ฅผ ํตํด์ ์ ๋ฌ
Calculator proxy_cal = (Calculator) Proxy.newProxyInstance(
cal.getClass().getClassLoader(),
cal.getClass().getInterfaces(),
new LogPrintHandler(cal));
System.out.println(proxy_cal.add(3, 4)); // (2) ์ฃผ ์
๋ฌด ์ฒ๋ฆฌ ํด๋์ค์ add ๋ฉ์๋๋ฅผ ํธ์ถ
}
Calculator proxy_cal = (Calculator) Proxy.newProxyInstance(
// ๋์ ์ผ๋ก ์์ฑ๋๋ DynamicProxy ํด๋์ค์ ๋ก๋ฉ์ ์ฌ์ฉํ ํด๋์ค ๋ก๋
cal.getClass().getClassLoader(),
// ๊ตฌํํ ์ธ์คํด์ค
cal.getClass().getInterfaces(),
// ๋ถ๊ฐ๊ธฐ๋ฅ๊ณผ ์์ ์ฝ๋๋ฅผ ๋ด์ ํธ๋ค๋ฌ
new LogPrintHandler(cal));
mainํจ์์์ ์ค์ ๊ฐ์ฒด๋ฅผ ํธ๋ค๋ฌ๋ฅผ ํตํด์ ์ ๋ฌํด์ค๋ค.
์ฃผ ์ ๋ฌด ํด๋์ค์ ๋ฉ์๋๋ฅผ ํธ์ถํ๊ฒ ๋๋ฉด ํ๋ก์ ํด๋์ค(LogPrintHandler)์ invoke ๋ฉ์๋๊ฐ ํธ์ถ๋์ด ์์ ์ ๋ณด์กฐ ์ ๋ฌด๋ฅผ ์ฒ๋ฆฌํ๊ณ , ์ฃผ ์ ๋ฌด์ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ค.
invoke()๋ ๋ฉ์๋๋ฅผ ์คํ์ํฌ ๋์ ๊ฐ์ฒด์ ํ๋ผ๋ฏธํฐ ๋ชฉ๋ก์ ๋ฐ์ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ค์ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ Object ํ์ ์ผ๋ก ๋๋ ค์ค๋ค.
๋ค์๊ณผ ๊ฐ์ด AOP๋ฅผ ๊ตฌํํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ Proxy์๋ ๋ค์๊ณผ ๊ฐ์ ๋จ์ ์ด ์๋ค.
๋งค๋ฒ ์๋ก์ด ํด๋์ค ์ ์๊ฐ ํ์ํ๋ค.
์ค์ ํ๋ก์ ํด๋์ค๋ ์ค์ ๊ตฌํ ํด๋์ค์ ๋์ผํ ํํ๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฏ๋ก, ๊ตฌํ ํด๋์ค์ Interface๋ฅผ ๋ชจ๋ ๊ตฌํํด์ผํ๋ค.
ํ๊ฒ์ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ณ ์์ํ๋ ์ฝ๋ ์์ฑ
๋ถ๊ฐ ๊ธฐ๋ฅ์ด ํ์์๋ ๋ฉ์๋๋ ๊ตฌํํ์ฌ ํ๊ฒ์ผ๋ก ์์ํ๋ ์ฝ๋๋ฅผ ์ผ์ผ์ด ๋ง๋ค์ด์ค์ผํ๋ค.
์ธํฐํ์ด์ค์ ๋ฉ์๋๊ฐ ๋ง์์ง๊ณ ๋ค์ํด์ง๋ฉด ๋ถ๋ด์ค๋ฌ์ด ์์ ์ด ๋ ์ ์๋ค.
ํ๊ฒ ์ธํฐํ์ด์ค์ ๋ฉ์๋๊ฐ ์ถ๊ฐ๋๊ฑฐ๋ ๋ณ๊ฒฝ๋ ๋๋ง๋ค ํจ๊ป ์์ ํด์ค์ผํ๋ค.
๋ถ๊ฐ๊ธฐ๋ฅ ์ฝ๋์ ์ค๋ณต ๊ฐ๋ฅ์ฑ
ํ๋ก์๋ฅผ ํ์ฉํ๋ ๋ถ๊ฐ๊ธฐ๋ฅ, ์ ๊ทผ์ ์ด ๊ธฐ๋ฅ ๋ฑ์ ์ผ๋ฐ์ ์ผ๋ก ์์ฃผ ํ์ฉ๋๋ ๊ฒ์ด ๋ง๋ค. ์ฆ, ๋ค์ํ ํ๊ฒ ํด๋์ค์ ๋ฉ์๋์ ์ค๋ณต๋์ด ๋ํ๋ ๊ฐ๋ฅ์ฑ์ด ๋ง๋ค.(ex) Transaction
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์๋ Dynamic Proxy๋ฅผ ๊ตฌํํ๋ฉด๋๋ค.
์ฐธ์กฐ ํ์ด์ง
Last updated
Was this helpful?