In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail "Springboot how to specify the configuration values in the yml file", detailed content, clear steps, details handled properly, I hope that this "Springboot how to specify how to obtain the configuration values in the yml file" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge bar.
Content:
Two methods are introduced here, both of which are based on annotations. They are:
@ Value ("${xxxxx.xx}")
@ ConfigurationProperties (prefix = "xxxxx")
Enter the topic: @ Value ("${xxxxx.xx}")
It is very easy to use this method (each annotation gets a corresponding configuration value)
Add our custom configuration items to the yml, such as (case is arbitrary, just correspond to the call):
Specify to get these values to use, such as:
@ Value ("${myKey.tua}") private String tuaKey; @ Value ("${myKey.aco}") private String acoKey; @ Value ("${mynum.new}") private String myNum; @ GetMapping ("/ getMyTest") public void getMyTest () {System.out.println ("tuaKey:" + tuaKey); System.out.println ("acoKey:" + acoKey); System.out.println ("myNum:" + myNum);}
You can see the result and get normal:
@ ConfigurationProperties (prefix = "xxxxx")
It is also very easy to use this method (get multiple configuration values at once and instantiate them into bean and put them in the spring container)
Add our custom configuration items to yml, such as (note that using the initial key parameter in lowercase, but using uppercase will make an error, because prefix does not support hump naming and underscore):
Then we create an entity class for these configuration items and use the above annotation @ ConfigurationProperties, such as:
Prefix refers to the prefix, which is usually the first, and the first one in our example is myinfo.
Import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component; / * * @ Author: JCccc * @ CreateTime: 2020-5-19 * @ Description: * * / @ Component@ConfigurationProperties (prefix = "myinfo") public class MyInfo {private String name; private Integer age; private String description @ Override public String toString () {return "MyTest {" + "name=" + name + "" + ", age=" + age + ", description="+ description +" + "}";} public String getName () {return name;} public void setName (String name) {this.name = name } public Integer getAge () {return age;} public void setAge (Integer age) {this.age = age;} public String getDescription () {return description;} public void setDescription (String description) {this.description = description;}}
Specify to get these values for use, which is equivalent to using this class, which can be used directly with @ Autowired:
@ Autowired MyInfo myInfo; @ GetMapping ("/ getMyTest") public void getMyTest () {System.out.println ("myInfo:" + myInfo.toString ()); System.out.println ("myInfo name:" + myInfo.getName ());}
You can see the result and get normal:
Read this, the "Springboot how to specify how to obtain the configuration values in the yml file" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more about the article, 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.
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.