Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between BeanFactory and FactoryBean

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What are the differences between BeanFactory and FactoryBean? in view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Difference

To tell you the truth, they don't seem to have anything in common except their names.

"what's the difference between BeanFactory and FactoryBean?"

BeanFactory is a basic IOC container, which provides basic functions such as dependency lookup and dependency injection.

FactoryBean is a way to create a Bean to help create a complex Bean

There is also a high frequency interview question related to BeanFactory.

"what's the difference between ApplicationContext and BeanFactory?"

BeanFactory is a basic IOC container, which provides basic functions such as dependency lookup and dependency injection.

ApplicationContext inherits BeanFactory and adds enterprise-level functions on the basis of BeanFactory, such as AOP, resource management (Resources) events (Event), internationalization (i18n), Environment abstraction, etc.

How to create a Bean

There are four common ways to create a Bean

Through the constructor

Through the static factory method

Through the Bean factory method

Through FactoryBean

Data @ ToString public class User {private Long id; private String name; public static User createUser () {User user = new User (); user.setId (1L); user.setName ("li"); return user;}} public class UserFactory {public User createUser () {return User.createUser ();}} public class UserFactoryBean implements FactoryBean {@ Override public Object getObject () throws Exception {return User.createUser ();} @ Override public Class getObjectType () {return User.class }} public class BeanInstantiationDemo {public static void main (String [] args) {BeanFactory beanFactory = new ClassPathXmlApplicationContext ("classpath:/bean-instantiation-context.xml"); User user1 = beanFactory.getBean ("user-by-constructor", User.class); User user2 = beanFactory.getBean ("user-by-static-method", User.class); User user3 = beanFactory.getBean ("user-by-factory", User.class) User user4 = beanFactory.getBean ("user-by-factory-bean", User.class);}} implementation principle

Before analyzing the source code, let's clarify two concepts

"factoryBean is the Bean that we configured into the container to implement the FactoryBean interface, while subBean is the Bean created with FactoryBean."

During the startup of the Spring container, the non-delayed singleton Bean is instantiated, that is, the method DefaultListableBeanFactory#preInstantiateSingletons is called

The link to call FactoryBean#getObject is shown below

By analyzing the DefaultListableBeanFactory#preInstantiateSingletons method and the calling link of FactoryBean#getObject, we can get the result.

The singleton factoryBean object itself is initialized actively when the spring container starts. The initialization of subBean is triggered when the factoryBean is obtained from the cache for the first time and is not empty.

If the interface implemented by the factoryBean object is SmartFactoryBean and the isEagerInit method returns true, the subBean object will also initialize actively when the spring container starts

If the bean instance corresponding to beanName is a factoryBean when bean is registered, then the object we get through getBean (beanName) will be a subBean object; if we want to get the factory object factoryBean, we need to use getBean ("&" + beanName)

The singleton subBean is also cached in the spring container. The specific container is FactoryBeanRegistrySupport#factoryBeanObjectCache, a Map.

"I suggest you take a look at the call link between the DefaultListableBeanFactory#preInstantiateSingletons method and the FactoryBean#getObject method to understand the process I mentioned above, and I won't post too much source code."

Application

At present, I only see the application of FactoryBean in the Dubbbo source code.

"Service export: in Dubbo, the service provider is wrapped as a ServiceBean object and starts the service export when the ContextRefreshedEvent event is listened for"

"Service invocation: the service caller is packaged as a ReferenceBean object, ReferenceBean implements the FactoryBean interface and the InitializingBean interface, and the logic for creating subBean is in the ReferenceBean#getObject method."

"there are two times when Dubbo services are introduced. "

Hungry Chinese: init=true, the InitializingBean#afterPropertiesSet method is called during the initialization phase of the Bean life cycle, and this method invokes the ReferenceBean#getObject method to complete the creation of the subBean, that is, to complete the service introduction when the ReferenceBean is instantiated.

Lazy: init=false. When the service corresponding to ReferenceBean is injected into other classes, AbstractApplicationContext#getBean will be called to get the ReferenceBean object. Because ReferenceBean implements the FactoryBean interface, the ReferenceBean#getObject method will be called to complete the creation of subBean, that is, to complete the introduction of the service.

Public class ReferenceBean extends ReferenceConfig implements FactoryBean, ApplicationContextAware, InitializingBean, DisposableBean {@ Override public Object getObject () {return get ();} @ Override @ SuppressWarnings ({"unchecked"}) public void afterPropertiesSet () throws Exception {/ / omit part of the code if (shouldInit ()) {getObject () } the answers to the questions about the difference between BeanFactory and FactoryBean are shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report