Review the class architecture and source code of the BeanFactory interface

I. Overview

 

BeanFactory is the interface definition of org.springframework.beans.factory.BeanFactory. It is a basic type of IOC container (implementing inversion of control). It provides complete IOC service support. In short, BeanFactory is a factory that manages Beans. It is mainly responsible for initializing the Bean and calling its life cycle methods.

Spring uses BeanFactory to instantiate, configure and manage Beans.

Two BeanFactory's class architecture

According to the class architecture of BeanFactory, BeanFactory has three sub-interfaces as the top-level interface, and the three interfaces have been enhanced in function:

  • ListableBeanFactory
  • HierarchicalBeanFactory
  • AutoWireCapableBeanFactory

In addition, the ConfigurableBeanFactory interface inherits the HierarchicalBeanFactory interface and has been enhanced to some extent.

AbstractBeanFactory, as an abstract class, implements most of the functions in ConfigurableBeanFactory. Similarly, AbstractAutowireCapableBeanFactory as an abstract class, it inherits the AbstractBeanFactory abstract class and also implements the AutowireCapableBeanFactory interface.

The non-abstract class DefaultListableBeanFactory inherits from AbstractAutowireCapableBeanFactory and also implements the foreign interface BeanDefinitionRegistry.

The last XmlBeanFactory has not been removed in the current Spring version.

Three source code of BeanFactory

public interface BeanFactory {

    //用来引用一个实例,或把它和工厂产生的Bean区分开
	String FACTORY_BEAN_PREFIX = "&";

	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(ResolvableType requiredType);

	boolean containsBean(String name);

	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;

	@Nullable
	Class<?> getType(String name) throws NoSuchBeanDefinitionException;

	@Nullable
	Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException;

    //根据实例的名字获取别名
	String[] getAliases(String name);
}

According to the source code, 4 getBean instance methods are overloaded in the BeanFactory interface. The second is the containsBean method that judges existence based on the name, the isSingleton method that judges whether it is a singleton based on the name, the isPrototype method that judges whether it is a prototype based on the name, and the isTypeMatch method that judges the match based on the name.

In addition to the above methods, there are also the getType method to obtain the type, and the method geAliases to obtain the alias. These methods are all obtained by name.

Guess you like

Origin blog.csdn.net/calm_encode/article/details/114010960