SpringBoot automatic configuration process introduction

what is automatic configuration

In the past, when integrating the spring + mybatis framework, it was necessary to add a lot of beans, such as sqlSessionFactory, etc.
Now that springboot has done it for us, we only need to introduce the corresponding starter.
insert image description here
insert image description here
springBoot can help us configure some beans. Such as mysql, mogondb related operations, etc., there are currently more than 100.

We don't need to import one by one:insert image description here

A single import is too inefficient.

How to achieve batch import?

@import(AutoConfigurationImportSelector.class)
insert image description here
imports the configuration class collection through the AutoConfigurationImportSelector.selectImports method.

Of course, there can also be configuration classes in other jars, not necessarily all in the starter.

It is necessary to actively tell springBoot in other jars, indicating that it is a configuration class: the file path is meta-inf/spirng.factories
insert image description here

find steps

1 jar find file

meta-inf/spirng.factories

2 find the key

insert image description here
values ​​is a configuration class.
insert image description here

4 Deduplication

insert image description here

5 classLoader loads the class - find the automatic configuration class name

insert image description here

6 Filter irrelevant configuration classes

First look at the dependencies, see if there are any in the pom, and then see if there are key classes.
insert image description here
insert image description here
There are 135 configuration classes, many irrelevant configuration classes,
insert image description here

Through the spring-autoconfigure-metadata.propertes file

First other jvm, start the spring container, resulting in slower speed.

other

After springBoot3.0, the gravVM technology is used, and beans are scanned out at compile time. When starting later, directly register and load the bean, no need to scan again

Guess you like

Origin blog.csdn.net/qq_39463175/article/details/129787257