Springboot如何獲取yml、properties參數(shù)
如何獲取yml、properties參數(shù)
1、使用@Value()注解
1.1 配置數(shù)據(jù)
如:在properties.yml文件配置如下數(shù)據(jù)
message_zh: 張三 message_en: ergouzi
在controller中獲?。?/p>
1.2 讀取數(shù)據(jù)
讀取自定義文件:須加注解
@PropertySource(value = {"classpath:config.yml","classpath:config.properties"})
讀取application文件不需要加注解
// 中文
@Value("${message_zh}")
private String message_zh;
// 英文
@Value("${message_en}")
private String message_en;
@RequestMapping(value = "/{id}")
public String index(HttpServletRequest request, @PathVariable Integer id){
? ? if (id == 1 ){
? ? ? ? request.setAttribute("info",message_zh);
? ? }else {
? ? ? ? request.setAttribute("info", message_en);
? ? }
? ? return "index";
}2、使用 @component
@ConfigurationProperties(prefix = "user") @PropertySource(value = "classpath:myConfig.properties")
首先在myConfig.properties或myConfig.yml中配置參數(shù):
user.userName = '李二狗' user.password = 'admin'
2.1 javabean
/**
?* 〈一句話功能簡(jiǎn)述〉<br>?
?* 〈yml或properties配置參數(shù)〉
?*
?* @author 丶Zh1Guo
?* @create 2018/11/21
?* @since 1.0.0
?*/
@Component // 組件
@ConfigurationProperties(prefix = "user") ? // 前綴
@PropertySource(value = "classpath:myConfig.properties") // 自定義配置文件路徑
public class properConfig {
? ? 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;
? ? }
}2.2 controller
/**
?* 〈一句話功能簡(jiǎn)述〉<br>?
?* 〈〉
?*
?* @author 丶Zh1Guo
?* @create 2018/11/21
?* @since 1.0.0
?*/
@restController
public class template {
? ? @Autowired
? ? properConfig config;
? ? @RequestMapping(value = "/config")
? ? public String config(){
? ? ? ? return config.getUserName();
? ? }
}總結(jié):
第一種方法適合只取某些數(shù)據(jù)
第二種方法適合取所有數(shù)據(jù)
yml和properties區(qū)別
yml:key:(空格)value

properties: key = value

配置文件讀取yml自定義參數(shù)(親測(cè)可用)
dict: ? js: ? ? url: D:\jsFile\
首先自定義一個(gè)參數(shù)
@Component
@Data
@ConfigurationProperties(prefix = "dict.js")
@PropertySource(value = "classpath:application-dev.yml")
public class PropertisParam {
? ? private String url;
}利用平時(shí)@value 獲取值
然后在所需要的調(diào)用的配置類里面注入PropertisParam,利用@PostConstruct初始化值
@Resource
private PropertisParam param;
private static String root=null;
@PostConstruct
public void init(){
? ? root = param.getUrl();
}另一種方式
@Data
@Component
@ConfigurationProperties(prefix = "spring")
public class LoginBody {
? ? private String appid;
? ? private String apiCode;
? ? private String userName;
}基本寫法就不解釋了:主要講一哈注入方式
類上面添加@component
private static LoginBody loginBody;
@Resource
public void init(LoginBody loginBody) {
? ? SecurityUtil.loginBody = loginBody;
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解用JWT對(duì)SpringCloud進(jìn)行認(rèn)證和鑒權(quán)
這篇文章主要介紹了詳解用JWT對(duì)SpringCloud進(jìn)行認(rèn)證和鑒權(quán),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Java客戶端調(diào)用.NET的WebService實(shí)例
下面小編就為大家?guī)?lái)一篇Java客戶端調(diào)用.NET的WebService實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Java 基礎(chǔ) byte[]與各種數(shù)據(jù)類型互相轉(zhuǎn)換的簡(jiǎn)單示例
這篇文章主要介紹了Java 基礎(chǔ) byte[]與各種數(shù)據(jù)類型互相轉(zhuǎn)換的簡(jiǎn)單示例的相關(guān)資料,這里對(duì)byte[]類型對(duì)long,int,double,float,short,cahr,object,string類型相互轉(zhuǎn)換的實(shí)例,需要的朋友可以參考下2017-01-01
利用Java的MyBatis框架獲取MySQL中插入記錄時(shí)的自增主鍵
這篇文章主要介紹了利用Java的MyBatis框架獲取MySQL中插入記錄的自增長(zhǎng)字段值,其中大家可以看到MyBatis支持普通SQL語(yǔ)句所帶來(lái)的遍歷,需要的朋友可以參考下2016-06-06
Java中try catch的使用和如何拋出異常問(wèn)題
這篇文章主要介紹了Java中try catch的使用和如何拋出異常問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
淺談BeanPostProcessor加載次序及其對(duì)Bean造成的影響分析
這篇文章主要介紹了淺談BeanPostProcessor加載次序及其對(duì)Bean造成的影響分析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

