@Override public Object get(String name, ObjectFactory<?> objectFactory) { Object scopedObject = this.servletContext.getAttribute(name); if (scopedObject == null) { scopedObject = objectFactory.getObject(); this.servletContext.setAttribute(name, scopedObject); } return scopedObject; }
@Override public Object get(String name, ObjectFactory<?> objectFactory) { Map<String, Object> scope = this.threadScope.get(); Object scopedObject = scope.get(name); if (scopedObject == null) { scopedObject = objectFactory.getObject(); scope.put(name, scopedObject); } return scopedObject; }
@Override public Object get(String name, ObjectFactory<?> objectFactory) { scopeCount++; return objectFactory.getObject(); }
@Override public Object get(String name, ObjectFactory<?> objectFactory) { return objectFactory.getObject(); } @Override
public TestBean getTestBean() { return this.testBeanFactory.getObject(); } }
public TestBean getTestBean() { return this.testBeanFactory.getObject(); } }
public TestBean getTestBean() { return (TestBean) this.testBeanFactory.getObject(); } }
ObjectFactoryConstructorConfig(ObjectFactory<Colour> colourFactory) { this.colour = colourFactory.getObject(); } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); if (methodName.equals("equals")) { // Only consider equal when proxies are identical. return (proxy == args[0]); } else if (methodName.equals("hashCode")) { // Use hashCode of proxy. return System.identityHashCode(proxy); } else if (methodName.equals("toString")) { return this.objectFactory.toString(); } try { return method.invoke(this.objectFactory.getObject(), args); } catch (InvocationTargetException ex) { throw ex.getTargetException(); } } }
/** * Resolve the given autowiring value against the given required type, * e.g. an {@link ObjectFactory} value to its actual object result. * @param autowiringValue the value to resolve * @param requiredType the type to assign the result to * @return the resolved value */ public static Object resolveAutowiringValue(Object autowiringValue, Class<?> requiredType) { if (autowiringValue instanceof ObjectFactory && !requiredType.isInstance(autowiringValue)) { ObjectFactory<?> factory = (ObjectFactory<?>) autowiringValue; if (autowiringValue instanceof Serializable && requiredType.isInterface()) { autowiringValue = Proxy.newProxyInstance(requiredType.getClassLoader(), new Class<?>[] {requiredType}, new ObjectFactoryDelegatingInvocationHandler(factory)); } else { return factory.getObject(); } } return autowiringValue; }
@Override public Object get(String name, ObjectFactory<?> objectFactory) { Assert.state(this.active, "Not active"); if (this.instance == null) { this.instance = objectFactory.getObject(); } return this.instance; }
@Override public Object get(String name, ObjectFactory<?> objectFactory) { synchronized (this.map) { Object scopedObject = this.map.get(name); if (scopedObject == null) { scopedObject = objectFactory.getObject(); this.map.put(name, scopedObject); } return scopedObject; } }
@Override public Object get(String name, ObjectFactory<?> objectFactory) { if (createNewScope) { beans.clear(); // reset the flag back createNewScope = false; } Object bean = beans.get(name); // if a new object is requested or none exists under the current // name, create one if (bean == null) { beans.put(name, objectFactory.getObject()); } return beans.get(name); }
@Override public Object get(String name, ObjectFactory<?> objectFactory) { ScopedObjectsHolder scopedObjects = (ScopedObjectsHolder) TransactionSynchronizationManager.getResource(this); if (scopedObjects == null) { scopedObjects = new ScopedObjectsHolder(); TransactionSynchronizationManager.registerSynchronization(new CleanupSynchronization(scopedObjects)); TransactionSynchronizationManager.bindResource(this, scopedObjects); } Object scopedObject = scopedObjects.scopedInstances.get(name); if (scopedObject == null) { scopedObject = objectFactory.getObject(); scopedObjects.scopedInstances.put(name, scopedObject); } return scopedObject; }
@Test public void testFactoryOperation() throws Exception { FactoryTestBean testBean = beanFactory.getBean("factoryTestBean", FactoryTestBean.class); ObjectFactory<?> objectFactory = testBean.getObjectFactory(); Date date1 = (Date) objectFactory.getObject(); Date date2 = (Date) objectFactory.getObject(); assertTrue(date1 != date2); }
@Test public void testFactorySerialization() throws Exception { FactoryTestBean testBean = beanFactory.getBean("factoryTestBean", FactoryTestBean.class); ObjectFactory<?> objectFactory = testBean.getObjectFactory(); objectFactory = (ObjectFactory) SerializationTestUtils.serializeAndDeserialize(objectFactory); Date date1 = (Date) objectFactory.getObject(); Date date2 = (Date) objectFactory.getObject(); assertTrue(date1 != date2); }
@Override public Object get(String name, ObjectFactory<?> objectFactory) { SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes(); Object scopedObject = simpAttributes.getAttribute(name); if (scopedObject != null) { return scopedObject; } synchronized (simpAttributes.getSessionMutex()) { scopedObject = simpAttributes.getAttribute(name); if (scopedObject == null) { scopedObject = objectFactory.getObject(); simpAttributes.setAttribute(name, scopedObject); } return scopedObject; } }
@Override public Object get(String name, ObjectFactory<?> objectFactory) { RequestAttributes attributes = RequestContextHolder.currentRequestAttributes(); Object scopedObject = attributes.getAttribute(name, getScope()); if (scopedObject == null) { scopedObject = objectFactory.getObject(); attributes.setAttribute(name, scopedObject, getScope()); // Retrieve object again, registering it for implicit session attribute updates. // As a bonus, we also allow for potential decoration at the getAttribute level. Object retrievedObject = attributes.getAttribute(name, getScope()); if (retrievedObject != null) { // Only proceed with retrieved object if still present (the expected case). // If it disappeared concurrently, we return our locally created instance. scopedObject = retrievedObject; } } return scopedObject; }
@Test public void getWithObjectFactory() { given(this.objectFactory.getObject()).willReturn("value"); Object actual = this.scope.get("name", this.objectFactory); assertThat(actual, is("value")); assertThat(this.simpAttributes.getAttribute("name"), is("value")); }
@Test public void testDoesNotComplainWhenTargetBeanNameRefersToSingleton() throws Exception { final String targetBeanName = "singleton"; final String expectedSingleton = "Alicia Keys"; BeanFactory beanFactory = mock(BeanFactory.class); given(beanFactory.getBean(targetBeanName)).willReturn(expectedSingleton); ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean(); factory.setTargetBeanName(targetBeanName); factory.setBeanFactory(beanFactory); factory.afterPropertiesSet(); ObjectFactory<?> objectFactory = factory.getObject(); Object actualSingleton = objectFactory.getObject(); assertSame(expectedSingleton, actualSingleton); }