01. Proxy๋ ?
- ๋๋ฆฌ์ธ, ์ค๊ฐ์
- ํด๋ผ์ด์ธํธ / ํธ์ถ์์ ์์ฒญ์ ์ค์ ๊ฐ์ฒด ๋์ ์ฒ๋ฆฌํด์ฃผ๋ ์ญํ
Diagram

Code
public class Service {
public void perform() {
// ์ค์ ๋ก์ง
}
}
//ํ๋ก์ ๊ฐ์ฒด
public class ServiceProxy implements Service {
private final Service target = new Service();
public void perform() {
// ์ฌ์ ์ฒ๋ฆฌ
target.perform();
// ์ฌํ ์ฒ๋ฆฌ
}
}
02. Spring Framework์์์ Proxy
1. Proxy ์์ฑ ๋ฐฉ๋ฒ
1) JDK Dynamic Proxy
- ์ธํฐํ์ด์ค ๊ธฐ๋ฐ์ผ๋ก ๋์
- ๋์ ํด๋์ค๊ฐ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๊ฒฝ์ฐ ์ฌ์ฉ
- java.lang.reflect.Proxy ํด๋์ค ํ์ฉ
2) CGLIB Proxy
- ํด๋์ค ๊ธฐ๋ฐ์ผ๋ก ๋์
- ๋์ ํด๋์ค๊ฐ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ์ง ์์ ๊ฒฝ์ฐ ์ฌ์ฉ
- ๋ฐ์ดํธ์ฝ๋ ์กฐ์์ ํตํด ํ์ ํด๋์ค ์์ฑ
2. AOP (Aspect-oriented Programming)
- ์ฌ๋ฌ ๋ชจ๋์์ ์ฌ์ฉํ๋ ๋์ผํ ๋ก์ง์ ๊ณตํต์ ์ผ๋ก ์ฒ๋ฆฌ / ๊ด๋ฆฌ ๋ชจ๋ํ๋ฅผ ํ๊ธฐ ์ํ Framework
- OOP์ ํ๊ณ๋ฅผ ๊ทน๋ณตํ์ฌ ๊ธฐ๋ฅ์ ๋ณด์ํด์ค
1. ํก๋จ ๊ด์ฌ์ฌ์ ๋ถ๋ฆฌ ์ด๋ ค์
- ๋ก๊น , ๋ณด์, ํธ๋์ญ์ ๊ฐ์ ๊ณตํต ๊ธฐ๋ฅ (ํก๋จ ๊ด์ฌ์ฌ)์ด ๋น์ฆ๋์ค ๋ก์ง ์์ ํฉ์ด์ง๊ธฐ ์ฝ๋ค.
- ๊ธฐ๋ฅ์ ํด๋์ค๋ ๋ฉ์๋ ๋จ์๋ก ๊น๋ํ ๋ถ๋ฆฌํ๊ธฐ ์ด๋ ต๊ณ , ์ฝ๋ ์ค๋ณต์ด๋ ๊ฐ๋ ์ฑ ์ ํ๋ฅผ ์ด๋ํ๋ค.
2. ๊ฐ์ฒด ๊ฐ ๊ฒฐํฉ๋ ์ฆ๊ฐ
- ๊ฐ์ฒด๊ฐ ๋ค๋ฅธ ๊ฐ์ฒด์ ๊ฐํ๊ฒ ์์กดํ๊ฒ ๋๋ฉด (์์•์ง์ ์ฐธ์กฐ) ๋ชจ๋ ๊ฐ ๊ฒฐํฉ๋๊ฐ ๋์์ง๋ค.

