How does Springboot specify to get the configuration values in the yml file
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.