springboot配置檔案詳解
1:熱啟動外掛依賴
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-devtools</ artifactId >
</ dependency >
|
該外掛功能:是boot的一個熱部署工具,當我們修改了(類、屬性檔案、頁面等)時,會自動重新啟動應用(由於其採用的雙類載入器機制,這個啟動會非常快,比手動重啟快很多倍)
2:啟動入口程式的方式必須是runas----->springboot app
注意:如果在pom檔案新增依賴建議重新啟動工程
定義:在application.properties屬性配置檔案中定義屬性名和屬性值
springboot.pic.url=192.168.28.120
|
取值:在控制器中取值
@Value ( "${springboot.pic.url}" )
private String URL;
|
Springboot有很多預設配置參考springboot文件。
比如:編碼對中文支援很友好,統一utf-8
spring.messages.encoding=UTF-8 # Message bundles encoding. server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
spring.freemarker.charset=UTF-8 # Template encoding.
spring.http.encoding.charset=UTF-8 # Charset of HTTP requests and responses. Added to the "Content-Type"
|
比如:tomcat埠號
server.port=8080 # Server HTTP port. |
比如thymeleaf模板引擎的預設配置
#pojo中有date型別怎麼轉string型別就靠這兩個配置
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=Asia/Chongqing
|
1:重新新建一個random.properties屬性配置檔案,如下圖:
2:配置隨機數資訊如下:
# 隨機字串
com.springboot.value=${random.value}
# 隨機int
com.springboot.number=${random.int}
# 隨機long
com.springboot.bignumber=${random.long}
# 10以內的隨機數
com.springboot.ten=${random.int(10)}
# 10-20的隨機數
com.springboot.ten2twenty =${random.int[10,20]}
|
3:將屬性注入到一個Pojo實體類,這種注法,你發現並沒有使用@Value註解哦,怎麼做到的呢?
package com.springboot.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@PropertySource ( "classpath:config/random.properties" )
@ConfigurationProperties (prefix= "com.springboot" )
public class RandomProperties {
//隨機字串
private String value;
//隨機int
private int number;
//隨機long
private long bignumber;
//10以內的隨機數
private int ten;
//10-20的隨機數
private int ten2twenty;
public String getValue() {
return value;
}
public void setValue(String value) {
this .value = value;
}
public int getNumber() {
return number;
}
public void setNumber( int number) {
this .number = number;
}
public long getBignumber() {
return bignumber;
}
public void setBignumber( long bignumber) {
this .bignumber = bignumber;
}
public int getTen() {
return ten;
}
public void setTen( int ten) {
this .ten = ten;
}
public int getTen2twenty() {
return ten2twenty;
}
public void setTen2twenty( int ten2twenty) {
this .ten2twenty = ten2twenty;
}
}
|
4:Springboot的單元測試隨機數
package com.springboot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.springboot.pojo.RandomProperties;
@RunWith (SpringRunner. class )
@SpringBootTest
public class Springboot001ApplicationTests {
@Autowired
private RandomProperties properties;
@Test
public void contextLoads() {
System.out.println(properties.getBignumber());
System.out.println(properties.getNumber());
System.out.println(properties.getTen());
System.out.println(properties.getTen2twenty());
System.out.println(properties.getValue());
}
}
|
無需匯入相關jar因為在新建spring boot 專案時會自動引入snakeyaml,從而自動實現對yaml的支援
舉例子:
environments: dev: url: http://dev.bar.com name: Developer Setup prod: url: http://foo.bar.com name: My Cool App |
Would be transformed into these properties:
environments.dev.url=http://dev.bar.com environments.dev.name=Developer Setup environments.prod.url=http://foo.bar.com environments.prod.name=My Cool Ap |
注意:
1:一定要注意冒號後一定要加空格,要不然就無法生效
2:大小寫敏感
3:使用縮排表示層級關係
4:縮排時不允許使用Tab鍵,只允許使用空格。
6:縮排的空格數目不重要,只要相同層級的元素左側對齊即可
命令:java -jar xxx.jar --server.port=8888 通過使用–-server.port屬性來設定xxx.jar應用的埠為8888。 java -jar xxx.jar --server.port=8888命令,等價於我們在application.properties中新增屬性server.port=8888 |