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
/** ?* 〈一句話功能簡述〉<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
/** ?* 〈一句話功能簡述〉<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ù)(親測可用)
dict: ? js: ? ? url: D:\jsFile\
首先自定義一個參數(shù)
@Component @Data @ConfigurationProperties(prefix = "dict.js") @PropertySource(value = "classpath:application-dev.yml") public class PropertisParam { ? ? private String url; }
利用平時@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; }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解用JWT對SpringCloud進行認證和鑒權(quán)
這篇文章主要介紹了詳解用JWT對SpringCloud進行認證和鑒權(quán),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03Java客戶端調(diào)用.NET的WebService實例
下面小編就為大家?guī)硪黄狫ava客戶端調(diào)用.NET的WebService實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09Java 基礎(chǔ) byte[]與各種數(shù)據(jù)類型互相轉(zhuǎn)換的簡單示例
這篇文章主要介紹了Java 基礎(chǔ) byte[]與各種數(shù)據(jù)類型互相轉(zhuǎn)換的簡單示例的相關(guān)資料,這里對byte[]類型對long,int,double,float,short,cahr,object,string類型相互轉(zhuǎn)換的實例,需要的朋友可以參考下2017-01-01利用Java的MyBatis框架獲取MySQL中插入記錄時的自增主鍵
這篇文章主要介紹了利用Java的MyBatis框架獲取MySQL中插入記錄的自增長字段值,其中大家可以看到MyBatis支持普通SQL語句所帶來的遍歷,需要的朋友可以參考下2016-06-06淺談BeanPostProcessor加載次序及其對Bean造成的影響分析
這篇文章主要介紹了淺談BeanPostProcessor加載次序及其對Bean造成的影響分析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04