關(guān)于springboot中的自定義配置項
自定義配置項
在項目開發(fā)的過程中,經(jīng)常需要自定義系統(tǒng)業(yè)務(wù)方面的配置文件及配置項,Spring Boot提供了@value注解、@ConfigurationProperties注解和Environment接口等3種方式自定義配置項。
@value
在實際項目中,經(jīng)常需要在配置文件中定義一些簡單的配置項,Spring Boot提供@Value注解來設(shè)置簡單的配置項,默認(rèn)讀取application.properties文件中的配置屬性。
我們在application.properties配置文件下自定義配置屬性。

然后在使用的位置調(diào)用@Value注解來獲取配置項的值,如下所示:
@Value("${com.jingt.name.firstName}")
private String firstName;
@Value("${com.jingt.name.secondName}")
private String secondName;
public String testValue(){
return firstName + "|" + secondName;
}@Autowired
private HelloServices helloServices;
@Test
void testValue() {
System.out.println(helloServices.testValue());
}
注意:
- 使用@Value注解時,所在類必須被Spring容器管理,也就是使用
@Component、@Controller、@Service等注解定義的類。 @Value需要傳入完整的配置項的Key值。@Value注解默認(rèn)讀取application配置文件,如果需要使用其他的配置文件,可以通過@PropertySource注解指定對應(yīng)的配置文件。
在啟動類上加注解@PropertySource
@SpringBootApplication
@PropertySource(value = {"classpath:application_test.properties"})
public class HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
}在application_test.properties 配置文件中加入
com.jingt.name.firstName=guo1 com.jingt.name.thirdName=jingt1
@Value("${com.jingt.name.firstName}")
private String firstName;
@Value("${com.jingt.name.thirdName}")
private String secondName;
public String testValue(){
return firstName + "|" + secondName;
}把secondName改為thirdName,firstName從guo改為guo1

會發(fā)現(xiàn)thirdName也已經(jīng)獲取到了,firstName還是為guo,這是因為application.properties配置文件加載的優(yōu)先級的原因,application.properties會覆蓋application_test.properties對應(yīng)文件中的值。
Environment接口
Environment是Spring為運行環(huán)境提供的高度抽象的接口,它會自動獲取系統(tǒng)加載的全部配置項,包括命令行參數(shù),系統(tǒng)屬性,系統(tǒng)環(huán)境,隨機(jī)數(shù),配置文件等。使用時無須其他的額外配置,只要在使用的類中注入Environment即可。
在application.properties文件中增加自定義配置項
com.jingt.name.firstName=guo com.jingt.name.secondName=jingt
Environment讀取的是系統(tǒng)中所有的配置。我們既可以在application.properties中設(shè)置自定義的配置項,又可以在自定義配置文件中添加配置項。
將Environment對象注入,獲取系統(tǒng)配置
@Autowired
private Environment env;
@Test
void getEnv(){
System.out.println(env.getProperty("com.jingt.name.firstName"));
System.out.println(env.getProperty("com.jingt.name.secondName"));
}引入的是import org.springframework.core.env.Environment;,不要引入錯了。

使用Environment時還需要注意以下兩點:
使用Environment無須指定配置文件,其獲取的是系統(tǒng)加載的全部配置文件中的配置項。需要注意配置文件的編碼格式,默認(rèn)為ISO8859-1。
@ConfigurationProperties
在實際項目開發(fā)中,需要注入的配置項非常多時,@value和Environment兩種方法就會比較煩瑣。這時可以使用注解@ConfigurationProperties將配置項和實體Bean關(guān)聯(lián)起來,實現(xiàn)配置項和實體類字段的關(guān)聯(lián),讀取配置文件數(shù)據(jù)。
在resources下創(chuàng)建自定義的website.properties配置文件,增加配置屬性。
com.jingt.resource.name=jingt com.jingt.resource.website=com.jingt.com com.jingt.resource.language=java
創(chuàng)建一個配置類

@Configuration
@ConfigurationProperties(prefix = "com.jingt.resource")
@PropertySource(value = "classpath:website.properties")
public class ResourceConfig {
private String name;
private String website;
private String language;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}我們使用了@Configuration注解、@ConfigurationProperties和@PropertySource三個注解來定義WebSiteProperties實體類:
- @Configuration定義此類為配置類,用于構(gòu)建bean定義并初始化到Spring容器。
- @ConfigurationProperties(prefix = “com.weiz.resource”)綁定配置項,其中prefix表示所綁定的配置項名的前綴。
- @PropertySource(value = “classpath:website.properties”)指定讀取的配置文件及其路徑。@PropertySource不支持引入YML文件。
通過上面的WebSiteProperties類即可讀取全部對應(yīng)的配置項。
在單元測試中,我們測試是否可以通過
@Autowired
private ResourceConfig config;
@Test
void getResourceConfig(){
System.out.println(config.getName());
System.out.println(config.getWebsite());
System.out.println(config.getLanguage());
}
- 使用YML文件時注意空格和格式縮進(jìn)。
- Properties文件默認(rèn)使用的是ISO8859-1編碼格式,容易出現(xiàn)亂碼問題。如果含有中文,加入spring.http.encoding.charset=UTF-8配置即可。
- Properties配置的優(yōu)先級高于YML文件。因為YML文件的加載順序先于Properties文件,如果兩個文件存在相同的配置,后面加載的Properties中的配置會覆蓋前面YML中的配置。
- @PropertySource注解默認(rèn)只會加載Properties文件,YML文件不能使用此注解。
- 簡單值推薦使用@Value,復(fù)雜對象推薦使用@ConfigurationProperties。
- 只有Spring容器中的組件才能使用容器提供的各類方法,所以,配置讀取類需要增加@Component注解才能加入Spring容器中。
到此這篇關(guān)于關(guān)于springboot中的自定義配置項的文章就介紹到這了,更多相關(guān)springboot自定義配置項內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jmeter接口測試教程及接口測試流程詳解(全網(wǎng)僅有)
Jmeter是由Apache公司開發(fā)的一個純Java的開源項目,即可以用于做接口測試也可以用于做性能測試。本文給大家分享jmeter接口測試教程及接口測試流程,感興趣的朋友跟隨小編一起看看吧2021-12-12
java導(dǎo)出數(shù)據(jù)庫的全部表到excel
這篇文章主要為大家詳細(xì)介紹了java導(dǎo)出數(shù)據(jù)庫的全部表到excel的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03

