springboot reads custom profile nodes

Posted by iupme on Wed, 15 May 2019 07:35:49 +0200

Today, I share with you the reading of custom configuration information. Recently, there are plans to write blogs to write articles on spring boot and spring cloud separately, because there are many articles expected by spring boot, so cloud articles need to wait until very later to write. The main reason for sharing these two articles is to facilitate their own search and use of information. Friends to be used play a convenient role;

  • @ Value tag read (application.yml nodes can be read directly by default)
  • Entity mapping application.yml nodes
  • Nodes of Entity Mapping Custom Profile
  • Values of multi-level nodes in entity mapping

@ Value tag read (application.yml nodes can be read directly by default)

First, the following node information is customized in the default application.yml configuration file:

1 shenniu:
2 name: Shenniu 003

Then the attribute values are obtained directly through the @Value tag, and a / conf/name interface is defined to better display the data to the interface:

1     @Value("${shenniu.name}")
2     private String name;
3 
4     @GetMapping("/conf/defname")
5     public String getDefName() {
6         return this.name;
7     }

The effect after operation is as follows:

  

Entity mapping application.yml nodes

To map the default configuration file values with entity classes, you just need to prefix an attribute name, which is only for the properties in the application.yml default file:

 1 @Configuration
 2 @ConfigurationProperties(prefix = "shenniu")
 3 public class ShenniuConf {
 4 
 5     private String name;
 6 
 7     public String getName() {
 8         return name;
 9     }
10 
11     public void setName(String name) {
12         this.name = name;
13     }
14 
15     private Url url;
16 
17     public Url getUrl() {
18         return url;
19     }
20 
21     public void setUrl(Url url) {
22         this.url = url;
23     }
24 
25     private String des;
26 
27     public String getDes() {
28         return des;
29     }
30 
31     public void setDes(String des) {
32         this.des = des;
33     }
34 
35     public static class Url {
36 
37         private String blogurl;
38 
39         public String getBlogurl() {
40             return blogurl;
41         }
42 
43         public void setBlogurl(String blogurl) {
44             this.blogurl = blogurl;
45         }
46 
47     }
48 }

I added several more attributes to the entity class for the convenience of later testing. To do configuration mapping for application.yml, I just need to add @Configuration Properties (prefix = shenniu) tag. The prefix value corresponds to the prefix of the custom configuration file attributes. Here, I add the output of the / conf/name interface to map the value of the object:

1 @Autowired
2     private ShenniuConf shenniuConf;
3 
4     @GetMapping("/conf/name")
5     public String getName() {
6         return shenniuConf.getName();
7     }

The same effect is achieved with the @Values annotation, which means that the matching of entity and configuration file custom attributes is successful, and the main entity attribute names should be consistent with the configuration file node names.

  

Nodes of Entity Mapping Custom Profile

Reading the attribute values in application.yml is very simple. Sometimes you need to customize your own configuration file, such as my.properties here, which reads as follows:

1 shenniu.des = I love my motherland
2 shenniu.url.blogurl=http://www.a.com

The mapping entity still uses the above, but the entity needs to add the annotation @PropertySource("classpath:my.properties") to represent the custom file source, and also needs prefix to specify the node prefix; we add the following display interface:

1 @Autowired
2     private ShenniuConf shenniuConf;
3 
4     @GetMapping("/conf/des")
5     public String getDes() {
6         return shenniuConf.getDes();
7     }

  

Values of multi-level nodes in entity mapping

Usually, custom configuration nodes have hierarchical relationships, so how can we get attribute values at different levels? In fact, we only need to express them in the entity class through class hierarchical relations, such as the Url entity class attributes in ShenniuConf entity above.

  

Add a test interface here:

1     @Autowired
2     private ShenniuConf shenniuConf;
3     
4     @GetMapping("/conf/myconf")
5     public ShenniuConf getBlogurl() {
6         return shenniuConf;
7     }

The following results can be obtained without accident:

  

Topics: Java Attribute Spring