preface

Now that you know the concept of BeanDefinition and what information a Bean defines in Spring, let’s move on.

Source code analysis

When initialized, a reader is entered into the method, which goes through the following logic:

GetOrCreateEnvironment (Registry) returns an Environment representing the current operating Environment and so on.

ConditionEvaluator is used to complete judgments on @Conditional annotations.

This block can be seen in the Environment Abstraction chapter on the website

N includes @Configuration Classes or @Bean Methods

supplement

BeanDefinitionRegistry: An interface for registration, removal, retrieval, and other operations on BeanDefinitions. For example, registerBeanDefinition, removeBeanDefinition, and containsBeanDefinition.

registerAnnotationConfigProcessors

Let’s look at the last line:

AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
Copy the code

This code is relatively long, so I’ll use a screenshot instead.

After adding a dependency comparator and auto-assembly parser to the beanFactory, you can start adding various processors.

Add processors, shrink the code below and ConfigurationClassPostProcessor internal logic is the same, only is call registerPostProcessor method, the effect of a few processors are introduced under this below:

  • ConfigurationClassPostProcessor used to guide the @ the Configuration class.
  • AutowiredAnnotationBeanPostProcessor processing @autowired @ Value and JSR – 330 @ Inject and @ Lookup annotation
  • CommonAnnotationBeanPostProcessor used to handle @ PostConstruct @ PreDestroy @ the Resource.
  • Add the PersistenceAnnotationBeanPostProcessor when support for JPA.
  • @ EventListener EventListenerMethodProcessor support.

registerPostProcessor

The registerPostProcessor method has two logics:

  1. Register these processors with the beanFactory;
  2. The processor is encapsulated as a BeanDefinitionHolder object.

BeanDefinitionHolder encapsulates a BeanDefinition, which means who holds the BeanDefinition.

Bold speculation

After perform this line, in this a few BeanDefinition AnnotationConfigApplicationContext.

Caution beg a certificate

The Debug start!

The forehead… Only four. Pretend to be right. Take a look step by step! There is no JSR-250 or JPA, so this is normal.

conclusion

In short, this step is to register some of Spring’s own PostProcessors.

Related to recommend

  • Spring source learning 05: BeanDefinition concept and its implementation
  • Spring source learning 04: initialize container and DefaultListableBeanFactory
  • Several ways to create an IoC container