當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
SpringBoot 2.x yml 文件中自定义参数解析对象
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot 2.x yml 文件中自定义参数解析对象
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在SpringBoot中通過(guò)自定義參數(shù)可以實(shí)現(xiàn)很多重要的功能來(lái)達(dá)到解耦。
SpringBoot 自定義參數(shù)的類型有:
簡(jiǎn)單鍵值對(duì),自定義對(duì)象,數(shù)組,List,Map,List<Map>
簡(jiǎn)單的鍵值對(duì) 不帶[]的簡(jiǎn)單數(shù)組
1. yml中添加自定義參數(shù)
# yml 自定義參數(shù) cus:val: 雨澤bean:id: 2name: 雨昕arr1: 1, 3, 4arr2: [1, 3, 4]list:- lista- listbmap:a: mapab: mapblistmap:- a: ab: bc: c- d: de: ef: f二、使用注解 @Value 來(lái)獲取
獲取原則(參數(shù)較少):
2.1. 獲取yml自定義參數(shù)中的鍵值對(duì)
方式一:使用注解 @Value 來(lái)獲取
@Value("${cus.val}") private String cusVal;注意:在${}中不能有空格。2.2. 獲取yml自定義參數(shù)中的數(shù)組
方式一:使用注解 @Value 來(lái)獲取
@Value("${cus.arr1}") private String cusVal;注意:在${}中不能有空格。對(duì)于沒(méi)與使用”[]“括起來(lái)的數(shù)組可以使用方式一來(lái)獲取,而使用了”[]“括起來(lái)的數(shù)組只能用方式二獲取三、配置文件中其他獲取
配置文件中其他文件就不能使用簡(jiǎn)單的 @Value 注解來(lái)獲取,需要使用如下方式獲取。使用如下方式可獲取:1.獲取yml自定義參數(shù)中的鍵值對(duì),對(duì)應(yīng)上面配置文件的 val 2.獲取yml自定義參數(shù)中的數(shù)組,對(duì)應(yīng)上面配置文件的 arr1,arr2 3.獲取yml自定義參數(shù)中的對(duì)象,對(duì)應(yīng)上面配置文件的 bean 對(duì)象 4.獲取yml自定義參數(shù)中的List,對(duì)應(yīng)上面配置文件的 list 5.獲取yml自定義參數(shù)中Map<Object,Object>,對(duì)應(yīng)上面配置文件的 map 6.獲取yml自定義參數(shù)中List<Map<Object,Object>>,對(duì)應(yīng)上面配置文件的 listmap 第一步首先定義一個(gè)如下格式的類,具體格式: (1)其中的成員變量名稱需要與配置的自定義參數(shù)名稱一致 (2)每個(gè)成員變量必須要有 get/set 方法 (3)該類使用注解 @Component 和 @ConfigurationProperties 修飾 package com.gblfy.mall.config;import com.gblfy.mall.form.UserLoginForm; import com.gblfy.mall.pojo.User; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;import java.util.List; import java.util.Map;@Component @ConfigurationProperties(prefix = "cus") @Data public class YmlParamConfig {private String val;private UserLoginForm bean;private String[] arr1;private String[] arr2;private List<String> list;private Map<String, String> map;private List<Map<String, String>> listmap; }測(cè)試類:
package com.gblfy.mall.controller;import com.gblfy.mall.config.YmlParamConfig; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;@RestController @Slf4j public class YMLController {@Autowiredprivate YmlParamConfig ymlParamConfig;@Value("${cus.val}")private String cusVal;@Value("${cus.arr1}")private String cusArrl;@GetMapping("/getYmlByValue")public void getYmlByValue() {log.info("獲取yml自定義參數(shù)中的 簡(jiǎn)單鍵值對(duì) cusVal={}", cusVal);log.info("獲取yml自定義參數(shù)中的 不帶[]的數(shù)組 cusArrl={}", cusArrl);}@GetMapping("/getYmlByconfigFile")public void getYmlByconfigFile() {log.info("獲取yml自定義參數(shù)中的 鍵值對(duì)={}", ymlParamConfig.getVal());log.info("獲取yml自定義參數(shù)中的 數(shù)組1 ={}", ymlParamConfig.getArr1());log.info("獲取yml自定義參數(shù)中的 數(shù)組2 ={}", ymlParamConfig.getArr2());log.info("獲取yml自定義參數(shù)中的 對(duì)象 ={}", ymlParamConfig.getBean());log.info("獲取yml自定義參數(shù)中的 List ={}", ymlParamConfig.getList());log.info("獲取yml自定義參數(shù)中的 Map<Object,Object>", ymlParamConfig.getMap());log.info("獲取yml自定義參數(shù)中的 List<Map<Object,Object>>={}", ymlParamConfig.getListmap());} }日志輸出:
c.g.mall.controller.YMLController - 獲取yml自定義參數(shù)中的 簡(jiǎn)單鍵值對(duì) cusVal=雨澤 c.g.mall.controller.YMLController - 獲取yml自定義參數(shù)中的 不帶[]的數(shù)組 cusArrl=1, 3, 4 c.g.mall.controller.YMLController - 獲取yml自定義參數(shù)中的 鍵值對(duì)=雨澤 c.g.mall.controller.YMLController - 獲取yml自定義參數(shù)中的 數(shù)組1 =1 c.g.mall.controller.YMLController - 獲取yml自定義參數(shù)中的 數(shù)組2 =1 c.g.mall.controller.YMLController - 獲取yml自定義參數(shù)中的 對(duì)象 =User(id=2, username=null, password=null, email=null, phone=null, question=null, answer=null, role=null, createTime=null, updateTime=null) c.g.mall.controller.YMLController - 獲取yml自定義參數(shù)中的 List =[lista, listb] c.g.mall.controller.YMLController - 獲取yml自定義參數(shù)中的 Map<Object,Object> c.g.mall.controller.YMLController - 獲取yml自定義參數(shù)中的 List<Map<Object,Object>>=[{a=a, b=b, c=c}, {d=d, e=e, f=f}]擴(kuò)展:
#這種方式是:在配置文件中使用配置 #請(qǐng)求url攔截配置信息 excludeRegister: /user/register excludeLogin: /user/login requrl:pathPatterns: '/**'excludePathPatterns: ${excludeRegister},${excludeLogin}總結(jié)
以上是生活随笔為你收集整理的SpringBoot 2.x yml 文件中自定义参数解析对象的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Sublime Text设置快捷键让ht
- 下一篇: SpringBoot2整合Activit