關于springboot中的自定義配置項
自定義配置項
在項目開發(fā)的過程中,經常需要自定義系統(tǒng)業(yè)務方面的配置文件及配置項,Spring Boot提供了@value注解、@ConfigurationProperties注解和Environment接口等3種方式自定義配置項。
@value
在實際項目中,經常需要在配置文件中定義一些簡單的配置項,Spring Boot提供@Value注解來設置簡單的配置項,默認讀取application.properties文件中的配置屬性。
我們在application.properties
配置文件下自定義配置屬性。
然后在使用的位置調用@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
注解默認讀取application配置文件,如果需要使用其他的配置文件,可以通過@PropertySource
注解指定對應的配置文件。
在啟動類上加注解@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ā)現thirdName也已經獲取到了,firstName還是為guo,這是因為application.properties配置文件加載的優(yōu)先級的原因,application.properties會覆蓋application_test.properties對應文件中的值。
Environment接口
Environment是Spring為運行環(huán)境提供的高度抽象的接口,它會自動獲取系統(tǒng)加載的全部配置項,包括命令行參數,系統(tǒng)屬性,系統(tǒng)環(huán)境,隨機數,配置文件等。使用時無須其他的額外配置,只要在使用的類中注入Environment即可。
在application.properties文件中增加自定義配置項
com.jingt.name.firstName=guo com.jingt.name.secondName=jingt
Environment讀取的是系統(tǒng)中所有的配置。我們既可以在application.properties中設置自定義的配置項,又可以在自定義配置文件中添加配置項。
將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)加載的全部配置文件中的配置項。需要注意配置文件的編碼格式,默認為ISO8859-1。
@ConfigurationProperties
在實際項目開發(fā)中,需要注入的配置項非常多時,@value和Environment兩種方法就會比較煩瑣。這時可以使用注解@ConfigurationProperties將配置項和實體Bean關聯起來,實現配置項和實體類字段的關聯,讀取配置文件數據。
在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定義此類為配置類,用于構建bean定義并初始化到Spring容器。
- @ConfigurationProperties(prefix = “com.weiz.resource”)綁定配置項,其中prefix表示所綁定的配置項名的前綴。
- @PropertySource(value = “classpath:website.properties”)指定讀取的配置文件及其路徑。@PropertySource不支持引入YML文件。
通過上面的WebSiteProperties類即可讀取全部對應的配置項。
在單元測試中,我們測試是否可以通過
@Autowired private ResourceConfig config; @Test void getResourceConfig(){ System.out.println(config.getName()); System.out.println(config.getWebsite()); System.out.println(config.getLanguage()); }
- 使用YML文件時注意空格和格式縮進。
- Properties文件默認使用的是ISO8859-1編碼格式,容易出現亂碼問題。如果含有中文,加入spring.http.encoding.charset=UTF-8配置即可。
- Properties配置的優(yōu)先級高于YML文件。因為YML文件的加載順序先于Properties文件,如果兩個文件存在相同的配置,后面加載的Properties中的配置會覆蓋前面YML中的配置。
- @PropertySource注解默認只會加載Properties文件,YML文件不能使用此注解。
- 簡單值推薦使用@Value,復雜對象推薦使用@ConfigurationProperties。
- 只有Spring容器中的組件才能使用容器提供的各類方法,所以,配置讀取類需要增加@Component注解才能加入Spring容器中。
到此這篇關于關于springboot中的自定義配置項的文章就介紹到這了,更多相關springboot自定義配置項內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!