Difference in Spring BeanFactory and the FactoryBean

Spring and factoryBean difference in beanFactory

concept

1.BeanFactory

Ending Factory, indicating that it is a factory class, is used to manage a factory Bean's
First, we must understand that a BeanFactory bean plant, is responsible for the production and management of the bean. Is an interface for accessing the container Spring bean root interface. Specific implementation is not IOC containers, which provides the basic specifications for the IOC container. However, given many implementations Spring: As ApplicationContext, AutowireCapableBeanFactory, XmlBeanFactory, DefaultListableBeanFactory, ClassPathXmlApplicationContext the like, which is commonly used in a XmlBeanFactory, XmlBeanFactory class will hold XML configuration metadata, and use it to build a system application or fully configurable .

package org.springframework.beans.factory;

import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;

public interface BeanFactory {
	String FACTORY_BEAN_PREFIX = "&";
	/**
	* 返回给定名称注册的bean实例。根据bean的配置情况,如果是singleton模式将返回一个共享实例,
	* 否则将返回一个新建的实例,如果没有找到指定bean,该方法可能会抛出异常;
	*/
	Object getBean(String name) throws BeansException;
	<T> T getBean(String name, Class<T> requiredType) throws BeansException;
	Object getBean(String name, Object... args) throws BeansException;
	<T> T getBean(Class<T> requiredType) throws BeansException;
	<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
	
	<T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);
	<T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType);
	// 判断工厂中是否包含给定名称的bean定义,若有则返回true;
	boolean containsBean(String name);
	// 判断给定名称的bean定义是否为单例模式
	boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
	boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
	boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;
	boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;
	/**
	 * 返回给定名称的bean的Class,如果没有找到指定的bean实例,
	 * 则抛出NoSuchBeanDefinitionException异常;
	 */
	@Nullable
	Class<?> getType(String name) throws NoSuchBeanDefinitionException;
	// 返回给定bean名称的所有别名
	String[] getAliases(String name);
}


ApplicationContext BeanFactory and container are two IOC Spring framework, now generally used ApplicationnContext,
which not only includes the effect of BeanFactory, but also for more extended. BeanFacotry Spring is the more primitive Factory. The original Spring BeanFactory can not support many plug-ins, such as AOP functionality, Web applications ApplicationContext interface, which derived from the BeanFactory interface, BeanFactory ApplicationContext includes all the features of the generally recommended to use
ApplicationContext. ApplicationContext in a more oriented manner and context of the framework and layering
implementation inheritance. Also to be noted is that the Spring Framework is not used before, we use the service layer to the object layer, dao,
had a new object in the service layer, such a dependency exists between the layers. With IOC container Spring Framework provides a
service layer needs to be configured to use dao layer object to xml configuration file, as to how the object is created, the relationship is how a combination of
both to Spring framework to achieve.

2.FactorBean

Ending Bean, indicating that it is a Bean, is different from ordinary Bean: Bean FactoryBean it implements the interface, in accordance with the Id obtained from Bean is actually the FactoryBean BeanFactory getObject () returns an object, rather than FactoryBean itself, if FactoryBean to get the object, can be added in front of a symbol id & acquired.

In general, Spring specify a class using the class instance attributes of Bean tag profile by reflection, in some cases, Bean instantiation process is complex, if the conventional manner, it is necessary to provide a large amount in configuration information. Flexibility of configuration is restricted, then using coding method may get a simple solution. Spring factory class provides the interface for this org.springframework.bean.factory.FactoryBean a user can instantiate Bean by logic implementing this interface customization. FactoryBean interfaces for the Spring Framework occupy an important position, Spring provides its own implementation of more than 70 FactoryBean. They hide the details of some of the examples of complex Bean, to the upper application is made easier. From Spring3.0 start, began to support generic FactoryBean that interface declaration instead of FactoryBean form. Ending Bean, indicating that it is a Bean, is different from ordinary Bean: Bean FactoryBean it implements the interface, based on the ID obtained from the Bean BeanFactory is actually the object of the FactoryBean getObject () returns.


public interface FactoryBean<T> {

	/**
	 * Return an instance (possibly shared or independent) of the object
	 * 获取bean对应的实例对象
	 */
	@Nullable
	T getObject() throws Exception;

	/**
	 * Return the type of object that this FactoryBean creates
	 * 获取factoryBean获取到的实例类型
	 */
	@Nullable
	Class<?> getObjectType();

	/**
	 * Is the object managed by this factory a singleton? 
	 * factoryBean创建的实例是否是单实例
	 */
	default boolean isSingleton() {
		return true;
	}

}
测试代码如下:

AnnotationConfigApplicationContext applicationContext2 = new AnnotationConfigApplicationContext(MainConfig.class);

Object object = applicationContext2.getBean("getMyFactoryBean");

System.out.println("实例bean为:"+object);

Object object2 = applicationContext2.getBean("&getMyFactoryBean");

System.out.println("实例bean为:"+object2);

运行结果如下:

实例bean为 :com.test.bean.Student@3bd94634

实例bean为 : com.test.bean.CustomFactoryBean@58a90037

From the above test results that we know, based on the ID obtained from the Bean BeanFactory is actually FactoryBean of getObject () returns an object, rather than FactoryBean itself, if you want to get FactoryBean object, add an ampersand in front of the id to Obtain.

The difference 3.BeanFactory and FactoryBean

BeanFactory and FactoryBean fact no comparative, just the name of both particularly close, so sometimes will come up with something to compare, BeanFactory is to provide the most basic form of IOC container, to achieve specific IOC container provides specifications, FactoryBean can be said to provide a container for the IOC Implementation Bean a more flexible way, FactoryBean on the basis of the IOC container to achieve Bean, plus a simple factory pattern and decorative patterns, we can flexibly configure the getObject () method. In fact, there are many FactoryBean Spring source implementation class, in order to in-depth and accurate understanding FactoryBean, Spring only to read the source code.

Released seven original articles · won praise 1 · views 323

Guess you like

Origin blog.csdn.net/baidu_36882394/article/details/105229865