您的位置:首页 > 编程语言 > Java开发

spring-boot实战【05】:Spring Boo多环境配置及配置属性注入到对象

2017-08-17 22:46 701 查看
项目工程结构:



配置文件application.properties文件

com.yucong.blog.name=yucong
com.yucong.blog.title=Spring Boot Course
com.yucong.blog.desc=${com.yucong.blog.name} is learing ${com.yucong.blog.title}

# 随机字符串
com.yucong.blog.value=${random.value}
# 随机int
com.yucong.blog.number=${random.int}
# 随机long
com.yucong.blog.bignumber=${random.long}
# 10以内的随机数
com.yucong.blog.test1=${random.int(10)}
# 10-20的随机数
com.yucong.blog.test2=${random.int[10,20]}

# 多环境配置文件激活属性
spring.profiles.active=dev


applicaation-dev.properties

server.port=1111


application-test.properties

server.port=2222


application-prod.properties

server.port=3333


将application.properties文件中自定义的属性注入到类的属性中

BlogProperties.java

1 package com.yucong.demo.service;
2
3 import org.springframework.beans.factory.annotation.Value;
4 import org.springframework.stereotype.Component;
5
6 /**
7  * @author yucong
8  * @version 1.0.0
9  * @date 17/8/17 下午12:16.
10  */
11 @Component
12 public class BlogProperties {
13
14     @Value("${com.yucong.blog.name}")
15     private String name;
16     @Value("${com.didispace.blog.title}")
17     private String title;
18     @Value("${com.yucong.blog.desc}")
19     private String desc;
20
21     @Value("${com.yucong.blog.value}")
22     private String value;
23     @Value("${com.yucong.blog.number}")
24     private Integer number;
25     @Value("${com.yucong.blog.bignumber}")
26     private Long bignumber;
27     @Value("${com.yucong.blog.test1}")
28     private Integer test1;
29     @Value("${com.yucong.blog.test2}")
30     private Integer test2;
31
32     public String getName() {
33         return name;
34     }
35
36     public void setName(String name) {
37         this.name = name;
38     }
39
40     public String getTitle() {
41         return title;
42     }
43
44     public void setTitle(String title) {
45         this.title = title;
46     }
47
48     public String getDesc() {
49         return desc;
50     }
51
52     public void setDesc(String desc) {
53         this.desc = desc;
54     }
55
56     public String getValue() {
57         return value;
58     }
59
60     public void setValue(String value) {
61         this.value = value;
62     }
63
64     public Integer getNumber() {
65         return number;
66     }
67
68     public void setNumber(Integer number) {
69         this.number = number;
70     }
71
72     public Long getBignumber() {
73         return bignumber;
74     }
75
76     public void setBignumber(Long bignumber) {
77         this.bignumber = bignumber;
78     }
79
80     public Integer getTest1() {
81         return test1;
82     }
83
84     public void setTest1(Integer test1) {
85         this.test1 = test1;
86     }
87
88     public Integer getTest2() {
89         return test2;
90     }
91
92     public void setTest2(Integer test2) {
93         this.test2 = test2;
94     }
95 }


单元测试YucongDemo02ApplicationTests.java:

1 package com.yucong.demo;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.junit.Assert;
6 import org.junit.Test;
7 import org.junit.runner.RunWith;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.test.context.junit4.SpringRunner;
11
12 import com.yucong.demo.service.BlogProperties;
13
14 @RunWith(SpringRunner.class)
15 @SpringBootTest(classes=YucongDemo02Application.class)
16 public class YucongDemo02ApplicationTests {
17
18     private static final Log log = LogFactory.getLog(YucongDemo02ApplicationTests.class);
19
20     @Autowired
21     private BlogProperties blogProperties;
22
23
24     @Test
25     public void test1() throws Exception {
26         Assert.assertEquals("yucong", blogProperties.getName());
27         Assert.assertEquals("Spring Boot Course", blogProperties.getTitle());
28         Assert.assertEquals("yucong is learing Spring Boot Course", blogProperties.getDesc());
29
30         log.info("随机数测试输出:");
31         log.info("随机字符串 : " + blogProperties.getValue());
32         log.info("随机int : " + blogProperties.getNumber());
33         log.info("随机long : " + blogProperties.getBignumber());
34         log.info("随机10以下 : " + blogProperties.getTest1());
35         log.info("随机10-20 : " + blogProperties.getTest2());
36
37     }
38
39 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