spring-boot-configuration-processor
and spring-boot-autoconfigure-processor
are 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-processor
the role of
This package is mainly to analyze annotations @ConfigurationProperties
, generate description information of configuration items, and store them META-INF/spring-configuration-metadata.json
in 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/INF
Sometimes 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-autoconfigure
in this package. We can use this Find the .json file in the package:
The ones in the above figure
additional-spring-configuration-metadata.json
are only used asspring-configuration-metadata.json
supplements, andspring-boot-configuration-processor
will only be generated by defaultspring-configuration-metadata.json
. The final content of these two files isadditional-spring-configuration-metadata.json
mainly.
So generallyadditional-spring-configuration-metadata.json
speaking, 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-processor
used in the source code written by ourselves@ConfigurationProperties
, which will be explained in detail later.
That is to say, spring-boot-autoconfigure
if the package in the above figure does not provide a .json file, introducing this package ourselves spring-boot-configuration-processor
will not allow IDEA to have a reminder function, because spring-boot-configuration-processor
the corresponding .json file can only be generated for our own source code.
A link is posted here, which is also about
spring-boot-configuration-processor
the 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-processor
the 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-autoconfigure
this 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
, @ConditionalOnProperty
etc. Part of the file content is as follows:
As spring-boot-configuration-processor
with , spring-boot-autoconfigure-processor
it 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-processor
the role of spring-boot-autoconfigure-processor .
annotation processor
For spring-boot-configuration-processor
and 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 javac
command, and when javac
it 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:
javac
The corresponding section of the official documentation is attached here :