What are the advantages of Spring: FactoryBean over BeanFactory?

FactoryBean is understood to revolve around
, so what are the advantages of
in software engineering or in using
over BeanFactory in software engineering?
Why should there be such a design pattern?

Mar.25,2021

BeanFactory is the core interface of the IOC container in Spring, following the basic interface required in the IOC container. For example, we are very common: ApplicationContext,XmlBeanFactory and so on all use the interface BeanFactory.

FactoryBean is a factory class interface, when you just want to simply construct Bean, and do not want to implement a large number of methods. It is a Bean, but this Bean can be used as a factory to create Bean, while also decorating the generation of objects.

conclusion: FactoryBean is more flexible than BeanFactory in producing Bean and can modify objects, with the design idea of factory pattern and decoration pattern in it, but it still exists in the form of Bean.

just look at MyBatis's SqlSessionFactoryBean and you can see it

.
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="typeAliasesPackage" value="com.george"/>
    <property name="typeAliasesSuperType" value="com.george.common.persistence.BaseEntity"/>
    <property name="mapperLocations" value="classpath:/mappings/**/**/*.xml"/>
    <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
</bean>

Spring will call SqlSessionFactoryBean, the factory Bean that implements FactoryBean, and load the path of the dataSource,Mapper file at the same time to initialize sqlSessionFactory (let's not paste the source code here and take a look at the source code)

because BeanFactory is the core interface, writing complex logic is easy to come into contact with other unnecessary interfaces and difficult to implement.
it's much easier if it's FactoryBean. Bean, a factory in SqlSessionFactoryBean, produces sqlSessionFactory, from different data sources and loads the corresponding configuration information at the same time.


the two are not used in the same place, so they are neither substitutive nor comparable, just like table legs and spoons. You think of the spring framework as a table, and BeanFactory is like a table leg, which is part of the frame, while FactoryBean is based on business needs within the framework. You can use spoons to drink soup and chopsticks to pick up food, but spoons and chopsticks can not replace table legs.


the answer given by Mr. Su Sheng above is already very good.
I share an article analyzing the principle of Spring framework. Spring framework design concept and design pattern analysis
there is a metaphor that I think is very appropriate.

We compare IOC to a box with several ball molds that can be used to make many different kinds of balls, and a machine that can produce ball molds. BeanFactory is the machine that makes the ball model, Bean is the ball model, and the ball created by the ball mold is an example of Bean. So where are the extension points mentioned above? BeanFactoryPostProcessor corresponds to when the ball model is created, you will have the opportunity to make appropriate changes to it, that is, it can help you modify the ball model. While InitializingBean and DisposableBean are at the beginning and end of ball making, you can do some preparatory and finishing work. BeanPostProcessor allows you to make appropriate corrections to the ball created by the ball mold. Finally, there is a FactoryBean, which is a magical ball model. The ball mold is not shaped in advance, but it is up to you to determine their shape. Since you can determine the shape of the ball mold, the ball it makes is of course the ball you want.

Spring takes Bean as the core, and generally generates the desired Bean, according to BeanDefinition. Then between the BeanDefinition and the final Bean, part of the flexibility of FactoryBean is given to the configuration (part of the custom logic of xml or JavaConfig), is handed over to the getObject method of the subclass inheriting FactoryBean; about its benefits, such as integrating third-party frameworks, such as customized factory logic, such as pooling, without the assistance of FactoryBean, it may be more complex to generate Bean from raw BeanDefinition;

the above is a little bit about FactoryBean, which is not the same thing as BeanFactory;

BeanFactory is a container, and its factory logic is to generate Bean, from raw material BeanDefinition. Its purpose is similar to that of FactoryBean, and its customized characteristics are obviously not as good as FactoryBean.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1be2e8c-3162a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1be2e8c-3162a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?