์ฉ์ด ์ ๋ฆฌ
- Join Point: Advice๊ฐ ์ ์ฉ ๊ฐ๋ฅํ ์ง์ (๋ฉ์๋ ์คํ ๋ฑ)
- Pointcut: Join Point ์ค ํน์ ์ง์ ์ ์ ๋ณํ๋ ํํ์
- Advice: ์ค์ ๋ก ์คํ๋๋ ๊ธฐ๋ฅ (Before, After, Around ๋ฑ)
- Aspect: Advice์ Pointcut์ ๊ฒฐํฉํ ๋ชจ๋ ๋จ์
03. AOP์ ๋ํ ์ฌ๋ก
@Transactional: ํธ๋์ญ์ ๊ฒฝ๊ณ๋ฅผ ์ค์ ํ๊ธฐ ์ํ Aspect
//without AOP
public class UserService {
private final EntityManager em;
public void save() {
EntityTransaction et = em.getTransaction()
try {
et.begin();
// ๋น์ฆ๋์ค ๋ก์ง
et.commit();
} catch(Exception e) {
et.rollback();
}
}
}
// With AOP
public class UserService {
@Transactional
public void save() {
// ๋น์ฆ๋์ค ๋ก์ง
}
}
Self-Invocation ์๊ฐ ํธ์ถ
public class UserService {
public void externalMethod() {
this.internalMethod(); // -> ๋ด๋ถ ๋ฉ์๋ ํธ์ถ(์๊ฐ ํธ์ถ)
}
@Transactional
private void internalMethod() {
}
}
Self-Invocation ์คํ ์์

