Make writing a habit together! This is the fourth day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

Public shooting falcon above gao Yong, get no disadvantage.

preface

IOC is an important concept in Spring, which is essentially a map structure that stores container and business Bean information. However, the difference between BeanFactory and FactoryBean is a very important knowledge point, which will be analyzed and explained with the source code in this article.

Distinction and connection

BeanFactory

In Spring, all beans are managed by the BeanFactory (i.e. IOC container). BeanFactory defines the basic form of the container, and defines the basic interface and life cycle of the IOC container. There are also many implementation classes for BeanFactory. Such as ApplicationContext, DefaultListableBeanFactory, XmlBeanFactory are the additional function is added to the interface. Bean objects created by the BeanFactory follow the following life cycle pattern, and Bean production is implemented through reflection.

Aware means Aware in English, and you can see that the BeanFactory lifecycle process contains a number of Aware interfaces:

The following methods are relevant to the life cycle of the BeanFactory interface. In project development, ApplicationContextAware, InitializingBean, and DisposableBean have a wide range of applications.

BeanNameAware. SetBeanName is used to set the name of the Bean BeanClassLoaderAware. SetBeanClassLoader set the class loader BeanFactoryAware. SetBeanFactory . Set the bean factory ResourceLoaderAware setResourceLoader set resource loader ApplicationEventPublisherAware. SetApplicationEventPublisher . Set up the event publishing MessageSourceAware setMessageSource ApplicationContextAware. Set up information resources setApplicationContext set application context ServletContextAware. SetServletContext set the Servlet context BeanPostProcessor. PostProcessBeforeInitialization pre processor InitializingBean. AfterPropertiesSet Bean initialization RootBeanDefinition. GetInitMethodName set Bean initialization method name BeanPostProcessor. Rear postProcessAfterInitialization processor DisposableBean. Destroy set Bean destroyed RootBeanDefinition. GetDestroyMethodName get Bean destruction methodCopy the code

By default, if you call the getBean method directly, you return a factory-created object, and if you want to get the Bean itself, you prefix it with an ampersand.

FactoryBean

But in some specific cases, instantiationBeanThe operation will be very complex, according to its requirements need to configure a large number of attributes, at this timeBeanConfiguration flexibility is limited and is neededFactoryBeanThe interface can be constructed according to the user’s requirementsBeanObjects that no longer comply withBeanLifecycle processes.SpringIt has a lot to offerFactoryBeanThey hide a series of complex details of instantiation, bringing convenience to upper-layer applications. Since Srping3.0FactoryBeanStart to support generics, that is, interface declarations are changedFactoryBean<T>In the form ofFactoryBeanIn the application ofSqlSessionFactoryBeanIt is good practice to provide creation in the process of working with a databaseSqlSessionThe factoryFactory.

conclusion

The BeanFactory provides a Spring IOC container specification that provides a factory interface for producing and managing beans. But factorybeans are a special way of creating beans that can Bean extension of beans rather than following the IOC container’s specifications. Creating and using complex Bean objects encapsulates the creation details of the object.