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

How does SpringBoot read data from configuration files to map and list

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

Share

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

Today, I would like to share with you how SpringBoot reads the data in the configuration file to the relevant knowledge points of map and list. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Read data from configuration files to map and list

You have previously used @ Value ("${name}") to read configuration information in the springboot configuration file, such as:

@ Value ("${server.port}") private Integer port

There is a new problem later. What if I want to read a series of data in the configuration file into the same data structure at once?

For example, reading configuration information to map or list

Let's talk about how to implement this function.

Springboot reads the configuration information from the configuration file to map

First, look at the information to be read into the map in the configuration file:

Test: limitSizeMap: baidu: 1024 sogou: 90 hauwei: 4096 qq: 1024

Then we need to add the following dependencies to the pom.xml file of maven:

Org.springframework.boot spring-boot-configuration-processor true

Then define a configuration class with the following code:

Package com.eknows.config;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Configuration;import java.util.HashMap;import java.util.Map / * * configuration class * reads data mapping from configuration file to map * Note: the set method * @ author eknows * @ version 1.0 * @ since must be implemented at 9:23 on 2019-2-13 * / ConfigurationConfigurationProperties (prefix = "test") EnableConfigurationProperties (MapConfig.class) public class MapConfig {/ * data at the beginning of limitSizeMap read from the configuration file * Note: the name must be Consistent with configuration file * / private Map limitSizeMap = new HashMap () Public Map getLimitSizeMap () {return limitSizeMap;} public void setLimitSizeMap (Map limitSizeMap) {this.limitSizeMap = limitSizeMap;}}

In this way, we can read out the data in the configuration file in the form of map. Key is the last suffix of the configuration information, and value is the value.

Test the code at the end of the article.

Springboot reads the configuration information from the configuration file to list

First, look at the information to be read into the list in the configuration file:

Test-list: limitSizeList [0]: "baidu: 1024" limitSizeList [1]: "sogou: 90" limitSizeList [2]: "hauwei: 4096" limitSizeList [3]: "qq: 1024"

Then add the spring-boot-configuration-processor dependency as above.

Then define the configuration class as follows:

Package com.eknows.config;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Configuration;import java.util.ArrayList;import java.util.List / * * configuration class * read data from the configuration file is mapped to list * @ author eknows * @ version 1.0 * @ since 9:34 on 2019-2-13 * / Configuration@ConfigurationProperties (prefix = "test-list") / / different configuration classes with different prefixes @ EnableConfigurationProperties (ListConfig.class) / / must indicate that this class is allowed to be configured public class ListConfig {private List limitSizeList = new ArrayList () Public List getLimitSizeList () {return limitSizeList;} public void setLimitSizeList (List limitSizeList) {this.limitSizeList = limitSizeList;}} test whether the above configuration is valid

Write test classes:

Package com.eknows;import com.eknows.config.ListConfig;import com.eknows.config.MapConfig;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import java.util.List;import java.util.Map / * @ author eknows * @ version 1.0 * @ since 9:28 on 2019-2-13 * / @ SpringBootTest@RunWith (SpringRunner.class) public class ConfigTest {@ Autowired private MapConfig mapConfig; @ Autowired private ListConfig listConfig; @ Test public void testMapConfig () {Map limitSizeMap = mapConfig.getLimitSizeMap (); if (limitSizeMap = = null | | limitSizeMap.size ()

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