Usually when we write the configuration information we need in the configuration file application.yml, idea will first give a friendly automatic prompt, which is convenient for us to operate quickly, and it can also avoid typing wrong words, as follows:
These prompts are all built-in. These configuration-related files exist in the jar package we imported. Let's take a brief look at which file the automatic prompt of spring.application.name comes from, as follows:
It can be found that the spring.application.name property is defined in the additional-spring-configuration-metadata.json file under the META-INF folder.
So next imitate this approach, create a new META-INF folder under the resources folder of our own project, and then create a new spring-configuration-metadata.json file ( yes, it can only be named spring-configuration-metadata .json instead of additional-spring-configuration-metadata.json, otherwise it will not work ) Custom configuration items, the custom configuration information in the file is as follows:
Add the following dependencies to the pom.xml file to generate metadata information for the custom configuration class:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Then open the configuration file application.yml and try to use the custom configuration items above. The results are as follows:
It can be found that the effect of automatic prompting has appeared, and at the same time with the description in Chinese, it can allow the development to understand the function of the configuration item in more detail.
to sum up
Maintaining some of the configuration items specifically required by your own project in a unified spring-configuration-metadata.json file not only allows you to automatically prompt for custom configuration, but also develops more standardized.