Spring Boot application .yml引數配置
阿新 • • 發佈:2019-01-03
配置檔案application .yml
YAML 是專門用來寫配置檔案的語言。springboot中可新增如下配置檔案
application.yml
application-dev.yml
application-pro.yml
注意:冒號後面要加空格,這樣spring boot 才能識別
系統環境變數裡如果有配置屬性PROFILE,會讀取環境變數的值
spring: profiles: active: ${PROFILE:dev} http: encoding: force: true server: port: 8080 management: security: enabled: false #關掉安全認證 port: 8088 #管理埠調整成8088 context-path: /monitor #actuator的訪問路徑 endpoints: shutdown: enabled: true
application-dev.yml
product:
name: stark
nickName: s
在類中可用如下方式讀取配置值
@Data
@Component
public class Product{
@Value("${product.name}")
private String name;
@Value("${product.nickName}")
private String nickName;
}
使用Product物件需要使用@Autowired、@Resource引入
通過命令列配置屬性
命令:java -jar xxx.jar --server.port=8888,通過使用–server.port屬性來設定xxx.jar應用的埠為8888
兩個減號–就是對application.properties中的屬性值進行賦值的標識
安全起見,Spring Boot提供了遮蔽命令列訪問屬性的設定,只需要這句設定就能遮蔽:SpringApplication.setAddCommandLineProperties(false)。
配置優先順序
- 命令列引數
- 來自java:comp/env的JNDI屬性
- Java系統屬性(System.getProperties())
- 作業系統環境變數
- RandomValuePropertySource配置的random.*屬性值
- jar包外部的application-{profile}.properties或application.yml(帶spring.profile)配置檔案
- jar包內部的application-{profile}.properties或application.yml(帶spring.profile)配置檔案
- jar包外部的application.properties或application.yml(不帶spring.profile)配置檔案
- jar包內部的application.properties或application.yml(不帶spring.profile)配置檔案
- @Configuration註解類上的@PropertySource
- 通過SpringApplication.setDefaultProperties指定的預設屬性