此文做記錄交流,如有不當,還望指正。
application.properties文件
SpringBoot項目系統的默認配置文件名為application.properties或者application.yml
配置文件可以放在以下4個位置,并具有優先順序
1,在相對于應用程序運行目錄的/congfig子目錄里。
2,在應用程序運行的目錄里
3,在config包內
4,在Classpath根目錄
src/main/resources/config下application.properties覆蓋src/main/resources下application.properties中相同的屬性,如下圖
我們可以在application.properties修改SpringBoot的默認配置
server.port=8000 #修改啟動端口號
server.name=helloword #項目名稱
當然也可以自定義屬性
my.sex=1
my.name=張三
my.age=55
具體配置可參考
SpringBoot application.properties全部配置一
SpringBoot application.properties全部配置二
程序中讀取配置文件的幾種方式
@Value("${my.sex}") 注解方式讀取 代碼如下
package com.demo.springboot_helloword.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class PropertiesUtil {
@Value("${my.age}")
private int age;
@Value("${my.name}")
private String name;
@Value("${my.sex}")
private int sex;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
}
@ConfigurationProperties(prefix = "my")注解方式把配置文件中的屬性賦值給實體bean 代碼如下
package com.demo.springboot_helloword.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="my")
public class PropertiesUtil {
private int age;
private String name;
private int sex;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
}
讀取自定義的配置文件。在resource下面創建my.properties
package com.demo.springboot_helloword.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:my.properties")
@ConfigurationProperties(prefix="my")
public class PropertiesUtil {
private int age;
private String name;
private int sex;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
}
application.properties中的參數互相引用及使用隨機數
my.secret=${random.value}
my.bignumber=${random.long}
my.number.in.range=${random.int[1024,65536]}
my.age=${random.int(10)}
my.name=zhangsan
my.describe=我的名字叫${my.name},我今年${my.age}歲了
多個環境的配置文件
在我們開發和測試及線上生產環境中所對應的配置往往是不同的,我們就需要在不同的地方使用不同的文件,假設我們在resources下面創建以下三個文件
application-test1.properties:測試環境 其中配置 server.port=8000
application-test2.properties:開發環境 其中配置 server.port=8001
application-test3.properties:生產環境 其中配置 server.port=8002
然后在application.properties中添加
spring.profiles.active=test1
啟動項目測試,我們會發現項目啟動后端口為8000
spring.profiles.active=${activestart:test1}
上面的寫法表示如果我們給傳遞 activestart 參數值 則spring.profiles.active的值為我們傳遞的值,否則默認為test1
項目在啟動的時候傳遞參數
java -jar xxx.jar --activestart=test2
項目就以application-test2.properties為配置啟動了