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 > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the way springboot injects list and map from application.properties". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the way springboot injects list and map from application.properties?"
Demand
In the configuration class, read a complex list from application.properties. Such as List or initializing a map
Directly to the code configuration class @ Configuration@Component@ConfigurationProperties (prefix = "datahub") public class Config {private List datahubParams; private Map map; public void setMap (Map map) {this.map = map;} public Map getMap () {return map;} public List getDatahubParams () {return datahubParams;} public void setDatahubParams (List datahubParams) {this.datahubParams = datahubParams;}}
DatahubParam.java:
Public class DatahubParam {private String project; private String topic; private String subId; private String shardId; @ Override public String toString () {return "{project:" + project + ", topic:" + topic + ", subId:" + subId + ", shardId:" + shardId + "}";} public String getProject () {return project;} public void setProject (String project) {this.project = project } public String getTopic () {return topic;} public void setTopic (String topic) {this.topic = topic;} public String getSubId () {return subId;} public void setSubId (String subId) {this.subId = subId;} public String getShardId () {return shardId;} public void setShardId (String shardId) {this.shardId = shardId }}
Application.properties:
Datahub.datahub-params [0] .project = project-0datahub.datahub-params [0] .topic = topic-0datahub.datahub-params [0]. Sub-id = subId-0datahub.datahub-params [0]. Shard-id = shardId-0 datahub.datahub-params [1] .project = project-1datahub.datahub-params [1] .topic = topic-1datahub.datahub-params [1]. Sub-id = subId-1datahub.datahub-params [1]. Shard-id = shardId-1 datahub.map.username=uweidatahub. Map.age=22datahub.map.salary=15000 test class @ RunWith (SpringRunner.class) @ SpringBootTestpublic class HelloTest {@ Autowired Config config Test public void test () {System.out.println (config.getDatahubParams ()); System.out.println (config.getMap ());}}
Running result:
Error occurred in application.properties configuration injection
Configuration list,map (list contains objects) multi-layer nesting problem, failed to inject objects and could not be loaded into the configuration file
After SpringBoot cannot assign a value using @ Value, kimball has encountered a problem of application.properties configuration injection. As a novice, it's time to step on it.
Problem description
Application.properties configuration injection error, configuration list,map (list contains objects) multi-layer nesting problem, injection object failed to load into the configuration file. After asking many gods, still can not load, then take your time to test it.
My directory structure is like this: the object establishes a three-tier structure of postinfolist > postinfo > header, header contains Map key-value pairs, and postinfolist is the List collection of postinfo, so it is also a multi-layer nested format. At the beginning, my configuration is as follows:
Probably the situation should be described clearly, let's analyze the cause of the error that was not loaded into the configuration file and the analysis method.
As freshman, I just verify step by step. Since I can't load so many layers, I verify from the lowest level of header and configure header,header.headerMap.AppKey=xxx separately (header prefix pay attention to modification). The result is loaded, so good, it is not the problem of configuration file loading or introduction, but it should be written by multi-layer nesting itself.
So the second step is to go up to the next level and configure postinfo.header.headerMap.AppKey=xxx (prefix corresponding to modification, not more), and then load it again, so you can know where the problem lies. When configuring postinfolist, List is not configured correctly. In this way, we can find the problem step by step, so if there is a problem, think of the troubleshooting method first. If we find the problem, it can be easily solved. It is a bit like methodology in nature.
Well, looking back at this configuration file, we also have some understanding of the configuration of application.properties. In fact, it is to express the parameters you want to inject from the top down, that is, my upper postinfolist (Springboot helps us to find the highest level when we inject), and then look for the corresponding relationship later, that is, first find the List collection of postinfolist in the object, that is, the member variables of the object. Then we can write postinfolist.postinfolist to get the List collection, and then express the postinfo,postinfolist.postinfolist [0] in the set, which is equivalent to list.get (0). From this point of view, it is easy to understand the configuration file writing of application, and then express layer by layer to the parameters you want to inject, that is, postinfolist.postinfolist [0] .header.headerMap.AppKey. The previous writing only thought that it would be read with the prefix of prefix. In fact, Springboot does more for us, the object you write through step-by-step hierarchical relationship to analyze and inject, it is also a little respect for the newcomers to Springboot, come into contact with a period of ssh before, it is a painful process, a lot of things have to configure, and springboot really let us focus on development features rather than tedious configuration files.
At this point, I believe you have a deeper understanding of "what is the way springboot injects list and map from application.properties". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.