// If it's a FactoryBean, we use it to create a bean instance, unless the
// caller actually wants a reference to the factory.
if (beanInstance instanceof FactoryBean) {
if (!BeanFactoryUtils.isFactoryDereference(name)) {
// Return bean instance from factory.
FactoryBean factory = (FactoryBean) beanInstance;
// Cache object obtained from FactoryBean if it is a singleton.
RootBeanDefinition mbd =
(containsBeanDefinition(beanName) ? getMergedLocalBeanDefinition(beanName) : null);
boolean shared = (mbd == null || mbd.isSingleton());
if (shared && factory.isSingleton()) {
synchronized (getSingletonMutex()) {
object = this.factoryBeanObjectCache.get(beanName);
if (object == null) {
object = getObjectFromFactoryBean(factory, beanName, mbd);
this.factoryBeanObjectCache.put(beanName, object);