๋ฐ์ํ
ํ๋ก์ ํจํด์ด๋?
์ด๋ค ๊ฐ์ฒด ์์ ๋๋ฆฌ์ธ(proxy) ์ ํ๋ ๋๊ณ ,
๊ทธ ๊ฐ์ฒด์ ์คํ ์ ํ์ ๋ค๋ฅธ ํ๋์ ๋ง๋ถ์ด๋ ๊ตฌ์กฐ
์ค๋ฌด ๋น์
- ์นด์นด์คํ์ด์ ๊ฒฐ์ ์์ฒญ์ ๋ณด๋ธ๋ค
- ๊ทธ๋ฐ๋ฐ ๊ทธ ์์ฒญ์ ์นด์นด์ค API๋ก ๋ฐ๋ก ๊ฐ์ง ์๊ณ ,
์ค๊ฐ์์ “ํ๋ก์ ์๋ฒ”๊ฐ ๋ก๊ทธ๋ฅผ ๋จ๊ธฐ๊ณ ๊ฒ์ฆํ ๋ค ์นด์นด์ค์ ๋๊ฒจ์ค
์ด ํ๋ก์๊ฐ ๋ฐ๋ก ์ค๊ฐ์์ ๋์ ์ผ ์ฒ๋ฆฌํด์ฃผ๋ ๊ฐ์ฒด
์์ : ๋ก๊ทธ๋ฅผ ์๋์ผ๋ก ๋จ๊ธฐ๋ ํ๋ก์ ํจํด
1. ์ง์ง ์๋น์ค ์ธํฐํ์ด์ค
public interface PayService {
void pay(int amount);
}
2.์ค์ ๊ตฌํ์ฒด
public class KakaoPayService implements PayService {
public void pay(int amount) {
System.out.println("์นด์นด์คํ์ด๋ก " + amount + "์ ๊ฒฐ์ ");
}
}
3.ํ๋ก์ ํด๋์ค
public class PayServiceProxy implements PayService {
private final PayService target;
public PayServiceProxy(PayService target) {
this.target = target;
}
public void pay(int amount) {
System.out.println("[ํ๋ก์] ๊ฒฐ์ ์์ฒญ ๋ก๊ทธ ์์");
target.pay(amount); // ์ค์ ๊ฐ์ฒด ํธ์ถ
System.out.println("[ํ๋ก์] ๊ฒฐ์ ์์ฒญ ๋ก๊ทธ ๋");
}
}
4.์ฌ์ฉํ๋ ์ฝ๋
public class Main {
public static void main(String[] args) {
PayService realService = new KakaoPayService();
PayService proxy = new PayServiceProxy(realService);
proxy.pay(1000);
}
}
๊ฒฐ๊ณผ
[ํ๋ก์] ๊ฒฐ์ ์์ฒญ ๋ก๊ทธ ์์
์นด์นด์คํ์ด๋ก 1000์ ๊ฒฐ์
[ํ๋ก์] ๊ฒฐ์ ์์ฒญ ๋ก๊ทธ ๋
์ด๊ฑธ Spring์์๋ ์ด๋ป๊ฒ ์ธ๊น?
๋ฐ๋ก๋ฐ๋ก AOP (Aspect-Oriented Programming) ๊ตฌ์กฐ์ ๋ น์ ์๋ค.
๋ํ์ ์ธ ํ๋ก์ ๊ธฐ๋ฐ ๊ธฐ๋ฅ๋ค
| ์ด๋ ธํ ์ด์ | ์ค๋ช |
| @Transactional | ํธ๋์ญ์ ํ๋ก์๊ฐ ๊ฐ์ธ์ begin/commit ์ฒ๋ฆฌ |
| @Cacheable | ์บ์ ํ๋ก์๊ฐ ๊ฐ์ธ์ ๊ฒฐ๊ณผ ์ ์ฅ |
| @Secured | ๋ณด์ ์ฒดํฌ ํ๋ก์ |
| @LogExecutionTime (์ปค์คํ AOP ์์ ๋ ๊ฐ๋ฅ) | ์คํ ์๊ฐ ์ธก์ ํ๋ก์ |
์) @Transactional๋ ์ฌ์ค ํ๋ก์๋ค!
@Service
public class OrderService {
@Transactional
public void placeOrder() {
// ์ด ๋ฉ์๋ ์คํ ์ ํ๋ก ํ๋ก์๊ฐ ํธ๋์ญ์
๊ด๋ฆฌํด์ค
}
}
ํ๋ก์ ํจํด ์ ๋ฆฌ
- ๋ชฉ์ : ์คํ ์ /ํ์ ๋ถ๊ฐ๊ธฐ๋ฅ ๋ผ์๋ฃ๊ธฐ
- ๊ตฌ์กฐ : ์ค์ ๊ฐ์ฒด๋ฅผ ํ๋ก์ ๊ฐ์ฒด๊ฐ ๊ฐ์ธ์ ๋์ ํธ์ถ
- ์คํ๋ง ์ ์ฉ : ํธ๋์ญ์ , ๋ก๊น , ์บ์ฑ, ๋ณด์ ๋ฑ AOP ๊ธฐ๋ฅ์ด ์ ๋ถ ํ๋ก์ ๊ธฐ๋ฐ
๋ฐ์ํ