When developing applications using Spring Boot, have you ever noticed that a custom property has a high volume background, mouse over it, and a configuration warning that Cannot resolve Configuration Property.
If you are not bothered by this warning and want to remove it, you can remove it by setting:
However, my advice is not to leave it out, because this warning can be used to distinguish your custom configuration from the framework configuration by highlighting it, so you can quickly tell which ones are custom.
If you really want to get rid of it, it is not recommended to do so in this way, but rather by perfecting the configuration metadata. So, today, let’s talk about the application of configuration metadata!
What is configuration metadata?
Open a Spring Boot project that has been created and look at its Spring Boot dependencies. You can find a JSON file as shown below:
The error is the configured metadata information. Do you notice that the values of these names are familiar? Is description familiar? Yes, these are the metadata information of the Spring Boot native configuration that we often use.
See what configuration metadata can be used for? It helps the IDE perform configuration associations and display configuration prompts.
And our custom configuration will report a warning, at the same time there is no prompt message, because there is no metadata configuration file!
Configure automatic metadata generation
Now that you know how it works, let’s give configuration metadata a try!
Step 1: Create a configuration class to define a custom configuration
@data@configurationProperties (prefix = "com.didispace") public class DidiProperties {/** * This is a test Configuration */ private String from; }Copy the code
Step 2: Add a dependency to pom.xml that automatically generates configuration metadata
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
Copy the code
Step 3: MVN install install this project.
At this point we can find the metadata file in the project target directory:
At the same time, when we try to write this custom configuration item in the configuration file, we can see that the compiler gives us some associations and hints:
Also, after writing the configuration, there are no highlighted warnings!
Source: www.tuicool.com/articles/Zr…