Initialize the Spring environment and use Javaconfig technology to rely on the appConfig configuration class to scan the specified path package under the configuration

AnnotationConfigApplicationContext annotation configuration application context

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Appconfig.java);
Copy the code

This line of code indicates that all of Spring’s prerequisite environments are ready

Be ready for what?

  • Get the bean factory ready
  • Prepare the IOC container for the bean
  • Classes are instantiated

Go into this class and see

Because this class has a parent, the constructor of the parent is called first

AnnotatedBeanDefinitionReader reader;
Copy the code

This class is, as the name suggests, a reader reader. What does it read

AnnoationBeanDefintition means an annotated bean

This class is instantiated in the constructor

We start with the idea that we have this reader to read the annotated bean

ClassPathBeanDefinitionScanner(this);
Copy the code

This Scanner is for scanning

Continue with the **** Register method

Why was the Reader reader instantiated earlier

Because you need to call the register method in there

A problem was demonstrated earlier in Appcofig**@ComponentScan**

IndexDao. Query does not exist in the Spring container. There is no scan at all

But you can register individual classes

Note that you must refresh, otherwise an error will be reported

Go to Register and call the registerBean

Registering a single bean to the container for example, if you have a new class, you can use this method. However, after registering the bean, you need to manually call the refresh method to parse the annotations. Register configuration class 2. Separate register a bean AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition (annotatedClass);Copy the code

According to create a AnnotationGenericBeanDefintition bean

The AnnotationGenericBeanDefintition can be understood as a data structure

AnnotationGenericBeanDefintition contains other information such as meta information, lazy, scope

if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {   
return;
}
Copy the code

To determine whether the class skips parsing, you can see from the code that Spring determines whether the class needs to skip parsing mainly by adding annotations

/ / get the class scope ScopeMetadata ScopeMetadata = this. ScopeMetadataResolver. ResolveScopeMetadata (abd); / / the class is added to the scope of the data structure of abd. SetScope (scopeMetadata. GetScopeName ()); String beanName = (name! = null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry)); / * * * process of general comments main treatment * * analysis of the source code can know Lazy Primary DependsOn Description Role annotation *, etc After processing is completed processCommonDefinitionAnnotations is still added to the data structure of * him/AnnotationConfigUtils processCommonDefinitionAnnotations (abd);Copy the code

/** * TODO BeanDefinitionHolder is a data structure * why BeanDefinitionHolder * BeanDefinitionHolder is an encapsulation of beanDefinition * BeanDefinitionHolder (BeanDefinitionHolder) {BeanDefinition object (beanName bean name) {beanName bean name (beanName bean name); */ BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName); BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);Copy the code

Register bd with the container

// Still in startup registration phase this.beanDefinitionMap.put(beanName, beanDefinition); this.beanDefinitionNames.add(beanName); this.manualSingletonNames.remove(beanName); This is DefaultListableBeanFactoryCopy the code