文章目錄
1. properties 文件
對於 properties 文件的讀取,採用 @PropertySource 、@Value 註解即可直接獲取指定配置項。
1.1. @Value 方式
test-a.properties 文件:
test.a=測試A
test-b.properties 文件(用於測試從不同 properties 文件讀取):
test.b=測試B
1.1.1. 配置項讀取
@Data
@PropertySource(value = {"classpath:test-a.properties", "classpath:test-b.properties"}, encoding = "UTF-8")
@Component
public class PropertiesDemo {
/**
* test-a.properties 文件中 test.a 屬性
*/
@Value("${test.a}")
private String aValue;
/**
* test-b.properties 文件中 test.b 屬性
*/
@Value("${test.b}")
private String bValue;
}
@Data : 用於提供 getter,setter。簡化代碼
@PropertySource : 用於指定 properties 文件,需將 properties 文件放置在 能夠獲取到路徑的地方
@Component: 注入 bean ,不能省略,不然 @Value 註解不能獲取到配置項
測試:
注意點:單元測試讀取的是 test resources 下的配置文件
1.2. Environment 方式
test-a.properties
test.environment=Environment方式讀取配置文件
1.2.1. 配置項讀取
@Component
@PropertySource(value = {"classpath:test-a.properties"}, encoding = "UTF-8")
public class EnvironmentDemo {
@Autowired
private Environment environment;
public String getEnvironment() {
return environment.getProperty("test.environment");
}
}
代碼解釋:
@Component : 注入bean,不可缺少
@PropertySource : 指定配置文件,可指定編碼
Environment : 獲取配置文件屬性的接口
getProperty: 獲取配置屬性,參數爲配置屬性名
注意點:同樣的需要區分測試資源文件路徑
1.3. ConfigurationProperties 方式獲取
test-c.properties 文件:
id=1
name=測試C
1.3.1. 讀取配置
@Component
@ConfigurationProperties
@PropertySource(value = {"classpath:test-c.properties"}, encoding = "UTF-8")
@Data
public class PropertiesBean {
/**
* ID 對應配置屬性:id
*/
private int id;
/**
* name 對應配置屬性:name
*/
private String name;
}
@Component: 注入bean,不可少
@ConfigurationProperties: 進行獲取配置,對應屬性名稱(可指定配置前綴等參數)
@PropertySource: 指定文件名,文件編碼
@Data : 提供 getter ,setter,簡化代碼
小結:@Value 的方式獲取其實是這種方式的一種泛化方式。@Value 不需要屬性名稱、字段名稱一致,此種方式需要嚴格一致。
此種方式也適用於 yml 方式獲取。
2. yml 文件
前文所說的方法均適用於 yml 文件讀取配置屬性。
值得注意的是 @PropertySource 不支持配置 yml 文件,我們可以通過 application.yml 配置活躍 yml 文件進行讀取。
2.1. 獲取示例
application.yml
spring:
profiles:
active: bob
bob 表示活躍的配置文件
application-bob.yml
bob:
id: 2
name: bob姓名
application.yml , application-bob.yml 對應關係圖:
2.1.1. 獲取配置
@Component
@ConfigurationProperties(prefix = "bob")
@Data
public class YmlDemo {
/**
* 對應 配置文件 bob.id
*/
private int id;
/**
* 對應配置文件 bob.name
*/
private String name;
}
@Component : 注入 bean,同樣不能少
@ConfigurationProperties: 獲取配置屬性,對應上字段名稱。此處指定了 prefix(配置屬性前綴)
@Data : 提供 getter,setter,簡化代碼
注意:此種方式獲取,會延用項目編碼,不需要進行指定編碼。故需將 IDE 和項目編碼保持一致,避免中文亂碼。
3. 靜態字段賦值
細心的大佬可能已經發現,之前的方式全是普通的字段,並不是靜態字段。
3.1. 背景說明
Spring 不支持採用 @Value 的方式給靜態字段賦值。 意味着我們前邊的所有方式都不能使用。(直接使用不會報錯,但是獲取不到屬性值)
Spring 提供注入方式包括:setter注入、構造器注入等
3.2. 需求
我們需要項目啓動時,便給指定的靜態字段進行賦值。
3.3. 解決方案
SpringBoot 沒有提供 @Value 的方式讀取配置屬性給靜態變量賦值,但是靜態變量支持 setter 注入的方式。即通過 @Value 給 setter 注入參數值,再用 setter 給靜態變量賦值。即可實現需求。
properties 文件,延用之前的配置文件
3.3.1. 獲取配置
@PropertySource(value = {"classpath:test-a.properties", "classpath:test-b.properties"}, encoding = "UTF-8")
@Component
public class PropertiesStaticDemo {
/**
* test-a.properties 文件中 test.a 屬性
*/
public static String aValue;
/**
* test-b.properties 文件中 test.b 屬性
*/
public static String bValue;
/**
* 獲取配置文件項 test.a 給參數 aValue 賦值,然後通過方法體給 靜態屬性 PropertiesStaticDemo.aValue 賦值
*/
@Value("${test.a}")
public void setAValue(String aValue) {
PropertiesStaticDemo.aValue = aValue;
}
@Value("${test.b}")
public void setBValue(String bValue) {
PropertiesStaticDemo.bValue = bValue;
}
}
注:
a. 在全部修改爲 static 字段時,也需要保留 @Component 註解
b. @PropertySource 指定配置文件、文件編碼
3.3.2. 測試
@RunWith(SpringRunner.class)
@SpringBootTest
public class PropertiesStaticTest {
@Test
public void test() {
System.out.println("aValue:" + PropertiesStaticDemo.aValue);
System.out.println("bValue:" + PropertiesStaticDemo.bValue);
}
}
直接通過類名訪問。
4. 小結
properties、yml 兩個文件的讀取原理一樣的,只是對應的活躍文件指定方式略有區別。
對於靜態屬性的賦值,主要是採用 setter 的方式,巧妙的曲線救國。
知識有限,有錯有漏歡迎評論指正。謝謝。
5. 完整代碼
https://github.com/LamboChen/demo