/** * Obtain the singleton target object behind the given proxy, if any. * @param candidate the (potential) proxy to check * @return the singleton target object managed in a {@link SingletonTargetSource}, * or {@code null} in any other case (not a proxy, not an existing singleton target) * @since 4.3.8 * @see Advised#getTargetSource() * @see SingletonTargetSource#getTarget() */ @Nullable public static Object getSingletonTarget(Object candidate) { if (candidate instanceof Advised) { TargetSource targetSource = ((Advised) candidate).getTargetSource(); if (targetSource instanceof SingletonTargetSource) { return ((SingletonTargetSource) targetSource).getTarget(); } } return null; }
/** * Get the <em>target</em> object of the supplied {@code candidate} object. * <p>If the supplied {@code candidate} is a Spring * {@linkplain AopUtils#isAopProxy proxy}, the target of the proxy will * be returned; otherwise, the {@code candidate} will be returned * <em>as is</em>. * @param candidate the instance to check (potentially a Spring AOP proxy; * never {@code null}) * @return the target object or the {@code candidate} (never {@code null}) * @throws IllegalStateException if an error occurs while unwrapping a proxy * @see Advised#getTargetSource() * @see #getUltimateTargetObject */ @SuppressWarnings("unchecked") public static <T> T getTargetObject(Object candidate) { Assert.notNull(candidate, "Candidate must not be null"); try { if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised) { Object target = ((Advised) candidate).getTargetSource().getTarget(); if (target != null) { return (T) target; } } } catch (Throwable ex) { throw new IllegalStateException("Failed to unwrap proxied object", ex); } return (T) candidate; }
protected <T> T getTargetObject(Object proxy, Class<T> targetClass) throws Exception { if (AopUtils.isJdkDynamicProxy(proxy)) { return (T) ((Advised) proxy).getTargetSource().getTarget(); } else if (AopUtils.isCglibProxy(proxy)) { return (T) ((Advised) proxy).getTargetSource().getTarget(); } else { return (T) proxy; } } }
try { if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised) { Object target = ((Advised) candidate).getTargetSource().getTarget(); if (target != null) { return (T) getUltimateTargetObject(target);
@Test public void defaultRefreshCheckDelay() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext(CONFIG); Advised advised = (Advised) context.getBean("testBean"); AbstractRefreshableTargetSource targetSource = ((AbstractRefreshableTargetSource) advised.getTargetSource()); Field field = AbstractRefreshableTargetSource.class.getDeclaredField("refreshCheckDelay"); field.setAccessible(true); long delay = ((Long) field.get(targetSource)).longValue(); assertEquals(5000L, delay); }
private Object findTarget(Object item) { Object current = item; while (current instanceof Advised) { try { current = ((Advised) current).getTargetSource().getTarget(); } catch (Exception e) { ReflectionUtils.rethrowRuntimeException(e); } } return current; }
public static Object exposeRootBean(Object managedBean) { try { if (AopUtils.isAopProxy(managedBean) && managedBean instanceof Advised) { Advised advised = (Advised) managedBean; managedBean = advised.getTargetSource().getTarget(); } } catch (Exception e) { throw new RuntimeException(e); } return managedBean; }
private void clear(ApprovalStore approvalStore) throws Exception { if (approvalStore instanceof Advised) { Advised advised = (Advised) tokenStore; ApprovalStore target = (ApprovalStore) advised.getTargetSource().getTarget(); clear(target); return; } if (approvalStore instanceof InMemoryApprovalStore) { ((InMemoryApprovalStore) approvalStore).clear(); } if (approvalStore instanceof JdbcApprovalStore) { JdbcTemplate template = new JdbcTemplate(dataSource); template.execute("delete from oauth_approvals"); } }
private void clear(ApprovalStore approvalStore) throws Exception { if (approvalStore instanceof Advised) { Advised advised = (Advised) tokenStore; ApprovalStore target = (ApprovalStore) advised.getTargetSource().getTarget(); clear(target); return; } if (approvalStore instanceof InMemoryApprovalStore) { ((InMemoryApprovalStore) approvalStore).clear(); } if (approvalStore instanceof JdbcApprovalStore) { JdbcTemplate template = new JdbcTemplate(dataSource); template.execute("delete from oauth_approvals"); } }
@Before public void setup() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass()); afterAdviceAspect = (AfterReturningAdviceBindingTestAspect) ctx.getBean("testAspect"); mockCollaborator = mock(AfterReturningAdviceBindingCollaborator.class); afterAdviceAspect.setCollaborator(mockCollaborator); testBeanProxy = (ITestBean) ctx.getBean("testBean"); assertTrue(AopUtils.isAopProxy(testBeanProxy)); // we need the real target too, not just the proxy... this.testBeanTarget = (TestBean) ((Advised)testBeanProxy).getTargetSource().getTarget(); }
@Before public void setup() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass()); testBeanProxy = (ITestBean) ctx.getBean("testBean"); assertTrue(AopUtils.isAopProxy(testBeanProxy)); // we need the real target too, not just the proxy... testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget(); AdviceBindingTestAspect beforeAdviceAspect = (AdviceBindingTestAspect) ctx.getBean("testAspect"); mockCollaborator = mock(AdviceBindingCollaborator.class); beforeAdviceAspect.setCollaborator(mockCollaborator); }
@Before public void onSetUp() throws Exception { ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass()); AroundAdviceBindingTestAspect aroundAdviceAspect = ((AroundAdviceBindingTestAspect) ctx.getBean("testAspect")); ITestBean injectedTestBean = (ITestBean) ctx.getBean("testBean"); assertTrue(AopUtils.isAopProxy(injectedTestBean)); this.testBeanProxy = injectedTestBean; // we need the real target too, not just the proxy... this.testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget(); mockCollaborator = mock(AroundAdviceBindingCollaborator.class); aroundAdviceAspect.setCollaborator(mockCollaborator); }
@Before public void setup() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass()); AdviceBindingTestAspect afterAdviceAspect = (AdviceBindingTestAspect) ctx.getBean("testAspect"); testBeanProxy = (ITestBean) ctx.getBean("testBean"); assertTrue(AopUtils.isAopProxy(testBeanProxy)); // we need the real target too, not just the proxy... testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget(); mockCollaborator = mock(AdviceBindingCollaborator.class); afterAdviceAspect.setCollaborator(mockCollaborator); }
@Test public void testProxySerializableWithoutConfigMixin() throws Exception { Person pooled = (Person) beanFactory.getBean("pooledPerson"); //System.out.println(((Advised) pooled).toProxyConfigString()); assertTrue(((Advised) pooled).getTargetSource() instanceof CommonsPool2TargetSource); //((Advised) pooled).setTargetSource(new SingletonTargetSource(new SerializablePerson())); Person serialized = (Person) SerializationTestUtils.serializeAndDeserialize(pooled); assertTrue(((Advised) serialized).getTargetSource() instanceof SingletonTargetSource); serialized.setAge(25); assertEquals(25, serialized.getAge()); }
@Test public void testCustomPrototypeTargetSource() throws Exception { CountingTestBean.count = 0; BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS); ITestBean test = (ITestBean) bf.getBean("prototypeTest"); assertTrue(AopUtils.isAopProxy(test)); Advised advised = (Advised) test; assertTrue(advised.getTargetSource() instanceof PrototypeTargetSource); assertEquals("Rod", test.getName()); // Check that references survived prototype creation assertEquals("Kerry", test.getSpouse().getName()); assertEquals("Only 2 CountingTestBeans instantiated", 2, CountingTestBean.count); CountingTestBean.count = 0; }
@Test public void testLazyInitTargetSource() throws Exception { CountingTestBean.count = 0; BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS); ITestBean test = (ITestBean) bf.getBean("lazyInitTest"); assertTrue(AopUtils.isAopProxy(test)); Advised advised = (Advised) test; assertTrue(advised.getTargetSource() instanceof LazyInitTargetSource); assertEquals("No CountingTestBean instantiated yet", 0, CountingTestBean.count); assertEquals("Rod", test.getName()); assertEquals("Kerry", test.getSpouse().getName()); assertEquals("Only 1 CountingTestBean instantiated", 1, CountingTestBean.count); CountingTestBean.count = 0; }
@Test public void testHitMaxSizeLoadedFromContext() throws Exception { Advised person = (Advised) beanFactory.getBean("maxSizePooledPerson"); CommonsPool2TargetSource targetSource = (CommonsPool2TargetSource) person.getTargetSource(); int maxSize = targetSource.getMaxSize(); Object[] pooledInstances = new Object[maxSize]; for (int x = 0; x < maxSize; x++) { Object instance = targetSource.getTarget(); assertNotNull(instance); pooledInstances[x] = instance; } // should be at maximum now try { targetSource.getTarget(); fail("Should throw NoSuchElementException"); } catch (NoSuchElementException ex) { // desired } // lets now release an object and try to acquire a new one targetSource.releaseTarget(pooledInstances[9]); pooledInstances[9] = targetSource.getTarget(); // release all objects for (int i = 0; i < pooledInstances.length; i++) { System.out.println(i); targetSource.releaseTarget(pooledInstances[i]); } }
assertTrue(AopUtils.isAopProxy(test)); Advised advised = (Advised) test; assertTrue(advised.getTargetSource() instanceof CommonsPool2TargetSource); assertEquals("Rod", test.getName()); assertTrue(AopUtils.isAopProxy(test)); advised = (Advised) test; assertTrue(advised.getTargetSource() instanceof ThreadLocalTargetSource); assertEquals("Rod", test.getName()); assertTrue(AopUtils.isAopProxy(test)); advised = (Advised) test; assertTrue(advised.getTargetSource() instanceof PrototypeTargetSource); assertEquals("Rod", test.getName());
@Test public void testSerialization() throws Exception { SerializablePerson sp1 = new SerializablePerson(); sp1.setName("Tony"); SerializablePerson sp2 = new SerializablePerson(); sp1.setName("Gordon"); HotSwappableTargetSource hts = new HotSwappableTargetSource(sp1); ProxyFactory pf = new ProxyFactory(); pf.addInterface(Person.class); pf.setTargetSource(hts); pf.addAdvisor(new DefaultPointcutAdvisor(new SerializableNopInterceptor())); Person p = (Person) pf.getProxy(); assertEquals(sp1.getName(), p.getName()); hts.swap(sp2); assertEquals(sp2.getName(), p.getName()); p = (Person) SerializationTestUtils.serializeAndDeserialize(p); // We need to get a reference to the client-side targetsource hts = (HotSwappableTargetSource) ((Advised) p).getTargetSource(); assertEquals(sp2.getName(), p.getName()); hts.swap(sp1); assertEquals(sp1.getName(), p.getName()); } }
assertSame(hts, advised.getTargetSource()); SingletonTargetSource sts = new SingletonTargetSource(tb2); advised.setTargetSource(sts); assertEquals(tb2.getName(), proxy.getName()); assertSame(sts, advised.getTargetSource()); assertEquals(tb2.getAge(), proxy.getAge());