You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
proxyClass=class com.sun.proxy.$Proxy1 이 부분이 동적으로 생성된 프록시 클래스 정보이다. 이것은 우리가 만든 클래스가 아니라 JDK 동적 프록시가 이름 그대로 동적으로 만들어준 프록시이다. 이 프록시는
TimeInvocationHandler 로직을 실행한다.
➡️ 실행 순서
클라이언트는 JDK 동적 프록시의 call() 을 실행한다.
JDK 동적 프록시는 InvocationHandler.invoke() 를 호출한다. TimeInvocationHandler 가
구현체로 있으로 TimeInvocationHandler.invoke() 가 호출된다.
TimeInvocationHandler 가 내부 로직을 수행하고, method.invoke(target, args) 를 호출해
서 target 인 실제 객체(AImpl )를 호출한다.
AImpl 인스턴스의 call() 이 실행된다.
AImpl 인스턴스의 call() 의 실행이 끝나면 TimeInvocationHandler로 응답이 돌아온다. 시간
로그를 출력하고 결과를 반환한다.
➡️ 동적프록시클래스정보
dynamicA() 와 dynamicB() 둘을 동시에 함께 실행하면 JDK 동적 프록시가 각각 다른 동적 프록시 클래스를 만들어주는 것을 확인할 수 있다.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
섹션6. 동적 프록시 기술
1️⃣ 리플렉션
➡️ ReflectionTest
➡️ ReflectionTest - reflection1 추가
Method method: 첫 번째 파라미터는 호출할 메서드 정보가 넘어온다. 이것이 핵심이다. 기존에는 메서드 이름을 직접 호출했지만, 이제는
Method라는 메타정보를 통해서 호출할 메서드 정보가 동적으로 제공된다.
Object target: 실제 실행할 인스턴스 정보가 넘어온다. 타입이Object라는 것은 어떠한 인스턴스도받을 수 있다는 뜻이다. 물론
method.invoke(target)를 사용할 때 호출할 클래스와 메서드 정보가서로 다르면 예외가 발생한다.2️⃣ JDK 동적 프록시 - 소개
지금까지 배운 내용을 보면 프록시를 적용하기 위해서 100개의 적용 대상이 있으면 100개를 맞추는 1:1 방식으로 클래스를 만들었다.
프록시 클래스의 기본 코드와 흐름은 거의 비슷하고 프록시 적용 대상만 차이가 있었다.
위와 같은 문제를 해결 할 수 있는 방법 →
동적 프록시➡️ 기본 예제 코드
JDK 동적 프록시를 이해하기 위해 아주 단순한 예제 코드로 알아보자.
A, B 클래스를 만드는데
JDK 동적 프록시는 인터페이스가 필수이다.AInterface
3️⃣ JDK 동적 프록시 - 예제 코드
➡️ JDK 동적 프록시 InvocationHandler
➡️ TimeInvocationHandler
➡️ JdkDynamicProxyTest
✅ 생성된 JDK 동적 프록시
TimeInvocationHandler 로직을 실행한다.
➡️ 실행 순서
call()을 실행한다.InvocationHandler.invoke()를 호출한다.TimeInvocationHandler가구현체로 있으로
TimeInvocationHandler.invoke()가 호출된다.TimeInvocationHandler가 내부 로직을 수행하고,method.invoke(target, args)를 호출해서
target인 실제 객체(AImpl)를 호출한다.AImpl인스턴스의call()이 실행된다.AImpl인스턴스의call()의 실행이 끝나면TimeInvocationHandler로 응답이 돌아온다. 시간로그를 출력하고 결과를 반환한다.
➡️ 동적 프록시 클래스 정보
✅ JDK 동적 프록시 도입 전 - 직접 프록시 생성
4️⃣ JDK 동적 프록시 - 적용 1
➡️ DynamicProxyBasicConfig
Controller,Service,Repository에 맞는 동적 프록시를 생성해주면 된다.LogTraceBasicHandler: 동적 프록시를 만들더라도LogTrace를 출력하는 로직은 모두 같기 때문에 프록시는 모두LogTraceBasicHandler를 사용한다.🚨남은 문제
LogTraceBasicHandler가 실행되기 때문에 로그가 남는다. 이 부분을 로그가 남지 않도록 처리해야 한다.5️⃣ JDK 동적 프록시 - 적용 2
스프링이 제공하는
PatternMatchUtils.simpleMatch(..)를 사용하면 단순한 매칭 로직을 쉽게 적용할 수 있다.xxx: xxx가 정확히 매칭되면 참xxx*: xxx로 시작하면 참*xxx: xxx로 끝나면 참*xxx*: xxx가 있으면 참String[] patterns: 적용할 패턴은 생성자를 통해서 외부에서 받는다.DynamicProxyFilterConfig
Beta Was this translation helpful? Give feedback.
All reactions