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)文章
詳解MyBatis resultType與resultMap中的幾種返回類型
本文主要介紹了MyBatis resultType與resultMap中的幾種返回類型,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Jmeter基于JDBC請(qǐng)求實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)測(cè)試
這篇文章主要介紹了Jmeter基于JDBC請(qǐng)求實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)測(cè)試,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
spring?eurake中使用IP注冊(cè)及問(wèn)題小結(jié)
在開(kāi)發(fā)spring?cloud的時(shí)候遇到一個(gè)很奇葩的問(wèn)題,就是服務(wù)向spring?eureka中注冊(cè)實(shí)例的時(shí)候使用的是機(jī)器名,然后出現(xiàn)localhost、xxx.xx等這樣的內(nèi)容,這篇文章主要介紹了spring?eurake中使用IP注冊(cè),需要的朋友可以參考下2023-07-07
spring如何快速穩(wěn)定解決循環(huán)依賴問(wèn)題
這篇文章主要介紹了spring如何快速穩(wěn)定解決循環(huán)依賴問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
JavaWeb利用struts實(shí)現(xiàn)文件下載時(shí)改變文件名稱
這篇文章主要為大家詳細(xì)介紹了JavaWeb利用struts實(shí)現(xiàn)文件下載時(shí)改變文件名稱的相關(guān)資料,需要的朋友可以參考下2016-06-06

