---
title: "unproxy-a-spring-bean"
author: "roc08"
date: "2015-01-14T23:03:41Z"
tags: []
categories: ["编程语言"]
slug: "unproxy-a-spring-bean"
draft: false
lastmod: "2015-01-15T20:02:04Z"
id: 2175976
url: "https://roc08.iteye.com/blog/2175976"
---

http://stackoverflow.com/questions/8121551/is-it-possible-to-unproxy-a-spring-bean

http://stackoverflow.com/questions/4409456/how-to-unwrap-the-original-object-from-a-dynamic-proxy

http://www.techper.net/2009/06/05/how-to-acess-target-object-behind-a-spring-proxy/

http://jinnianshilongnian.iteye.com/blog/1613222

@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
  }
}