Search
🌱

AOP와 Proxy

Advisor = @Aspect = Advice + PointCut

JDK Dynamic Proxy

β€’
Proxy 객체 - Invocation Handler λ₯Ό λ³€μˆ˜λ‘œ κ°€μ§„λ‹€
β€’
reflection 을 이용
β€’
μΊμŠ€νŒ…λœ 객체에 λ©”μ„œλ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ Proxy 의 invoke κ°€ λ¨Όμ € λ°˜μ‘(Proxy -1 ν–‰ μ‹€ν–‰)

문제점

β€’
interface κ°€ λ°˜λ“œμ‹œ μ‘΄μž¬ν•΄μ•Όν•œλ‹€
β—¦
μ–΄λ– ν•œ μ‹œμ μ—μ„œ λ°˜λ“œμ‹œ interface λ‘œμ„œ ν˜ΈμΆœλ˜μ–΄μ•Ό ν•œλ‹€.
β€’
reflection μ΄λΌμ„œ 무겁닀.
β€’
invoke λ©”μ„œλ“œκ°€ μƒλ‹Ήνžˆ μ œν•œμ μ΄λ‹€.

CGLIB

β€’
ProxyFactoryBean vs Enhancer
β—¦
EnhancerBySpringCGLIB vs EnhancerByCGLIB
β—¦
implements ν•˜λŠ” MethodInterceptor κ°€ λ‹€λ₯΄λ‹€
β–ͺ
org.aopalliance.intercept.MethodInterceptor;
β–ͺ
org.springframework.cglib.proxy;
β—¦
@Transactional 의 ν”„λ‘μ‹œλ“€μ€ λŒ€λΆ€λΆ„ EnhancerBySpringCGLIB or Dynamic Proxy(interface κ°€ μžˆλŠ” μ• λ“€) 이닀.

ProxyFactoryBean

β€’
Aop κ°€ κ±Έλ¦° Bean을 등둝할 λ•Œ λ“±λ‘λœλ‹€.
β€’
ν•΄λ‹Ή 객체의 λ©”μ„œλ“œκ°€ 호좜될 λ•Œ CGLIB 객체의 λ©”μ„œλ“œκ°€ 호좜되고(-1ν–‰)
β€’
DynamicAdvisedInterceptor κ°€ λ™μž‘ν•œλ‹€. μ–˜κ°€ λ“±λ‘λœ advisor λ“€ μ‹€ν–‰μ‹œν‚¨λ‹€.
β€’
advisor λ₯Ό κ°•μ œν•˜λŠ” 것 κ°™λ‹€ β†’ μ΄λŠ” 좔후에 μ°¨μ°¨ μ•Œμ•„λ³΄μž!

AspectJ

β€’
implementation group: 'org.springframework', name: 'spring-aop', version: '5.2.22.RELEASE'
β€’
μ•ˆν†΅ν•¨
β€’
Bean named 'userService' is expected to be of type 'aop.stage2.UserService' but was actually of type 'com.sun.proxy.$Proxy71’
이 μ—λŸ¬ 계속 남
β€’
UserService λŠ” @Component 뢙인 클래슀
β€’
aop κ΄€λ ¨ implementation 함
β€’
β€’
Component λŠ” EnhancerBySpringCglib 이닀.
AspectJ: AOP 기술의 원쑰, κ°€μž₯ κ°•λ ₯ν•œ AOP ν”„λ ˆμž„μ›Œν¬
AspectJ μžμ²΄λŠ” Proxy λ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€.
μŠ€ν”„λ§λ„ AspectJ 의 포인트컷 ν‘œν˜„μ‹μ„ μ°¨μš©ν•΄μ„œ μ‚¬μš©
ν”„λ‘μ‹œ 방식보닀 κ°•λ ₯ν•˜κ³  μœ μ—°ν•œ AOP κ°€ κ°€λŠ₯
β€’
였브젝트 생성, ν•„λ“œ κ°’μ˜ μ‘°νšŒμ™€ μ‘°μž‘, μŠ€νƒœν‹± μ΄ˆκΈ°ν™” λ“± λ‹€μ–‘ν•œ μž‘μ—…μ— λΆ€κ°€ κΈ°λŠ₯을 λΆ€μ—¬
Service λŠ” CGLIB, Repository λŠ” JDK Dynamic Proxy!
μ΄μœ λŠ” Proxy μƒμ„±μ‹œ if μ ˆμ— interface κ°€ μžˆλŠ”μ§€ 확인함. 있으면 JDK Dynamic Proxy 둜 생성해버림
근데 JDK Dynamic Proxy 이닀. κ·Έλž˜μ„œ μ–˜λ„ invoke handler κ°€ μ•„λ‹ˆκ³  advisor 둜 λˆλ‹€.

@Transactional

β€’
ProxyFactoryBean 으둜 μƒμ„±λœ SpringCGLIB 을 μ΄μš©ν•œλ‹€
β€’
advisor λŠ” BeanFactoryTransactionAttributeSourceAdvisor
β—¦
advice λŠ” TransactionInterceptor
β—¦
pointcut 은 TransactionAttributeSourcePointcut