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配置文件中不存在的話,會(huì)拋出異常:
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)中文亂碼:

?這里,我們?cè)赼pplication.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í),上述所述的兩種方法工作量會(huì)變得比較大,這時(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)識(shí)為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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)飛機(jī)大戰(zhàn)案例詳解
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)飛機(jī)大戰(zhàn)案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
idea根據(jù)實(shí)體類生成數(shù)據(jù)庫(kù)表的流程步驟
在開發(fā)的時(shí)候,經(jīng)常會(huì)遇到數(shù)據(jù)庫(kù)表結(jié)構(gòu)設(shè)計(jì)無法滿足業(yè)務(wù)的需求,需要去改動(dòng)數(shù)據(jù)庫(kù)表,我們就需要去設(shè)計(jì)數(shù)據(jù)庫(kù)的字段,然后又回來增加實(shí)體類里的字段,這樣很麻煩,所以本文給大家介紹了idea根據(jù)實(shí)體類生成數(shù)據(jù)庫(kù)表的流程步驟,需要的朋友可以參考下2024-12-12
Mybatis-Plus字段策略FieldStrategy的使用
本文主要介紹了Mybatis-Plus字段策略FieldStrategy的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java中的RabbitMQ使用場(chǎng)景和實(shí)踐完全指南
本文涵蓋了RabbitMQ在Java中的主要使用場(chǎng)景和實(shí)踐方法,在實(shí)際應(yīng)用中,還需要根據(jù)具體的業(yè)務(wù)需求和系統(tǒng)架構(gòu)進(jìn)行適當(dāng)?shù)恼{(diào)整和優(yōu)化,本文結(jié)合代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2025-08-08
新建springboot項(xiàng)目時(shí),entityManagerFactory報(bào)錯(cuò)的解決
這篇文章主要介紹了新建springboot項(xiàng)目時(shí),entityManagerFactory報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Hadoop MultipleOutputs輸出到多個(gè)文件中的實(shí)現(xiàn)方法
這篇文章主要介紹了 Hadoop MultipleOutputs輸出到多個(gè)文件中的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10
Jenkins初級(jí)應(yīng)用之Invoke?Phing?targets插件配置
這篇文章主要為大家介紹了Jenkins初級(jí)應(yīng)用之Invoke?Phing?targets的插件配置,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪<BR>2022-04-04

