First on a spring source overview diagram, the following source code learning basic can follow this diagram to look at the picture.

Spring’s most basic function as an IOC container is to define beans through configuration files (or annotations)

<beans> <bean id=? class=? abstract init-method scope dependon..... > <property name=? value=? / > < property name =? ref=? /> </bean> <bean id=? class=? abstract init-method scope dependon..... > <constructor-arg name=? value=? / > < constructor - arg name =? ref=? /> </bean> </beans>Copy the code

To get a bean and call its methods, do the following:

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
XXX xxx = ac.getBean(XXX.class);
xxx.method();
Copy the code

Spring’s process for loading beans:

Spring uses Map to store generated beans. When we obtain beans, we can use byName and byType, so there will be key:String,value:Object key-value pairs. There is also a key: Class, value: the Object of key/value pairs. The key: String, value: BeanDefinition is store the bean definition information. Key: String, value: the ObjectFactory level 3 when the cache is used.

BeanDefinition is the definition information for the storage bean, which we can look at briefly

Most methods are basically member attributes that are used when defining beans in XML. Example: DependsOn, InitMethodName. A BeanDefinition can be used to generate a bean by reflection (new can also be used to generate a bean by reflection). A single new method is not as flexible as reflection, which can fetch all the member variables and methods of a class. To obtain an object instance using reflection, you need to obtain the Class object first. The Class object can be obtained in the following three ways:

With Class, you can get an object instance with the following code:

Constructor ctor = clazz.getConstructor();
Object obj  = ctor.newInstance();
Copy the code
  • BeanFactory

The BeanFactory Bean factory, the root interface of the entire container, is also the entry point to the container

A more common bean factory is,DefaultListableBeanFactoryDefaultListableBeanFactory class diagram:

  • What if the bean information needs to be changed dynamically during container creation?

What if you want to modify beanDeifinition at any time? This needs to be handled by PostProcessor.

  • Object creation process Object creation includes object instantiation and object initialization