In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to configure metadata in Spring Boot. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Dependence
To generate this configuration metadata, we will use spring-boot-configuration-processor dependencies.
So let's continue to add dependencies as optional dependencies:
Org.springframework.boot spring-boot-configuration-processor 2.1.7.RELEASE true
This dependency will provide us with the Java annotation processor that we call when we build the project. We will discuss this issue in detail later.
To prevent @ ConfigurationProperties from being applied to other modules used by our project, it is best to add dependencies as optional dependencies in Maven.
4. Example of configuration properties
Now to examine how the processor works, we need to use Java bean to get some properties that are included in the Spring Boot application:
@ Configuration@ConfigurationProperties (prefix = "database") public class DatabaseProperties {public static class Server {private String ip; private int port; / / standard getters and setters} private String username; private String password; private Server server; / / standard getters and setters}
To do this, we can use the @ ConfigurationProperties annotation. * * the configuration processor scans the classes and methods that use this annotation, and * * is used to access configuration parameters and generate configuration metadata.
Let's add these attributes to the properties file. In the example, we named the file databaseproperties-test.properties:
# Simple Propertiesdatabase.username=baeldungdatabase.password=password
We will also add a test to make sure we all get it right:
RunWith (SpringRunner.class) @ SpringBootTest (classes = AnnotationProcessorApplication.class) @ TestPropertySource ("classpath:databaseproperties-test.properties") public class DatabasePropertiesIntegrationTest {@ Autowired private DatabaseProperties databaseProperties; @ Test public void whenSimplePropertyQueriedThenReturnsPropertyValue () throws Exception {Assert.assertEquals ("Incorrectly bound Username property", "baeldung", databaseProperties.getUsername ()); Assert.assertEquals ("Incorrectly bound Password property", "password", databaseProperties.getPassword ());}}
We also added nested attributes database.server.id and database.server.port through the inner class Server. We should add the inner class Server and a property of server and generate its getter and setter methods.
In our test, let's take a quick check to make sure that we can also successfully set and read nested properties:
@ Testpublic void whenNestedPropertyQueriedThenReturnsPropertyValue () throws Exception {Assert.assertEquals ("Incorrectly bound Server IP nested property", "127.0.0.1", databaseProperties.getServer () .getIp ()); Assert.assertEquals ("Incorrectly bound Server Port nested property", 3306, databaseProperties.getServer () .getPort ());}
Okay, now we're ready to use the processor.
5. Generate configuration metadata
As we mentioned earlier, the configuration processor generates a file-which is implemented using annotation processing.
So, after compiling our project, we will see a file named spring-configuration-metadata.json in the directory target/classes/META-INF:
{"groups": [{"name": "database", "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties", "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"}, {"name": "database.server", "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server", "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties" "sourceMethod": "getServer ()"}], "properties": [{"name": "database.password", "type": "java.lang.String", "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"}, {"name": "database.server.ip", "type": "java.lang.String" "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server"}, {"name": "database.server.port", "type": "java.lang.Integer", "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server", "defaultValue": 0}, {"name": "database.username", "type": "java.lang.String" "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"}], "hints": []}
Next, let's look at how changing annotations on Java bean affects metadata.
5.1. Additional information about configuring metadata
First, let's add JavaDoc comments to Server.
Second, let's give a default value for the database.server.port field and finally add the @ Min and @ Max annotations:
Public static class Server {/ * * The IP of the database server * / private String ip; / * * The Port of the database server. * The Default value is 443. * The allowed values are in the range 400-4000. * / @ Min @ Max private int port = 443; / / standard getters and setters}
If we examine the spring-configuration-metadata.json file, we will see that this additional information is reflected:
{"groups": [{"name": "database", "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties", "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"}, {"name": "database.server", "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server", "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties" "sourceMethod": "getServer ()"}], "properties": [{"name": "database.password", "type": "java.lang.String", "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"}, {"name": "database.server.ip", "type": "java.lang.String" "description": "The IP of the database server", "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server"}, {"name": "database.server.port", "type": "java.lang.Integer", "description": "The Port of the database server. The Default value is 443. The allowed values are in the range 400-4000 "," sourceType ":" com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server "," defaultValue ": 443}, {" name ":" database.username "," type ":" java.lang.String "," sourceType ":" com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties "}]," hints ": []}
We can find the difference between the database.server.ip and database.server.port properties. In fact, additional information is very helpful. It is easier for developers and IDE to understand the functionality of each attribute.
We should also make sure that the build is triggered to get the updated file. In Eclipse, if we check the automatic generation option, each save operation will trigger the build. In IntelliJ, we should trigger the build manually.
5.2. Understanding metadata formats
Let's take a closer look at the JSON metadata file and discuss its composition.
Groups is a higher-level item used to group other properties without specifying the value itself. In our example, we have a database group, which is also a prefix for configuration properties. We also have a database group that treats the IP and port properties as a group through inner classes.
Property is a configuration item for which you can specify a value. These properties are configured in a file with the suffix * .properties or .yml, and can have additional information, such as default values and validation, as we saw in the example above.
Prompts are additional information that helps users set property values. For example, if we have a set of allowed values for attributes, we can provide a description of each attribute. IDE will provide automatic selection help for these prompts.
After reading the above, do you have any further understanding of how to configure metadata in Spring Boot? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.