Explanation of the functions of the two packages spring-boot-configuration-processor and spring-boot-autoconfigure-processor

spring-boot-configuration-processorand spring-boot-autoconfigure-processorare two common packages in Spring Boot projects, and the names of these two packages are similar. There are many introductions about these two packages on the Internet, but they are not very systematic, and some of them even have errors. Here is an explanation of what these two packages do.

spring-boot-configuration-processorthe role of

This package is mainly to analyze annotations @ConfigurationProperties, generate description information of configuration items, and store them META-INF/spring-configuration-metadata.jsonin order to improve the experience and efficiency of development.

It should be noted that this package only produces a .json file. As for how to use this file, it is the IDE's business. For example in IDEA:

The above picture is the function provided by IDEA by analyzing the .json file. Through the ctrl key + left mouse button, we can also locate the location of this property.

Why did not import this package, there will be this function

META/INFSometimes we do not import this package, but IDEA can also provide this function. This is because there is already a .json file in the folder in the package we imported . For example, the above properties are spring-boot-autoconfigurein this package. We can use this Find the .json file in the package:

The ones in the above figure additional-spring-configuration-metadata.jsonare only used as spring-configuration-metadata.jsonsupplements, and spring-boot-configuration-processorwill only be generated by default spring-configuration-metadata.json. The final content of these two files is additional-spring-configuration-metadata.jsonmainly.
So generally additional-spring-configuration-metadata.jsonspeaking, I write it myself as a supplement.

In addition, it should be declared that importing this package can only scan the places spring-boot-configuration-processorused in the source code written by ourselves@ConfigurationProperties , which will be explained in detail later.

That is to say, spring-boot-autoconfigureif the package in the above figure does not provide a .json file, introducing this package ourselves spring-boot-configuration-processorwill not allow IDEA to have a reminder function, because spring-boot-configuration-processorthe corresponding .json file can only be generated for our own source code.

A link is posted here, which is also about spring-boot-configuration-processorthe function. I think it is very good:
for the mapping of complex structures, how to make Spring Boot's configuration processor work normally (in IDEA) .

spring-boot-autoconfigure-processorthe role of

This package is mainly to analyze annotations @EnableAutoConfiguration, generate metadata for all automatic configuration class filter conditions, and store them in META-INF/spring-autoconfigure-metadata.properties, so that Spring Boot can quickly filter out automatic configuration classes that do not conform to the rules, thereby improving Spring Boot startup speed.

Also take spring-boot-autoconfigurethis package as an example, we can see this .properties file under its META/INF folder.

The file content is some of our common filtering rules, such as @ConditionalOnClass, @ConditionalOnPropertyetc. Part of the file content is as follows:

As spring-boot-configuration-processorwith , spring-boot-autoconfigure-processorit only acts on the source code you write . So if there is no automatic configuration class in your project, there is no need to introduce this package.

Here is also a link, which briefly introduces spring-boot-autoconfigure-processorthe role of spring-boot-autoconfigure-processor .

annotation processor

For spring-boot-configuration-processorand spring-boot-autoconfigure-processor, they are actually annotation processors , which is why these two packages only work on our own source code, because annotation processors only work on the compilation stage .

So for these two packages, we can add them when introducing dependencies <scope>provided</scope>, indicating that the dependencies only apply to the compilation phase.

Review how to use annotation processors in Java:

javac -processor 注解处理器类.java

This explicitly declares which annotation processor to use for processing. But generally we seldom do it, such as Lombok, and we don't use Lombok in this way, but use it directly mvn compile. This is because maven's default compiler is essentially an encapsulated javaccommand, and when javacit is used to , if no annotation processor is specified, it will go to META-INF/services/javax.annotation.processing.Processor the annotation processor declared in the file by default.

Note that the principle of this operation is to apply the SPI mechanism.

For spring-boot-configuration-processor, we can indeed find this file:

The content of the file is as follows:

javacThe corresponding section of the official documentation is attached here :

Guess you like

Origin blog.csdn.net/weixin_55658418/article/details/130559518