Created
May 7, 2015 17:49
-
-
Save geowarin/f4094e4e9ef13ee43ab2 to your computer and use it in GitHub Desktop.
Get the object behind a Spring proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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