標籤:blog code str 區別 files private poe conf ring
Spring Boot設定檔有兩種格式: application.properties 和 application.yml。兩種設定檔只需要使用一個。
這兩種設定檔的文法有些區別,如下
1. application.properties
server.port = 8080 -- tomcat 連接埠
server.context-path = /webName -- URL路徑
2. application.yml
server:
port: 8080 -- tomcat 連接埠,注意冒號後面有空格
context-path: /webName -- URL路徑,注意冒號後面有空格
一、Java類中使用配置
1. 方法一
@value("${server.port}")private String port;
2. 方法二
@Compoent@ConfigurationProperties(prefix="server")public class ServerProperties{ private String port; private String context-path; // set/get方法 }
注意:使用註解 @Compoent是為了方便在其他類中使用@Autowired引用該類
二、分環境使用設定檔
再建立兩個設定檔 application-dev.yml(測試環境設定檔) 和 application-prod.yml(正式環境設定檔)
在 application.yml 中配置如下:
spring: profiles: active: dev
註: 上面的配置是使用設定檔application-dev.yml,改成 active:prod即可使用設定檔application-prod.yml
三、java命令啟動使用配置
java -jar ****.jar --spring.profiles.active=dev
註:上面的配置是使用設定檔application-dev.yml,改成 --spring.profiles.active=prod即可使用設定檔application-prod.yml
Spring Boot學習——Spring Boot設定檔application