SpringBoot讀取properties或者application.yml配置文件中的數(shù)據(jù)
讀取application文件
在application.yml或者properties文件中添加:
user.address=china user.company=demo user.name=讓我康康
1、使用@Value注解讀取
直接 代碼如下:
package im.homeapi.controller;
import org.springframework.beans.factory.annotation.Value;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
@RequestMapping(value="/api")
public class HomeController {
@Value("${user.address}")
private String address;
@Value("${user.company}")
private String company;
@Value("${user.name}")
private String name;
//value 指定訪問地址,method 指定請(qǐng)求類型
@RequestMapping(value = "/home",method = RequestMethod.GET)
public String Home()
{
return "Hello Word";
}
@RequestMapping(value = "/getConfig")
public String getConfig() {
return "獲取的配置信息 :" +
" name=" + name +
" address=" + address +
" , company=" + company;
}
}
放到單獨(dú)的配置類中讀?。?/p>
package im.homeapi.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class UserConfig {
@Value("${user.address}")
private String address;
@Value("${user.company}")
private String company;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Value("${user.name}")
private String name;
}
調(diào)用如下:
@Autowired
private UserConfig userConfig;
//讀取配置類
@RequestMapping(value = "/getConfigEntity")
public String getConfigEntity() {
return "獲取的配置信息 :" +
" name=" + userConfig.getName() +
" address=" + userConfig.getAddress() +
" , company=" + userConfig.getCompany();
}
運(yùn)行結(jié)果如下:

2、使用@ConfigurationProperties注解讀取方式
代碼如下:
package im.homeapi.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "user")
public class UserConfig1 {
private String address;
private String company;
private String name;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
調(diào)用:
package im.homeapi.controller;
import im.homeapi.entity.UserConfig;
import im.homeapi.entity.UserConfig1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
@RequestMapping(value="/api")
public class ConfigController {
@Autowired
private UserConfig1 userConfig;
//讀取配置類 ConfigurationProperties注解讀取方式
@RequestMapping(value = "/getConfigEntity1")
public String getConfigEntity() {
return "獲取的配置信息 :" +
" name=" + userConfig.getName() +
" address=" + userConfig.getAddress() +
" , company=" + userConfig.getCompany();
}
}
運(yùn)行結(jié)果:

3、讀取指定文件
3.1、@PropertySource+@Value注解讀取方式
在resources下新建配置config/db-config.properties
注意:@PropertySource不支持yml文件讀取。
db.username=root db.password=123456
如圖:

代碼:
package im.homeapi.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = { "config/db-config.properties" })
public class DBConfig {
@Value("${db.username}")
private String username;
@Value("${db.password}")
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
調(diào)用代碼:
package im.homeapi.controller;
import im.homeapi.entity.DBConfig;
import im.homeapi.entity.UserConfig;
import im.homeapi.entity.UserConfig1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
@RequestMapping(value="/api")
public class DbController {
@Autowired
private DBConfig dbConfig;
//讀取配置類 PropertySource+@Value注解讀取方式
@RequestMapping(value = "/getConfigdb")
public String getConfigdb() {
return "獲取的配置信息 :" +
" name=" + dbConfig.getUsername() +
" , password=" + dbConfig.getPassword();
}
}
運(yùn)行結(jié)果:

3.2、@PropertySource+@ConfigurationProperties注解讀取方式
代碼:
package im.homeapi.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "db")
@PropertySource(value = { "config/db-config.properties" })
public class DBconfig1 {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
調(diào)用代碼:
@Autowired
private DBconfig1 dbConfig1;
//讀取配置類 @PropertySource+@ConfigurationProperties注解讀取方式
@RequestMapping(value = "/getConfigdb1")
public String getConfigdb1() {
return "獲取的配置信息 :" +
" name=" + dbConfig1.getUsername() +
" , password=" + dbConfig1.getPassword();
}
運(yùn)行結(jié)果:

@Component 表示將該類標(biāo)識(shí)為Bean
@ConfigurationProperties(prefix = "db")用于綁定屬性,其中prefix表示所綁定的屬性的前綴。
@PropertySource(value = "config/db-config.properties")表示配置文件路徑。
4、使用Environment讀取
代碼:
@Autowired
private Environment environment;
//讀取配置類 CEnvironment讀取方式
@RequestMapping(value = "/getConfigenv")
public String getConfigenv() {
return "獲取的配置信息 :" +
" name=" + environment.getProperty("user.name") +
" address=" + environment.getProperty("user.address") +
" , company=" + environment.getProperty("user.company");
}
運(yùn)行結(jié)果:

總結(jié)
從以上示例來看,Spring Boot可以通過@PropertySource,@Value,@Environment,@ConfigurationProperties來綁定變量。
到此這篇關(guān)于SpringBoot讀取properties或者application.yml配置文件中的數(shù)據(jù)的文章就介紹到這了,更多相關(guān)SpringBoot讀取配置數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java遞歸調(diào)用如何實(shí)現(xiàn)數(shù)字的逆序輸出方式
這篇文章主要介紹了Java遞歸調(diào)用如何實(shí)現(xiàn)數(shù)字的逆序輸出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
java SpringBoot自定義注解,及自定義解析器實(shí)現(xiàn)對(duì)象自動(dòng)注入操作
這篇文章主要介紹了java SpringBoot自定義注解,及自定義解析器實(shí)現(xiàn)對(duì)象自動(dòng)注入操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Netty分布式Server啟動(dòng)流程服務(wù)端初始化源碼分析
本章主要講解server啟動(dòng)的關(guān)鍵步驟,?讀者只需要了解server啟動(dòng)的大概邏輯,?知道關(guān)鍵的步驟在哪個(gè)類執(zhí)行即可,?并不需要了解每一步的運(yùn)作機(jī)制,?之后會(huì)對(duì)每個(gè)模塊進(jìn)行深度分析2022-03-03
Java實(shí)現(xiàn)多項(xiàng)式除法的代碼示例
今天小編就為大家分享一篇關(guān)于Java實(shí)現(xiàn)多項(xiàng)式除法的代碼示例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
Springboot的ThreadPoolTaskScheduler線程池輕松搞定15分鐘不操作自動(dòng)取消訂單
這篇文章主要介紹了Springboot的ThreadPoolTaskScheduler線程池輕松搞定15分鐘不操作自動(dòng)取消訂單,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01
java如何創(chuàng)建一個(gè)jdbc程序詳解
使用Java程序來操作數(shù)據(jù)庫,后者更加直接的話就是使用Java程序來發(fā)送SQL語句的技術(shù)稱之為:JDBC。下面這篇文章主要給大家介紹了關(guān)于利用java如何創(chuàng)建一個(gè)jdbc程序的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-11-11