์ ๊ทผ์ ์ด์์๋ ์๊ด ์์ด ๋ด๋ถ ๋ฉ์๋ ํธ์ถ์ Proxy๋ฅผ ํตํ์ง ์์์ผ๋ก Advice๊ฐ ์ ์ฉ๋์ง ์์
ํด๊ฒฐ ๋ฐฉ๋ฒ
- ์๊ธฐ ์์ ์ ์ฃผ์ ๋ฐ์ ์ฌ์ฉ
- AopContext.currentProxy()์ ํตํด ๋ช ์์ ์ผ๋ก ํธ์ถ
- Spring AOP ๋์ AspectJ์ Weaving ์ฌ์ฉ
- ํด๋์ค ๋ถ๋ฆฌ
04. JPA
์ง์ฐ ๋ก๋ฉ์ ์ํด Entity๋ฅผ ์ต์ด๋ก ๋ก๋ฉํ ๋ ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ์์ํ Proxy ๊ฐ์ฒด๋ฅผ ์์ฑ ํ ์ฐ๊ด๊ด๊ณ์ ์ถ๊ฐ
@Getter
@Entity
public class User {
private long id;
private String name;
}
//ํ๋ก์ ๊ฐ์ฒด
public class UserProxy extends User {
private long id = 1; //์ธ๋ํค๋ก ์ฌ์ฉ๋์๋ id์ ๊ฐ์ ์๊ณ ์์.
private User targetEntity = null;
@Override
public String getName() {
if(targetEntity == null) {
//1. ์์์ฑ ์ปจํ
์คํธ์ ์ค์ Entity ์์ฑ ์์ฒญ
//2. DB์์ ๋ฐ์ดํฐ ์กฐํ
//3. ์ค์ Entity ์์ฑ ๋ฐ ํ๊ฒ ๋ฑ๋ก
this.targetEntity = foundEntity;
}
return this.targetEntity.getName();
}
}
๋ฐ์ดํฐ๊ฐ ์ค์ ๋ก ํ์ํ ์์ ์ DB์ ์ง์๋ฅผ ํตํด ๊ฐ์ฒด๋ฅผ ๊ฐ์ง๊ณ ์ค๊ธฐ ๋๋ฌธ์
- ๋ถํ์ํ ์ฟผ๋ฆฌ์ ๋ฐ์์ ๋ฐฉ์ง
- ๋ถํ์ํ DB ํต์ ๋ฏธ์ฌ์ฉ ๋ฐ DB ๋ถํ ๊ฐ์
- ์ฌ์ฉํ์ง ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋ํ๋ ๋ฆฌ์์ค ๋ญ๋น ๋ฐฉ์ง
Proxy๋ฅผ ํตํ LazyLoading์ ํธ๋์ญ์ ๊ฒฝ๊ณ ๋ด์์ ๋์ํ๋ฏ๋ก LazyInitializationException์ ์ฃผ์
Persistence Content Proxy VS AOP Proxy
| ๊ตฌ๋ถ | ์์์ฑ (์ง์ฐ ๋ก๋ฉ) ํ๋ก์ | Spring AOP ํ๋ก์ |
| ๋ชฉ์ | ์ฐ๊ด ์ํฐํฐ๋ฅผ ์ค์ ๋ก ์ฌ์ฉํ ๋๊น์ง ์ง์ฐ ๋ก๋ฉ | ํก๋จ ๊ด์ฌ์ฌ (ํธ๋์ญ์ , ๋ก๊น , ๋ณด์ ๋ฑ) ์ ์ฉ |
| ๋์ ๊ฐ์ฒด | JPA ์ํฐํฐ ํด๋์ค | ์คํ๋ง ๋น (Bean) |
| ์์ฑ ์์ | EntityManager / ์ธ์ ์ด ์ํฐํฐ๋ฅผ ์ฒ์ ๋ก๋ฉํ ๋ | ์ปจํ ์ด๋ ์ด๊ธฐํ ์, ๋น ๋ฑ๋ก ๋จ๊ณ์์ |
| ํ๋ก์ ํ์ | ํด๋์ค ์๋ธํด๋์ค (CGLIB ๊ณ์ด ๋ฐ์ดํธ ์ฝ๋ ์กฐ์) | ์ธํฐํ์ด์ค๊ฐ ์์ผ๋ฉด JDK ๋ค์ด๋๋ฏน ํ๋ก์ / ์์ผ๋ฉด CGLIB ํ๋ก์ |
| ์์ฑ ์ฃผ์ฒด | JPA ๊ตฌํ์ฒด (Hibernate, EclipseLink ๋ฑ) | Spring AOP (ProxyFactoryBean ๋ฑ) |
| ๋์ ๋ฐฉ์ | ํ๋ก์ ๋ด๋ถ์ targetEntity ์ฐธ์กฐ๋ฅผ ์ง์ฐํด์ ์ฑ์ฐ๊ณ getter ํธ์ถ ์์ ์ ํ ๋ฒ๋ง DB ์กฐํ |
ํ๋ก์๊ฐ InvocatioHandler / MethodInterceptor๋ฅผ ํตํด ๋ฉ์๋ ์ง์ •์ข ๋ฃ ์์ ์ Advice (์ •ํ์ฒ๋ฆฌ)๋ฅผ ์ ์ฉ |
| ๋ฐ์ดํธ์ฝ๋ ์กฐ์ | ๋ฐํ์์ ์ํฐํฐ ํด๋์ค ์์ฒด๋ฅผ ์กฐ์ | ๋น (Bean) ๋ฑ๋ก ์์ ์ ํ๋ก์ ํด๋์ค ์์ฑ |
| ์ข ์์ฑ | JPA ํ์ค ๋ฐ ๊ตฌํ์ฒด์ ์ข ์ | Spring AOP ๋ชจ๋ (์คํ๋ง ์ฝ์ด)์ ์ข ์ |
'TIL - Today I Learned' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ์ข๋น ๊ฒ์๋ฌผ์ ๋ถํ - 0.1์ด์ ๋์์ฑ ์ฌํฌ ๐๏ธ (1) | 2026.02.10 |
|---|---|
| [ํ ํ๋ก์ ํธ - ๊ณต๋๊ตฌ๋งค ํ๋ซํผ, zero9] ์ฌ๊ณ ์ฐจ๊ฐ - ๋น๊ด์ ๋ฝ ํ ์คํธ (0) | 2026.02.09 |
| [25.1.1 TIL] ์ฑ๋ฆฌ์ง ์ธ์ - Spring IoC์ Bean (0) | 2026.01.02 |
| [25.12.31 TIL] ๋ฒ ์ด์ง ์ธ์ - Hello Spring (0) | 2025.12.31 |
| [25.12.30 TIL] ์ฑ๋ฆฐ์ง ์ธ์ - Spring Security (1) | 2025.12.30 |