Spring is the foundation for mastering Java skills. If you master Spring technology, you have conquered half of Java.

We use Spring Framework 5.3.10 to explore the technical framework in annotation mode

In fact, Spring is like a factory. Every time the program starts and ends, it is the process from the establishment to bankruptcy of the factory. This is a factory that produces beans. The general process is that the purchasing department first purchases raw materials, the processing department carries out preliminary processing of raw materials, and the production department is responsible for the final production of finished products.

So the factory set up, the first thing not to do is to build a factory, create department facilities, today we will first look at the process from the source point of view


Spring startup is this line of code

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
Copy the code

Take a closer look at the source code

public AnnotationConfigApplicationContext(Class<? >... componentClasses) { this(); register(componentClasses); refresh(); }Copy the code

Enter this method:

public AnnotationConfigApplicationContext() {
  StartupStep createAnnotatedBeanDefReader = this.getApplicationStartup().start("spring.context.annotated-bean-reader.create");
  this.reader = new AnnotatedBeanDefinitionReader(this);
  createAnnotatedBeanDefReader.end();
  this.scanner = new ClassPathBeanDefinitionScanner(this);
 }
Copy the code

Above is the whole source of Spring startup

Let’s take a look at their respective roles:

AnnotationConfigApplicationContext spring annotations mode is the start of the context, it is equivalent to the factory, all departments affiliated with the factory.

This. Scanner stands for scan, and this class is responsible for scanning against the configured scan path, acting as the purchasing department.

This. reader means to read. This class reads the configuration information of the scanned class and generates the BeanDefinition object, which acts as the primary processing department.

Now we have the purchasing department and the processing department, where is the production department?

In fact, not only these three departments are enough to establish a factory, but also some other small departments responsible for small functions, such as cleaning, logistics, fleet, sales, operation and so on.

We have to first take a look at AnnotationConfigApplicationContext class diagram

To analyze the first spring under this model, through the class diagram, have to say that the spring full use of object-oriented inheritance features, inheritance of some abstract classes and interfaces used correctly, this is also I in the chapter the abstract classes and object-oriented interface merge in the cause of the inheritance features, yes, object-oriented basis is very important, Even the strongest framework takes full advantage of the basics, and spring shows the power of interface-oriented programming by separating functions from each other, providing both isolation and a single responsibility.

Looking at the whole class diagram is actually the process of merging functions from the upper interface to the lower interface, and finally filling functions by the implementation class: combined with the source code, we look at the relationship

public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,MessageSource, ApplicationEventPublisher, ResourcePatternResolver
    
Copy the code

EnvironmentCapable: provides the ability to get runtime environment variables; MessageSource: Provides internationalization functionality; ApplicationEventPublisher: provide event mechanism function; ResourcePatternResolver: provides the function of loading resources. ListableBeanFactory and HierarchicalBeanFactory are subclasses of BeanFacory, which in Spring is called the Bean factory, but in our case we call it the production department, and this interface has many subinterfaces, The grandchild interface, which is inherited by the ListableBeanFactory, which adds batching capabilities to bean instances, and HierarchicalBeanFactory, which adds processing capabilities to the parent container.

The ApplicationContext interface inherits all of the above interfaces and therefore has all of the functionality provided by the above interfaces;

public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable 
Copy the code

Lifecycle: provides functions related to the container Lifecycle. Closeable: provides the function to close resources

ConfigurableApplicationContext interfaces inherited all of the above, also have the ApplicationContext, Lifecycle, Closeable interface provides all the functionality;

public abstract class AbstractApplicationContext extends DefaultResourceLoader
  implements ConfigurableApplicationContext
Copy the code

DefaultResourceLoader class implements the ResourceLoader interface to complete the interface functions, thus providing the function of resource loading, and the above ResourcePatternResolver interface is different, ResourcePatternResolver inherits the ResourceLoader interface, but adds functions to the ResourceLoader interface.

Inherited DefaultResourceLoader AbstractApplicationContext this abstract class, so it has the function of DefaultResourceLoader, and these functions have been achieved, At the same time it has realized the ConfigurableApplicationContext, we know that ConfigurableApplicationContext combines many interface function, thought the class implementing an interface implementation must implement all the methods in the interface, Includes a method enhanced by the ResourcePatternResolver interface. AbstractApplicationContext is an abstract class, which implements all of the interface at the same time, it also has its own abstract methods for subclasses class implements.

public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry
Copy the code

BeanDefinitionRegistry: Provides functionality for BeanDefinition related operations

GenericApplicationContext class inherits AbstractApplicationContext abstract class, has realized the BeanDefinitionRegistry interface, the concrete class at this time has the function of all concrete realization.

public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry 
Copy the code

AnnotationConfigRegistry: Defines the function of scanning and registration, AnnotationConfigApplicationContext eventually inherited GenericApplicationContext class, and implement the AnnotationConfigRegistry interface, Thus a complete factory has been formed, which has perfected various functions.

So where is the production department? Let me just cut to the chase,

A DefaultListableBeanFactory GenericApplicationContext class types of member variables, this class is actually one of the BeanFactory family’s children and grandchildren, when this property is initialized? We can see in GenericApplicationContext class constructor for this attribute is assigned a value, because AnnotationConfigApplicationContext is GenericApplicationContext subclass, All AnnotationConfigApplicationContext instantiation of first step calls the superclass constructor, to initialize DefaultListableBeanFactory the bean plant.

First here USES the combination, combination article said that before we can use without inheritance, interesting here, provide for the beanFactory bean operation interface, implementation by AnnotationConfigApplicationContext family, But the implementation and entrusted to the children of the family of the beanFactory AnnotationConfigApplicationContext GenericApplicationContext, this is someone do specialize, AnnotationConfigApplicationContext is as a whole the global family, work or go to the below.

In fact, everything we’ve said above is about this method.

So what do these two methods do? register(componentClasses); refresh();

In fact, after this method is done, the factory has been created. Now it’s time for the real work, and the following two methods start the whole process of producing beans. We’ll do that next time.

For more information, please follow our official wechat account: Code Nongbenong