SpringBoot讀取properties配置文件中的數(shù)據(jù)的三種方法
Spring Boot最常用的3種讀取properties配置文件中數(shù)據(jù)的方法:
1、使用@Value注解讀取
讀取properties配置文件時(shí),默認(rèn)讀取的是application.properties。
application.properties:
demo.name=Name demo.age=18
Java代碼:
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class GatewayController { @Value("${demo.name}") private String name; @Value("${demo.age}") private String age; @RequestMapping(value = "/gateway") public String gateway() { return "get properties value by ''@Value'' :" + //1、使用@Value注解讀取 " name=" + name + " , age=" + age; } }
運(yùn)行結(jié)果如下:
這里,如果要把
@Value("${demo.name}") private String name; @Value("${demo.age}") private String age;
部分放到一個(gè)單獨(dú)的類A中進(jìn)行讀取,然后在類B中調(diào)用,則要把類A增加@Component注解,并在類B中使用@Autowired自動(dòng)裝配類A,代碼如下。
類A:
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ConfigBeanValue { @Value("${demo.name}") public String name; @Value("${demo.age}") public String age; }
類B:
import cn.wbnull.springbootdemo.config.ConfigBeanValue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class GatewayController { @Autowired private ConfigBeanValue configBeanValue; @RequestMapping(value = "/gateway") public String gateway() { return "get properties value by ''@Value'' :" + //1、使用@Value注解讀取 " name=" + configBeanValue.name + " , age=" + configBeanValue.age; } }
運(yùn)行結(jié)果如下:
?注意:如果@Value${}所包含的鍵名在application.properties配置文件中不存在的話,會拋出異常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configBeanValue': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'demo.name' in value "${demo.name}"
2、使用Environment讀取
application.properties:
demo.sex=男 demo.address=山東
Java代碼:
import cn.wbnull.springbootdemo.config.ConfigBeanValue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class GatewayController { @Autowired private ConfigBeanValue configBeanValue; @Autowired private Environment environment; @RequestMapping(value = "/gateway") public String gateway() { return "get properties value by ''@Value'' :" + //1、使用@Value注解讀取 " name=" + configBeanValue.name + " , age=" + configBeanValue.age + "<p>get properties value by ''Environment'' :" + //2、使用Environment讀取 " , sex=" + environment.getProperty("demo.sex") + " , address=" + environment.getProperty("demo.address"); } }
運(yùn)行,發(fā)現(xiàn)中文亂碼:
?這里,我們在application.properties做如下配置:
server.tomcat.uri-encoding=UTF-8 spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true spring.messages.encoding=UTF-8
然后修改IntelliJ IDEA,F(xiàn)ile --> Settings --> Editor --> File Encodings ,將最下方Default encoding for properties files設(shè)置為UTF-8,并勾選Transparent native-to-ascii conversion。
?重新運(yùn)行結(jié)果如下:
3、使用@ConfigurationProperties注解讀取
在實(shí)際項(xiàng)目中,當(dāng)項(xiàng)目需要注入的變量值很多時(shí),上述所述的兩種方法工作量會變得比較大,這時(shí)候我們通常使用基于類型安全的配置方式,將properties屬性和一個(gè)Bean關(guān)聯(lián)在一起,即使用注解@ConfigurationProperties讀取配置文件數(shù)據(jù)。
在src\main\resources下新建config.properties配置文件:
demo.phone=10086 demo.wife=self
創(chuàng)建ConfigBeanProp并注入config.properties中的值:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "demo") @PropertySource(value = "config.properties") public class ConfigBeanProp { private String phone; private String wife; public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getWife() { return wife; } public void setWife(String wife) { this.wife = wife; } }
@Component 表示將該類標(biāo)識為Bean
@ConfigurationProperties(prefix = "demo")用于綁定屬性,其中prefix表示所綁定的屬性的前綴。
@PropertySource(value = "config.properties")表示配置文件路徑。
使用時(shí),先使用@Autowired自動(dòng)裝載ConfigBeanProp,然后再進(jìn)行取值,示例如下:
import cn.wbnull.springbootdemo.config.ConfigBeanProp; import cn.wbnull.springbootdemo.config.ConfigBeanValue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class GatewayController { @Autowired private ConfigBeanValue configBeanValue; @Autowired private Environment environment; @Autowired private ConfigBeanProp configBeanProp; @RequestMapping(value = "/gateway") public String gateway() { return "get properties value by ''@Value'' :" + //1、使用@Value注解讀取 " name=" + configBeanValue.name + " , age=" + configBeanValue.age + "<p>get properties value by ''Environment'' :" + //2、使用Environment讀取 " sex=" + environment.getProperty("demo.sex") + " , address=" + environment.getProperty("demo.address") + "<p>get properties value by ''@ConfigurationProperties'' :" + //3、使用@ConfigurationProperties注解讀取 " phone=" + configBeanProp.getPhone() + " , wife=" + configBeanProp.getWife(); } }
運(yùn)行結(jié)果如下:
到此這篇關(guān)于SpringBoot讀取properties配置文件中的數(shù)據(jù)的三種方法的文章就介紹到這了,更多相關(guān)SpringBoot properties讀取配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問題
本文主要介紹了詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04深度解析Java中的國際化底層類ResourceBundle
做項(xiàng)目應(yīng)該都會實(shí)現(xiàn)國際化,那么大家知道Java底層是如何實(shí)現(xiàn)國際化的嗎?這篇文章就來和大家深度解析一下Java中的國際化底層類ResourceBundle,希望對大家有所幫助2023-03-03spring boot2.0圖片上傳至本地或服務(wù)器并配置虛擬路徑的方法
最近寫了關(guān)于圖片上傳至本地文件夾或服務(wù)器,上傳路徑到數(shù)據(jù)庫,并在上傳時(shí)預(yù)覽圖片。本文通過實(shí)例代碼給大家分享spring boot2.0圖片上傳至本地或服務(wù)器并配置虛擬路徑的方法,需要的朋友參考下2018-12-12Java 不使用第三方變量交換兩個(gè)變量值的四種方法詳解
這篇文章主要介紹了四種不使用第三方變量交換兩個(gè)變量值的方法。文中對于四種方法進(jìn)行了詳細(xì)的分析,需要的小伙伴們可以跟隨小編一起學(xué)習(xí)一下2021-12-12Spring Boot利用@Async異步調(diào)用:使用Future及定義超時(shí)詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot利用@Async異步調(diào)用:使用Future及定義超時(shí)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2018-05-05