Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of configuring metadata in SpringBoot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how to configure metadata in SpringBoot, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Preface

When writing Spring Boot applications, it is useful to map configuration properties to Java bean. But what is the best way to record these attributes?

In this tutorial, we will explore Spring Boot Configuration Processor and the associated JSON metadata file, which records the meaning of each attribute, constraints, and so on.

Configure metadata

As developers, most of the applications we develop must be configurable to some extent. But in general, we don't really understand the role of a configuration parameter, such as it has a default value, or it's outdated, and sometimes we don't even know that this property exists.

To help us figure it out, Spring Boot generates an JSON file of configuration metadata that provides us with useful information about how to use attributes. Therefore, configuration metadata is a descriptive file that contains the necessary information to interact with configuration properties.

The real benefit of this file is that IDE can also read it, which completes the Spring properties and other configuration tips for us automatically.

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.

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 classes and methods that use this annotation 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 ());}

All right, now we're ready to use the processor.

Generate configuration metadata

As we mentioned earlier, the configuration processor generates a file-which is implemented using annotation processing.

So, after the project is compiled, 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.

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 the automatic build option is selected, each save operation triggers a build. In IntelliJ, we should trigger the build manually.

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.

Each component on the configuration metadata has its own properties. To explain the detailed use of configuration properties.

We introduced the Spring Boot configuration processor and its ability to create configuration metadata. This metadata makes it easier to interact with configuration parameters.

We give an example of the generated configuration metadata and explain its format and composition in detail.

We also saw how helpful autocomplete support on IDE can be.

The above is how SpringBoot configures metadata. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report