Spring boot custom configuration realizes automatic prompt

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:

Insert picture description here

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:

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-AE99iJVE-1606906955075)(/Volumes/Little cute/Typora/blogpic/image-20201202144759416.png)]

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:

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-mRGikAR1-1606906955078)(/Volumes/Little cute/Typora/blogpic/image-20201202145437025.png)]

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:

Insert picture description here

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.

Guess you like

Origin blog.csdn.net/weixin_38106322/article/details/110495310