`

BeanFactory和FactoryBean

阅读更多

 

1、 BeanFactory

    BeanFactory定义了 IOC 容器的最基本形式,并提供了 IOC 容器应遵守的的最基本的接口,也就是 Spring IOC所遵守的最底层和最基本的编程规范。在  Spring 代码中, BeanFactory 只是个接口,并不是 IOC 容器的具体实现,但是 Spring 容器给出了很多种实现,如 DefaultListableBeanFactory 、 XmlBeanFactory 、ApplicationContext 等,都是附加了某种功能的实现。

 

 

2 FactoryBean

FactoryBean接口是插入到Spring IoC容器用来定制实例化逻辑的一个接口点。如果你有一些复杂的初始化代码用Java可以更好来表示,而不是用(可能)冗长的XML,那么你就可以创建你自己的FactoryBean,并在那个类中写入复杂的初始化动作,然后把你定制的FactoryBean插入容器中。 

FactoryBean接口对于 Spring 框架来说占用重要的地位, Spring 自身就提供了 70 多个 FactoryBean 的实现。它们隐藏了实例化一些复杂 Bean 的细节,给上层应用带来了便利。从 Spring 3.0 开始, FactoryBean 开始支持泛型,即接口声明改为 FactoryBean<T> 的形式:

 

在该接口中还定义了以下3 个方法:

T getObject():返回由 FactoryBean 创建的 Bean 实例,如果 isSingleton() 返回 true ,则该实例会放到Spring 容器中单实例缓存池中;

boolean isSingleton():返回由 FactoryBean 创建的 Bean 实例的作用域是 singleton 还是 prototype ;

Class<T> getObjectType():返回 FactoryBean 创建的 Bean 类型。

 

当配置文件中<bean> 的 class 属性配置的实现类是 FactoryBean 时,通过 getBean() 方法返回的不是FactoryBean 本身,而是 FactoryBean#getObject() 方法所返回的对象,相当于 FactoryBean#getObject() 代理了 getBean() 方法。如果希望获取 FactoryBean 的实例,则需要在使用 getBean(beanName) 方法时在 beanName 前显示的加上 "&" 前缀:如 getBean("&beanName");

 

AbstractBeanFactory的getBean方法如下

 

public Object getBean(String name) throws BeansException {
		return getBean(name, null, null);
	}

public Object getBean(String name, Class requiredType, Object[] args) throws BeansException {
		return doGetBean(name, requiredType, args, false);
	}
protected Object doGetBean(
			final String name, final Class requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException {

		final String beanName = transformedBeanName(name);
		Object bean = null;

		// Eagerly check singleton cache for manually registered singletons.
		Object sharedInstance = getSingleton(beanName);
		if (sharedInstance != null && args == null) {
			if (logger.isDebugEnabled()) {
				if (isSingletonCurrentlyInCreation(beanName)) {
					logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
							"' that is not fully initialized yet - a consequence of a circular reference");
				}
				else {
					logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
				}
			}
			bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
		}

		else {
			// Fail if we're already creating this bean instance:
			// We're assumably within a circular reference.
			if (isPrototypeCurrentlyInCreation(beanName)) {
				throw new BeanCurrentlyInCreationException(beanName);
			}

			// Check if bean definition exists in this factory.
			BeanFactory parentBeanFactory = getParentBeanFactory();
			if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
				// Not found -> check parent.
				String nameToLookup = originalBeanName(name);
				if (args != null) {
					// Delegation to parent with explicit args.
					return parentBeanFactory.getBean(nameToLookup, args);
				}
				else {
					// No args -> delegate to standard getBean method.
					return parentBeanFactory.getBean(nameToLookup, requiredType);
				}
			}

			if (!typeCheckOnly) {
				markBeanAsCreated(beanName);
			}

			final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
			checkMergedBeanDefinition(mbd, beanName, args);

			// Guarantee initialization of beans that the current bean depends on.
			String[] dependsOn = mbd.getDependsOn();
			if (dependsOn != null) {
				for (int i = 0; i < dependsOn.length; i++) {
					String dependsOnBean = dependsOn[i];
					getBean(dependsOnBean);
					registerDependentBean(dependsOnBean, beanName);
				}
			}

			// Create bean instance.
			if (mbd.isSingleton()) {
				sharedInstance = getSingleton(beanName, new ObjectFactory() {
					public Object getObject() throws BeansException {
						try {
							return createBean(beanName, mbd, args);
						}
						catch (BeansException ex) {
							// Explicitly remove instance from singleton cache: It might have been put there
							// eagerly by the creation process, to allow for circular reference resolution.
							// Also remove any beans that received a temporary reference to the bean.
							destroySingleton(beanName);
							throw ex;
						}
					}
				});
				bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
			}

			else if (mbd.isPrototype()) {
				// It's a prototype -> create a new instance.
				Object prototypeInstance = null;
				try {
					beforePrototypeCreation(beanName);
					prototypeInstance = createBean(beanName, mbd, args);
				}
				finally {
					afterPrototypeCreation(beanName);
				}
				bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
			}

			else {
				String scopeName = mbd.getScope();
				final Scope scope = (Scope) this.scopes.get(scopeName);
				if (scope == null) {
					throw new IllegalStateException("No Scope registered for scope '" + scopeName + "'");
				}
				try {
					Object scopedInstance = scope.get(beanName, new ObjectFactory() {
						public Object getObject() throws BeansException {
							beforePrototypeCreation(beanName);
							try {
								return createBean(beanName, mbd, args);
							}
							finally {
								afterPrototypeCreation(beanName);
							}
						}
					});
					bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
				}
				catch (IllegalStateException ex) {
					throw new BeanCreationException(beanName,
							"Scope '" + scopeName + "' is not active for the current thread; " +
							"consider defining a scoped proxy for this bean if you intend to refer to it from a singleton",
							ex);
				}
			}
		}

		// Check if required type matches the type of the actual bean instance.
		if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) {
			throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
		}
		return bean;
	}
 

AbstractBeanFactory的getObjectForBeanInstance方法如下:

 

protected Object getObjectForBeanInstance(
			Object beanInstance, String name, String beanName, RootBeanDefinition mbd) {

		// Don't let calling code try to dereference the factory if the bean isn't a factory.
		if (BeanFactoryUtils.isFactoryDereference(name) && !(beanInstance instanceof FactoryBean)) {
			throw new BeanIsNotAFactoryException(transformedBeanName(name), beanInstance.getClass());
		}

		// Now we have the bean instance, which may be a normal bean or a FactoryBean.
		// 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) || BeanFactoryUtils.isFactoryDereference(name)) {
			return beanInstance;
		}

		Object object = null;
		if (mbd == null) {
			object = getCachedObjectForFactoryBean(beanName);
		}
		if (object == null) {
			// Return bean instance from factory.
			FactoryBean factory = (FactoryBean) beanInstance;
			// Caches object obtained from FactoryBean if it is a singleton.
			if (mbd == null && containsBeanDefinition(beanName)) {
				mbd = getMergedLocalBeanDefinition(beanName);
			}
			boolean synthetic = (mbd != null && mbd.isSynthetic());
			object = getObjectFromFactoryBean(factory, beanName, !synthetic);
		}
		return object;
	}

 

 其中getObjectFromFactoryBean(factory, beanName, !synthetic)方法如下:

 

protected Object getObjectFromFactoryBean(FactoryBean factory, String beanName, boolean shouldPostProcess) {
		if (factory.isSingleton() && containsSingleton(beanName)) {
			synchronized (getSingletonMutex()) {
				Object object = this.factoryBeanObjectCache.get(beanName);
				if (object == null) {
					object = doGetObjectFromFactoryBean(factory, beanName, shouldPostProcess);
					this.factoryBeanObjectCache.put(beanName, (object != null ? object : NULL_OBJECT));
				}
				return (object != NULL_OBJECT ? object : null);
			}
		}
		else {
			return doGetObjectFromFactoryBean(factory, beanName, shouldPostProcess);
		}
	}

 

 其中doGetObjectFromFactoryBean(factory, beanName, shouldPostProcess)方法如下:

 

private Object doGetObjectFromFactoryBean(
			final FactoryBean factory, final String beanName, final boolean shouldPostProcess)
			throws BeanCreationException {

		AccessControlContext acc = AccessController.getContext();
		return AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				Object object;

				try {
					object = factory.getObject();
				}
				catch (FactoryBeanNotInitializedException ex) {
					throw new BeanCurrentlyInCreationException(beanName, ex.toString());
				}
				catch (Throwable ex) {
					throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);
				}

				// Do not accept a null value for a FactoryBean that's not fully
				// initialized yet: Many FactoryBeans just return null then.
				if (object == null && isSingletonCurrentlyInCreation(beanName)) {
					throw new BeanCurrentlyInCreationException(
							beanName, "FactoryBean which is currently in creation returned null from getObject");
				}

				if (object != null && shouldPostProcess) {
					try {
						object = postProcessObjectFromFactoryBean(object, beanName);
					}
					catch (Throwable ex) {
						throw new BeanCreationException(beanName, "Post-processing of the FactoryBean's object failed", ex);
					}
				}

				return object;
			}
		}, acc);
	}

    由此可见如果spring中的Bean实现了FactoryBean,那么AbstractBeanFactory的getBean方法返回的是FactoryBean的getObject()方法返回的对象。

 

3、 实例

如JndiObjectFactoryBean:public class JndiObjectFactoryBean extends JndiObjectLocator implements FactoryBean, BeanClassLoaderAware {

	private Class[] proxyInterfaces;

	private boolean lookupOnStartup = true;

	private boolean cache = true;

	private boolean exposeAccessContext = false;

	private Object defaultObject;

	private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();

	private Object jndiObject;


....


/**
	 * Return the singleton JNDI object.
	 */
	public Object getObject() {
		return this.jndiObject;
	}

。。。。

public void afterPropertiesSet() throws IllegalArgumentException, NamingException {
		super.afterPropertiesSet();

		if (this.proxyInterfaces != null || !this.lookupOnStartup || !this.cache || this.exposeAccessContext) {
			// We need to create a proxy for this...
			if (this.defaultObject != null) {
				throw new IllegalArgumentException(
						"'defaultObject' is not supported in combination with 'proxyInterface'");
			}
			// We need a proxy and a JndiObjectTargetSource.
			this.jndiObject = JndiObjectProxyFactory.createJndiObjectProxy(this);
		}
		else {
			if (this.defaultObject != null && getExpectedType() != null &&
					!getExpectedType().isInstance(this.defaultObject)) {
				throw new IllegalArgumentException("Default object [" + this.defaultObject +
						"] of type [" + this.defaultObject.getClass().getName() +
						"] is not of expected type [" + getExpectedType().getName() + "]");
			}
			// Locate specified JNDI object.
			this.jndiObject = lookupWithFallback();
		}
	}
 

其中getObect()方法返回

this.jndiObject = JndiObjectProxyFactory.createJndiObjectProxy(this);产生的对象。该法法如下:

private static class JndiObjectProxyFactory {

		private static Object createJndiObjectProxy(JndiObjectFactoryBean jof) throws NamingException {
			// Create a JndiObjectTargetSource that mirrors the JndiObjectFactoryBean's configuration.
			JndiObjectTargetSource targetSource = new JndiObjectTargetSource();
			targetSource.setJndiTemplate(jof.getJndiTemplate());
			targetSource.setJndiName(jof.getJndiName());
			targetSource.setExpectedType(jof.getExpectedType());
			targetSource.setResourceRef(jof.isResourceRef());
			targetSource.setLookupOnStartup(jof.lookupOnStartup);
			targetSource.setCache(jof.cache);
			targetSource.afterPropertiesSet();

			// Create a proxy with JndiObjectFactoryBean's proxy interface and the JndiObjectTargetSource.
			ProxyFactory proxyFactory = new ProxyFactory();
			if (jof.proxyInterfaces != null) {
				proxyFactory.setInterfaces(jof.proxyInterfaces);
			}
			else {
				Class targetClass = targetSource.getTargetClass();
				if (targetClass == null) {
					throw new IllegalStateException(
							"Cannot deactivate 'lookupOnStartup' without specifying a 'proxyInterface' or 'expectedType'");
				}
				proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, jof.beanClassLoader));
			}
			if (jof.exposeAccessContext) {
				proxyFactory.addAdvice(new JndiContextExposingInterceptor(jof.getJndiTemplate()));
			}
			proxyFactory.setTargetSource(targetSource);
			return proxyFactory.getProxy(jof.beanClassLoader);
		}
	}
 

对应的sping配置文件如下:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>java:LpscmDS</value>
		</property>
		<property name="jndiTemplate">
			<ref local="jndiTemplate" />
		</property>
		<property name="defaultObject">
			<ref local="dbcpDataSource" />
		</property>
	</bean>
	
 

4、 区别

    BeanFactory是个 Factory ,也就是 IOC 容器或对象工厂, FactoryBean 是个 Bean 。在 Spring 中,所有的Bean 都是由 BeanFactory( 也就是 IOC 容器 ) 来进行管理的。但对 FactoryBean 而言,这个 Bean 不是简单的Bean ,而是一个能生产或者修饰对象生成的工厂 Bean, 它的实现与设计模式中的工厂模式和修饰器模式类似。

 

分享到:
评论

相关推荐

    简单了解Spring中BeanFactory与FactoryBean的区别

    简单了解Spring中BeanFactory与FactoryBean的...BeanFactory和FactoryBean都是Spring框架中的重要组件,它们提供了不同的功能和机制来管理和生成Bean。理解它们之间的区别对我们使用Spring框架开发应用程序非常重要。

    68-BeanFactory与FactoryBean1

    Spring 中的 BeanFactory 和 FactoryBean BeanFactory 是 Spring 框架中的核心组件之一,负责管理 Bean 的生命周期,包括实例化、配置和注入对象之间的关系。它是 Spring IOC(控制反转)容器的核心组件,提供了...

    Spring BeanFactory和FactoryBean区别解析

    Spring BeanFactory和FactoryBean的区别解析 Spring框架中,BeanFactory和FactoryBean是两个非常重要的概念,它们都是Spring IoC容器的核心组件。今天,我们将深入探讨这两个概念之间的区别和联系。 首先,让我们...

    Spring中BeanFactory与FactoryBean接口的区别详解

    在Spring框架中,BeanFactory和FactoryBean是两个重要的接口,它们各自扮演着不同的角色,但都与Spring容器的管理和创建对象密切相关。 **BeanFactory接口**是Spring容器的基础,它是整个Spring IoC(Inversion of ...

    spring中的BeanFactory与FactoryBean的讲解

    在Spring框架中,BeanFactory和FactoryBean都是非常重要的概念,它们都是Spring IoC容器的组成部分。今天,我们将深入探讨这两个概念的讲解。 一、BeanFactory BeanFactory是Spring IOC容器的基本实现,是Spring...

    Spring源码流程图

    BeanFactory和FactoryBean的区别在于,BeanFactory需要遵循完整的创建流程,而FactoryBean只需要调用getObject()方法来返回具体对象。 Spring源码流程图是了解Spring框架核心组件和Bean生命周期的关键。通过了解...

    美团Java开发面经.pdf

    11,beanfactory和factorybean的区别? 12,用过的设计模式有哪些?介绍一下负责链模式。 算法题: 1,三个线程顺序打印十个数。 2,合并两个有序数组 反问:索引的隔离级别是什么? 面试过程中面试官会做详细记录...

    猿星人的修仙之路II

    ### 知识点三:BeanFactory 和 FactoryBean 的区别 #### 1.3.1 BeanFactory 和 FactoryBean 的区别 ##### BeanFactory - 以 Factory 结尾,表示它是一个工厂类,用于管理 Bean。 - 提供基本的 Bean 创建与依赖...

    Spring5.0介绍.pptx

    在学习了书籍Spring5.0后,将自己的理解总结成了ppt,主要包含了IoC 和 DI,AOP,BeanFactory和FactoryBean,事务管理四个方面

    java程序员面试大纲错过了金三银四你还要错过2018吗.pdf,这是一份不错的文件

    5. **Spring框架**:Spring相关的面试题目可能会涵盖BeanFactory和FactoryBean的区别、Spring的IoC和AOP原理、Bean的生命周期管理、事务管理、SpringMVC的工作流程,以及Spring中采用的设计模式等。 6. **网络编程...

    设计模式答辩题

    Spring框架中的BeanFactory和FactoryBean也使用了工厂模式,它们负责根据配置信息创建并管理Bean对象。 2. **代理模式**:代理模式为其他对象提供一种代理以控制对这个对象的访问。在Struts中,ActionProxy可以视为...

    Spring 框架的设计理念与设计模式分析

    它提供了创建和管理Bean的基础架构,同时也包含了其他核心类和接口,比如BeanFactory和FactoryBean等。 - **Core组件的特点**:Core组件更像是一个工具箱,包含了大量实用类和接口,用于支持Bean的创建和配置过程。...

    spring 中常用的设计模式.docx

    工厂模式 - BeanFactory 和 FactoryBean - **BeanFactory**:作为 Spring 的核心接口之一,BeanFactory 是简单工厂模式的一个具体应用。它通过提供`getBean(String name)`方法,允许开发者通过唯一标识符获取相应...

    JAVA面试题2019

    1. **依赖注入**:Spring框架的核心机制之一,如何通过BeanFactory和FactoryBean实现依赖注入。 2. **Spring IOC容器**:IOC容器的初始化过程,如Bean的创建、装配和销毁。 3. **ApplicationContext**:与...

    javasnmp源码-java_review:复习资料

    BeanFactory和FactoryBean区别 ​ Spring事务配置 ​ SpringBoot面试题 ​ ​ ​ Java相关 三大特性:封装、继承、多态 ​ 抽象类和接口区别 ​ Object类下的方法 ​ Collection类 ​ 反射 ​ ​ ...

    Spring6_pdf版讲义.pdf

    - **BeanFactory和FactoryBean的区别**: - `BeanFactory`:Spring的核心容器接口。 - `FactoryBean`:一种特殊的bean,可以创建并返回其他bean。 ### 八、Bean的生命周期 - **初始化阶段**:Spring容器创建bean...

    java程序员面试大纲错过了金三银四你还要错过2018吗.docx

    1. **BeanFactory和FactoryBean**:`BeanFactory`是Spring的核心接口,用于创建和管理bean实例;`FactoryBean`则是用于创建其他bean的工厂bean。 2. **Spring IOC的理解**:控制反转(Inversion of Control, IoC)是...

    Spring Framework常用面试题及答案汇总

    六、BeanFactory和FactoryBean的区别 BeanFactory是IOC底层容器,而FactoryBean是创建Bean的一种方式,帮助实现复杂的初始化逻辑。 七、BeanFactory和ObjectFactory的区别 ObjectFactory和BeanFactory均提供依赖...

    深入了解Spring中的FactoryBean

    FactoryBean和BeanFactory都是Spring框架中的核心概念,但是它们的作用不同。BeanFactory是一个工厂接口,提供了基本的IoC容器功能,而FactoryBean是一个工厂类,负责创建和管理对象实例。BeanFactory是FactoryBean...

    spring中FactoryBean中的getObject()方法实例解析

    Spring 框架中 FactoryBean 是一个非常重要的概念,它提供了一种创建和管理 Bean 的机制。在 Spring 中,FactoryBean 是一个特殊的 Bean,它可以创建其他 Bean,並提供了对这些 Bean 的管理。今天,我们将深入探讨 ...

Global site tag (gtag.js) - Google Analytics