preface
The next few articles will take you through the source code for Spring. We will look at the source code for Spring in two main sections, ioc and AOP. Before reading the source code, let's take a look at some of Spring's inheritance systems and some confusing interfaces.Copy the code
Spring’s inheritance system
The spring base container (BeanFactory) inheritance system
- BeanFactory: The top-level interface of the IOC container, which defines the basic behavior of the IOC container, is clearly the factory interface of the factory pattern
- ListableBeanFactory: Defines an interface that returns a list of beans, for example, all instances of a specified type of bean, and can also fetch all beanName.
- ConfigurableBeanFactory: Sets the set of information that the bean loads
- AutowireCapableBeanFactory: bean automatic assembly plants, it provides the function of automatic assembly, according to the class definition BeanDefinition assembly bean, execution of front and rear processor at the same time.
- DefaultListableBeanFactory: basic with a comprehensive expression of the container, it includes all parent or parent interface methods, inherited BeanDefinitionRegistry at the same time, also have the ability of BeanDefinition operation.
The inheritance of Spring’s high-level container (ApplicationContext)
You can see that the advanced container not only embodies the functionality of the base container BeanFactory but also has many additional capabilities.
- ClassPathXmlApplicationContext: mainly through XML loading Bean’s entrance
- AnnotationConfigApplicationContext: annotation configuration is to load the main Bean
After looking at the two inheritance systems, let’s first talk about one difference between the two inheritance systems:
- The ApplicationContext interface is derived from the BeanFactory interface
- BeanFactory creation creates the bean instance on the first getBean()
- The ApplicationContext creates all the singleton beans at initialization
At the same time, we need to know the difference between BeanPostProcessor and BeanFactoryPostProcessor, and BeanFactory and FactoryBean, which have similar interface names. Otherwise the back to see the source code will have meng, and the interview may also be mentioned.
The difference between BeanPostProcessor and BeanFactoryPostProcessor
- BeanFactoryPostProcessor: Preprocessing and postprocessing of the BeanDefinition before the bean instance is created. BeanDefinition stores information about the bean to be loaded, including type, name, singleton, etc.
- BeanPostProcessor: Processes bean objects before and after the bean instance is created. Some proxy classes are created using BeanPostProcessor.
Let’s look at some of the differences between BeanFactory and FactoryBean
- BeanFactory: The underlying IOC container that manages all the beans in Spring
- Factorybeans: Also spring-managed registers, but this particular bean is factory beans that are used to generate bean objects. For example, Mapper interface defined in Mybatis uses MapperFactoryBean to generate the corresponding proxy object.
conclusion
Today we have taken you through the basic architecture of the springIoc container and some of the confusing sentiments. In the next article we will start to take you through the spring source code to see the process of loading the entire bean into creation.Copy the code