In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to map the list in YAML to Java List". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to map the list in YAML to Java List".
1. Overview
In this short tutorial, we will learn more about how to map YAML lists to lists in Spring Boot.
Let's start with some background on how to define lists in YAML. Then, we'll take a closer look at how to bind an YAML list to an object list.
two。 Take a quick look at the list in YAML
In short, YAML is a human-readable data serialization standard that provides a concise and clear way to write configuration files. The advantage of YAML is that it supports multiple data types, such as list, mapping, and scalar types.
The elements in the YAML list are defined with the "-" character, and they share the same indentation level:
Yamlconfig:
List:
-item1
-item2
-item3
-item4
Compared with properties:
Yamlconfig.list [0] = item1
Yamlconfig.list [1] = item2
Yamlconfig.list [2] = item3
Yamlconfig.list [3] = item4
In fact, the hierarchy of YAML significantly enhances readability compared to properties files. Another interesting feature of YAML is the ability to define different properties for different Spring profiles.
It is worth mentioning that Spring bootstrapping provides out-of-the-box support for YAML configuration. By design, the Spring boot loads configuration properties from the application. Yml starts without any extra work.
3. Bind a YAML list to a simple list of objects
Spring Boot provides @ ConfigurationProperties annotations to simplify the logic of mapping external configuration data to the object model.
In this section, we will use @ ConfigurationProperties to bind a YAML list to list.
Let's first define a simple list in application.yml:
Application:
Profiles:
-dev
-test
-prod
-1
-2
Then, we will create a simple ApplicationProps POJO to hold the logic for binding the YAML list to the object list:
@ Component
@ ConfigurationProperties (prefix = "application")
Public class ApplicationProps {
Private List profiles
/ / getter and setter
}
The ApplicationProps class needs to be decorated with @ ConfigurationProperties to express the intention to map all YAML attributes with the specified prefix to the ApplicationProps object.
To bind the profiles list, we only need to define a field of type list, and the rest is processed by the @ ConfigurationProperties annotation.
Notice that we use @ Component to register the ApplicationProps class as a normal Spring bean. Therefore, we can inject it into other classes in the same way as any other Spring bean.
Finally, we inject ApplicationProps bean into a test class and verify that our profile YAML list is correctly injected as list:
@ ExtendWith (SpringExtension.class)
@ ContextConfiguration (initializers = ConfigFileApplicationContextInitializer.class)
@ EnableConfigurationProperties (value = ApplicationProps.class)
Class YamlSimpleListUnitTest {
@ Autowired
Private ApplicationProps applicationProps
@ Test
Public void whenYamlList_thenLoadSimpleList () {
AssertThat (applicationProps.getProfiles () .get (0)) .isEqualTo ("dev")
AssertThat (applicationProps.getProfiles (). Get (4). GetClass ()) .isEqualTo (Integer.class)
AssertThat (applicationProps.getProfiles (). Size ()) .isEqualTo (5)
}
4. Bind a YAML list to a complex list
Now, let's take a closer look at how to inject nested YAML lists into complex structured lists.
First, let's add some nested lists to application.yml:
Application:
/ /...
Props:
-
Name: YamlList
Url: http://yamllist.dev
Description: Mapping list in Yaml to list of objects in Spring Boot
-
Ip: 10.10.10.10
Port: 8091
-
Email: support@yamllist.dev
Contact: http://yamllist.dev/contact
Users:
-
Username: admin
Password: admin@10@
Roles:
-READ
-WRITE
-VIEW
-DELETE
-
Username: guest
Password: guest@01
Roles:
-VIEW
In this example, we bind the prop property to a List. Similarly, we will map users to a list of User objects.
However, in the case of the user, all items share the same key, so to simplify its mapping, we may need to create a dedicated user class that encapsulates the key as a field:
Public class ApplicationProps {
/ /...
Private List props
Private List users
/ / getters and setters
Public static class User {
Private String username
Private String password
Private List roles
/ / getters and setters
}
}
Now let's verify that the nested YAML list is mapped correctly:
@ ExtendWith (SpringExtension.class)
@ ContextConfiguration (initializers = ConfigFileApplicationContextInitializer.class)
@ EnableConfigurationProperties (value = ApplicationProps.class)
Class YamlComplexListsUnitTest {
@ Autowired
Private ApplicationProps applicationProps
@ Test
Public void whenYamlNestedLists_thenLoadComplexLists () {
AssertThat (applicationProps.getUsers (). Get (0). GetPassword ()) .isEqualTo ("admin@10@")
AssertThat (applicationProps.getProps (). Get (0). Get ("name"). IsEqualTo ("YamlList")
AssertThat (applicationProps.getProps (). Get (1). Get ("port"). GetClass () .isEqualTo (Integer.class)
}
} Thank you for reading, the above is the content of "how to map the list in YAML to Java List". After the study of this article, I believe you have a deeper understanding of how to map the list in YAML to Java List, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.