@Test public void testGetRealTarget() { Bmw bmw = new Bmw(); ProxyFactory proxyFactory1 = new ProxyFactory(); TargetSource targetSource1 = new SingletonTargetSource(bmw); proxyFactory1.setTargetSource(targetSource1); Advisor[] advisors = new Advisor[1]; advisors[0] = advisorAdapterRegistry.wrap(new TestMethodInterceptor()); proxyFactory1.addAdvisors(advisors); Object object = proxyFactory1.getProxy(); ProxyFactory proxyFactory = new ProxyFactory(); TargetSource targetSource = new SingletonTargetSource(object); proxyFactory.setTargetSource(targetSource); Class<?>[] targetInterfaces = ClassUtils.getAllInterfacesForClass(bmw.getClass()); for (Class<?> targetInterface : targetInterfaces) { proxyFactory.addInterface(targetInterface); } advisors[0] = advisorAdapterRegistry.wrap(new TestMethodInterceptor()); proxyFactory.addAdvisors(advisors); Car proxy = (Car) proxyFactory.getProxy(); Object target = SpringProxyUtils.getRealTarget(proxy); assertEquals(bmw, target); assertEquals(bmw, SpringProxyUtils.getRealTarget(proxy)); }
public static <T> T getRealTarget(Object proxy) { Assert.notNull(proxy, "proxy not null"); while (AopUtils.isAopProxy(proxy)) { ProxyFactory proxyFactory = getProxyFactory(proxy); if (proxyFactory == null) { return null; } ConfigurablePropertyAccessor accessor = PropertyAccessorFactory.forDirectFieldAccess(proxyFactory); TargetSource targetSource = (TargetSource) accessor.getPropertyValue("targetSource"); if (targetSource == null) { return null; } try { proxy = targetSource.getTarget(); } catch (Exception e) { e.printStackTrace(); } } return (T) proxy; }
SpringProxyUtils { public static <T> T getRealTarget(Object proxy) { Assert.notNull(proxy, "proxy not null"); while (AopUtils.isAopProxy(proxy)) { ProxyFactory proxyFactory = getProxyFactory(proxy); if (proxyFactory == null) { return null; } ConfigurablePropertyAccessor accessor = PropertyAccessorFactory.forDirectFieldAccess(proxyFactory); TargetSource targetSource = (TargetSource) accessor.getPropertyValue("targetSource"); if (targetSource == null) { return null; } try { proxy = targetSource.getTarget(); } catch (Exception e) { e.printStackTrace(); } } return (T) proxy; } }
SpringProxyUtils { public static <T> T getRealTarget(Object proxy) { Assert.notNull(proxy, "proxy not null"); while (AopUtils.isAopProxy(proxy)) { ProxyFactory proxyFactory = getProxyFactory(proxy); if (proxyFactory == null) { return null; } ConfigurablePropertyAccessor accessor = PropertyAccessorFactory.forDirectFieldAccess(proxyFactory); TargetSource targetSource = (TargetSource) accessor.getPropertyValue("targetSource"); if (targetSource == null) { return null; } try { proxy = targetSource.getTarget(); } catch (Exception e) { e.printStackTrace(); } } return (T) proxy; }  }
SpringProxyUtils { public static <T> T getRealTarget(Object proxy) { Assert.notNull(proxy, "proxy not null"); while (AopUtils.isAopProxy(proxy)) { ProxyFactory proxyFactory = getProxyFactory(proxy); if (proxyFactory == null) { return null; } ConfigurablePropertyAccessor accessor = PropertyAccessorFactory.forDirectFieldAccess(proxyFactory); TargetSource targetSource = (TargetSource) accessor.getPropertyValue("targetSource"); if (targetSource == null) { return null; } try { proxy = targetSource.getTarget(); } catch (Exception e) { e.printStackTrace(); } } return (T) proxy; }  static T getRealTarget(Object proxy); static boolean isMultipleProxy(Object proxy); static ProxyFactory findJdkDynamicProxyFactory(Object proxy); static ProxyFactory findCglibProxyFactory(Object proxy); static boolean isTransactional(Object proxy); static void removeTransactional(Object proxy); static boolean isAsync(Object proxy); static void removeAsync(Object proxy); }
SpringProxyUtils { public static <T> T getRealTarget(Object proxy) { Assert.notNull(proxy, "proxy not null"); while (AopUtils.isAopProxy(proxy)) { ProxyFactory proxyFactory = getProxyFactory(proxy); if (proxyFactory == null) { return null; } ConfigurablePropertyAccessor accessor = PropertyAccessorFactory.forDirectFieldAccess(proxyFactory); TargetSource targetSource = (TargetSource) accessor.getPropertyValue("targetSource"); if (targetSource == null) { return null; } try { proxy = targetSource.getTarget(); } catch (Exception e) { e.printStackTrace(); } } return (T) proxy; }  static T getRealTarget(Object proxy); static boolean isMultipleProxy(Object proxy); static ProxyFactory findJdkDynamicProxyFactory(Object proxy); static ProxyFactory findCglibProxyFactory(Object proxy); static boolean isTransactional(Object proxy); static void removeTransactional(Object proxy); static boolean isAsync(Object proxy); static void removeAsync(Object proxy);  }