Java Code Examples for org.springframework.beans.factory.ObjectFactory#getObject()

The following examples show how to use org.springframework.beans.factory.ObjectFactory#getObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpSessionScope.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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;
	}
}
 
Example 2
Source File: ObjectFactoryCreatingFactoryBeanTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example 3
Source File: AbstractOpenApiResource.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
/**
 * Instantiates a new Abstract open api resource.
 *
 * @param groupName the group name
 * @param openAPIBuilderObjectFactory the open api builder object factory
 * @param requestBuilder the request builder
 * @param responseBuilder the response builder
 * @param operationParser the operation parser
 * @param operationCustomizers the operation customizers
 * @param openApiCustomisers the open api customisers
 * @param springDocConfigProperties the spring doc config properties
 * @param actuatorProvider the actuator provider
 */
protected AbstractOpenApiResource(String groupName, ObjectFactory<OpenAPIBuilder> openAPIBuilderObjectFactory,
		AbstractRequestBuilder requestBuilder,
		GenericResponseBuilder responseBuilder, OperationBuilder operationParser,
		Optional<List<OperationCustomizer>> operationCustomizers,
		Optional<List<OpenApiCustomiser>> openApiCustomisers,
		SpringDocConfigProperties springDocConfigProperties,
		Optional<ActuatorProvider> actuatorProvider) {
	super();
	this.groupName = Objects.requireNonNull(groupName, "groupName");
	this.openAPIBuilderObjectFactory = openAPIBuilderObjectFactory;
	this.openAPIBuilder = openAPIBuilderObjectFactory.getObject();
	this.requestBuilder = requestBuilder;
	this.responseBuilder = responseBuilder;
	this.operationParser = operationParser;
	this.openApiCustomisers = openApiCustomisers;
	this.springDocConfigProperties = springDocConfigProperties;
	if (operationCustomizers.isPresent())
		operationCustomizers.get().removeIf(Objects::isNull);
	this.operationCustomizers = operationCustomizers;
	this.actuatorProvider = actuatorProvider;
}
 
Example 4
Source File: ObjectFactoryCreatingFactoryBeanTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example 5
Source File: AbstractRequestAttributesScope.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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;
}
 
Example 6
Source File: AbstractRequestAttributesScope.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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;
}
 
Example 7
Source File: DefaultSingletonBeanRegistry.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Return the (raw) singleton object registered under the given name.
 * <p>Checks already instantiated singletons and also allows for an early
 * reference to a currently created singleton (resolving a circular reference).
 * @param beanName the name of the bean to look for
 * @param allowEarlyReference whether early references should be created or not
 * @return the registered singleton object, or {@code null} if none found
 */
@Nullable
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
	Object singletonObject = this.singletonObjects.get(beanName);
	// 检查缓存中是否存在实例
	if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
		// 记住,公共变量都需要加锁操作,避免多线程并发修改
		synchronized (this.singletonObjects) {
			// 如果此 bean 正在加载则不处理
			singletonObject = this.earlySingletonObjects.get(beanName);
			if (singletonObject == null && allowEarlyReference) {
				// 当某些方法需要提前初始化,调用 addSingletonFactory 方法将对应的
				// objectFactory 初始化策略存储在 earlySingletonObjects,并且从 singletonFactories 移除
				ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
				if (singletonFactory != null) {
					singletonObject = singletonFactory.getObject();
					this.earlySingletonObjects.put(beanName, singletonObject);
					this.singletonFactories.remove(beanName);
				}
			}
		}
	}
	return singletonObject;
}
 
Example 8
Source File: AnnotationDrivenEventListenerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
}
 
Example 9
Source File: SimpleThreadScope.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
}
 
Example 10
Source File: ThreadLocalScope.java    From geekbang-lessons with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {

    // 非空
    Map<String, Object> context = getContext();

    Object object = context.get(name);

    if (object == null) {
        object = objectFactory.getObject();
        context.put(name, object);
    }

    return object;
}
 
Example 11
Source File: AutowireUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: ServletContextScope.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
}
 
Example 13
Source File: SimpleTransactionScope.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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;
}
 
Example 14
Source File: SimpleMapScope.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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;
	}
}
 
Example 15
Source File: SimpleTransactionScope.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
}
 
Example 16
Source File: ObjectFactoryCreatingFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
}
 
Example 17
Source File: DependencyLookupDemo.java    From geekbang-lessons with Apache License 2.0 4 votes vote down vote up
private static void lookupInLazy(BeanFactory beanFactory) {
    ObjectFactory<User> objectFactory = (ObjectFactory<User>) beanFactory.getBean("objectFactory");
    User user = objectFactory.getObject();
    System.out.println("延迟查找:" + user);
}
 
Example 18
Source File: AutowiredConfigurationTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
ObjectFactoryConstructorConfig(ObjectFactory<Colour> colourFactory) {
	this.colour = colourFactory.getObject();
}
 
Example 19
Source File: AutowiredConfigurationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
ObjectFactoryConstructorConfig(ObjectFactory<Colour> colourFactory) {
	this.colour = colourFactory.getObject();
}
 
Example 20
Source File: Spr10744Tests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
	scopeCount++;
	return objectFactory.getObject();
}