An automatic assembly process when springboot starts

IT-QI 2021-01-21 10:16:20
automatic assembly process springboot starts

I see a lot of introductions on the Internet springboot Automatically assemble the article , Come up and say @SpringBootApplication It's a compound annotation , Start with this note springboot How to load the configuration items . In fact, I don't think it should be started first spring The container of , Then you can scan to the notes , Then you can parse the annotations ? It may also be that you think that the basic operations of creating and refreshing containers are known by default , So they didn't say .

But I'm analyzing springboot When assembling automatically , First of all Method start .


We enter SpringApplication Take a look at this class run() The core implementation of the method , I've annotated almost every line . In the method , I've numbered the key points .

  1. The first is to create ApplicationContext Containers .
  2. The second is to refresh ApplicationContext Containers .

Creating ApplicationContext when , It depends on whether the user has explicitly set ApplicationContextClass Type and the result of the initialization phase , Decide for the present SpringBoot What kind of ApplicationContext.

The creation is complete ApplicationContext After container , Let's go back to In the method .

Next, we start to initialize the prompts given by various plug-ins after the exception fails .

Then perform some operations ready to refresh the context . Actually prepareContext() The method is also very critical , It serves as a connecting link between the preceding and the following . So let's see prepareContext() What is actually implemented in the method .

I've also highlighted the key points , Mainly getAllSoures() Method , In this method , The one you get source Is the startup class DemoApplication.