dev2016. 6. 2. 10:18

http://www.techper.net/2009/06/05/how-to-acess-target-object-behind-a-spring-proxy/


spring applicationContext 에 등록되어 있는 bean 을 가져왔는데,

해당 object 의 getClass 를 했을 때, 실제 클래스 대신 com.sun.proxy.$Proxy47 같은 클래스로 반환되는 경우가 있다.


이유는 해당 클래스 내부에서 사용된 @Transactional 등의 어노테이션에 의해 Proxy 로 만들어져서 그에 해당하는 처리가 된 클래스다 정도로 이해하면 될 듯.

이에 대한 보다 상세한 설명은 아래 링크를 참조.

https://spring.io/blog/2012/05/23/transactions-caching-and-aop-understanding-proxy-usage-in-spring


원 객체에 접근해야 할 때에는 아래와 같이 사용하면 된다.

한참 헤맸네. -_-ㅋ


1
2
3
4
5
6
7
8
@SuppressWarnings({"unchecked"})
protected <T> T getTargetObject(Object proxy, Class<T> targetClass) throws Exception {
  if (AopUtils.isJdkDynamicProxy(proxy)) {
    return (T) ((Advised)proxy).getTargetSource().getTarget();
  } else {
    return (T) proxy; // expected to be cglib proxy then, which is simply a specialized class
  }
}




'dev' 카테고리의 다른 글

초보자를 위한 정규표현식  (0) 2016.06.03
태스크(Task) 실행과 스케줄링  (0) 2016.06.03
eclipse 에서 python 환경 구축 - pyDev  (0) 2016.05.24
LINE BOT trial account 마감... ㅠ  (0) 2016.04.19
zookeeper 알아보기  (0) 2016.04.15