Skip to content

Instantly share code, notes, and snippets.

@geowarin
Created May 7, 2015 17:49
Show Gist options
  • Save geowarin/f4094e4e9ef13ee43ab2 to your computer and use it in GitHub Desktop.
Save geowarin/f4094e4e9ef13ee43ab2 to your computer and use it in GitHub Desktop.
Get the object behind a Spring proxy
package masterspringmvc4.utils;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
public class ProxyUtils {
public static <T> T getProxyTarget(Object proxy) {
if (!AopUtils.isAopProxy(proxy)) {
throw new IllegalStateException("Target must be a proxy");
}
TargetSource targetSource = ((Advised) proxy).getTargetSource();
return getTarget(targetSource);
}
private static <T> T getTarget(TargetSource targetSource) {
try {
return (T) targetSource.getTarget();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment