目錄
建立pom
配置屬性檔案
BlogProperties
控制器
啟動類
測試類
測試結果
說明
參考
建立pom
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
配置屬性檔案
application.properties
com.didispace.blog.name=程式猿DD
com.didispace.blog.title=Spring Boot教程
#在application.properties中的各個參數之間也可以直接引用來使用
com.didispace.blog.desc=${com.didispace.blog.name}正在努力寫《${com.didispace.blog.title}》
# 随機字元串
com.didispace.blog.value=${random.value}
# 随機int
com.didispace.blog.number=${random.int}
# 随機long
com.didispace.blog.bignumber=${random.long}
# 10以内的随機數
com.didispace.blog.test1=${random.int(10)}
# 10-20的随機數
com.didispace.blog.test2=${random.int[10,20]}
# 多環境配置檔案激活屬性
spring.profiles.active=dev
application-dev.properties
# 服務端口
server.port=1111
application-prod.properties
# 服務端口
server.port=3333
application-test.properties
# 服務端口
server.port=2222
BlogProperties
package com.didispace.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class BlogProperties {
//通過@Value("${屬性名}")注解來加載對應的配置屬性
@Value("${com.didispace.blog.name}")
private String name;
@Value("${com.didispace.blog.title}")
private String title;
@Value("${com.didispace.blog.desc}")
private String desc;
@Value("${com.didispace.blog.value}")
private String value;
@Value("${com.didispace.blog.number}")
private Integer number;
@Value("${com.didispace.blog.bignumber}")
private Long bignumber;
@Value("${com.didispace.blog.test1}")
private Integer test1;
@Value("${com.didispace.blog.test2}")
private Integer test2;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public Long getBignumber() {
return bignumber;
}
public void setBignumber(Long bignumber) {
this.bignumber = bignumber;
}
public Integer getTest1() {
return test1;
}
public void setTest1(Integer test1) {
this.test1 = test1;
}
public Integer getTest2() {
return test2;
}
public void setTest2(Integer test2) {
this.test2 = test2;
}
}
控制器
package com.didispace.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index() {
return "Hello World";
}
}
啟動類
package com.didispace;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
測試類
package com.didispace;
import com.didispace.service.BlogProperties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTests {
private static final Log log = LogFactory.getLog(ApplicationTests.class);
@Autowired
private BlogProperties blogProperties;
@Test
public void test1() throws Exception {
Assert.assertEquals("程式猿DD", blogProperties.getName());
Assert.assertEquals("Spring Boot教程", blogProperties.getTitle());
Assert.assertEquals("程式猿DD正在努力寫《Spring Boot教程》", blogProperties.getDesc());
log.info("随機數測試輸出:");
log.info("随機字元串 : " + blogProperties.getValue());
log.info("随機int : " + blogProperties.getNumber());
log.info("随機long : " + blogProperties.getBignumber());
log.info("随機10以下 : " + blogProperties.getTest1());
log.info("随機10-20 : " + blogProperties.getTest2());
}
}
測試結果
2018-10-23 18:54:24.493 INFO 11396 --- [ main] com.didispace.ApplicationTests : 随機數測試輸出:
2018-10-23 18:54:24.493 INFO 11396 --- [ main] com.didispace.ApplicationTests : 随機字元串 : e43671b09139453c06c7686b9dbb22bf
2018-10-23 18:54:24.493 INFO 11396 --- [ main] com.didispace.ApplicationTests : 随機int : 1573829413
2018-10-23 18:54:24.493 INFO 11396 --- [ main] com.didispace.ApplicationTests : 随機long : -2379855551713179344
2018-10-23 18:54:24.493 INFO 11396 --- [ main] com.didispace.ApplicationTests : 随機10以下 : 9
2018-10-23 18:54:24.493 INFO 11396 --- [ main] com.didispace.ApplicationTests : 随機10-20 : 14
說明
1 通過指令行設定屬性值
java -jar xxx.jar --server.port=8888,通過使用--server.port屬性來設定xxx.jar應用的端口為8888。
在指令行運作時,連續的兩個減号--就是對application.properties中的屬性值進行指派的辨別。是以,java -jar xxx.jar --server.port=8888指令,等價于我們在application.properties中添加屬性server.port=8888
通過指令行來修改屬性值固然提供了不錯的便利性,但是通過指令行就能更改應用運作的參數,那豈不是很不安全?是的,是以Spring Boot也貼心的提供了屏蔽指令行通路屬性的設定,隻需要這句設定就能屏蔽:SpringApplication.setAddCommandLineProperties(false)。
2 多環境配置多環境配置
我們在開發Spring Boot應用時,通常同一套程式會被應用和安裝到幾個不同的環境,比如:開發、測試、生産等。其中每個環境的資料庫位址、伺服器端口等等配置都會不同,如果在為不同環境打包時都要頻繁修改配置檔案的話,那必将是個非常繁瑣且容易發生錯誤的事。
對于多環境的配置,各種項目建構工具或是架構的基本思路是一緻的,通過配置多份不同環境的配置檔案,再通過打包指令指定需要打包的内容之後進行區分打包,Spring Boot也不例外,或者說更加簡單。
在Spring Boot中多環境配置檔案名需要滿足application-{profile}.properties的格式,其中{profile}對應你的環境辨別,比如:
- application-dev.properties:開發環境
- application-test.properties:測試環境
- application-prod.properties:生産環境
至于哪個具體的配置檔案會被加載,需要在application.properties檔案中通過spring.profiles.active屬性來設定,其值對應{profile}值。
測試不同配置的加載
- 執行java -jar xxx.jar,可以觀察到服務端口被設定為1111,也就是預設的開發環境(dev)
- 執行java -jar xxx.jar --spring.profiles.active=test,可以觀察到服務端口被設定為2222,也就是測試環境的配置(test)
- 執行java -jar xxx.jar --spring.profiles.active=prod,可以觀察到服務端口被設定為3333,也就是生産環境的配置(prod)
總結多環境的配置思路
- application.properties中配置通用内容,并設定spring.profiles.active=dev,以開發環境為預設配置。
- application-{profile}.properties中配置各個環境不同的内容。
- 通過指令行方式去激活不同環境的配置。
3 配置yml和配置Properties都可以擷取到值,強烈推薦使用yml。
4 如果我們在某個業務中,隻需要擷取配置檔案中的某個值,可以使用@value
5 如果說,我們專門編寫一個JavaBean來和配置檔案進行映射,就直接使用@configurationProperties,不用猶豫。
6 屬性配置檔案優先級:
file和class目錄的根目錄見下面截圖:

file:./config/
file:./
classpath:/config/
classpath:/
參考
https://www.cnblogs.com/hellokuangshen/p/12457000.html