最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在SpringBoot 中從application.yml中獲取自定義常量方式

 更新時(shí)間:2020年04月26日 09:42:13   作者:--fox--  
這篇文章主要介紹了在SpringBoot 中從application.yml中獲取自定義常量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

要注意的地方是 application.yml 中不能用駝峰式寫(xiě)法(systemParams)要改成system-params

方法一:

引入依賴:

 <!-- 支持 @ConfigurationProperties 注解 -->
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-configuration-processor</artifactId>
 <optional>true</optional>
 </dependency>

配置文件(application.yml)中配置各個(gè)屬性的值:

myProps: #自定義的屬性和值 
 simpleProp: simplePropValue 
 arrayProps: 1,2,3,4,5 
 listProp1: 
 - name: abc 
  value: abcValue 
 - name: efg 
  value: efgValue 
 listProp2: 
 - config2Value1 
 - config2Vavlue2 
 mapProps: 
 key1: value1 
 key2: value2 

創(chuàng)建一個(gè)bean來(lái)接收配置信息:

@Component 
@ConfigurationProperties(prefix="myProps") //接收application.yml中的myProps下面的屬性 
public class MyProps { 
 private String simpleProp; 
 private String[] arrayProps; 
 private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1里面的屬性值 
 private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的屬性值 
 private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的屬性值 
 
 public String getSimpleProp() { 
  return simpleProp; 
 } 
 
 //String類型的一定需要setter來(lái)接收屬性值;maps, collections, 和 arrays 不需要 
 public void setSimpleProp(String simpleProp) { 
  this.simpleProp = simpleProp; 
 } 
 
 public List<Map<String, String>> getListProp1() { 
  return listProp1; 
 } 
 public List<String> getListProp2() { 
  return listProp2; 
 } 
 
 public String[] getArrayProps() { 
  return arrayProps; 
 } 
 
 public void setArrayProps(String[] arrayProps) { 
  this.arrayProps = arrayProps; 
 } 
 
 public Map<String, String> getMapProps() { 
  return mapProps; 
 } 
 
 public void setMapProps(Map<String, String> mapProps) { 
  this.mapProps = mapProps; 
 } 
} 

啟動(dòng)后,這個(gè)bean里面的屬性就會(huì)自動(dòng)接收配置的值了。

單元測(cè)試用例:

@Autowired 
 private MyProps myProps; 
 
 @Test 
 public void propsTest() throws JsonProcessingException { 
  System.out.println("simpleProp: " + myProps.getSimpleProp()); 
  System.out.println("arrayProps: " + objectMapper.writeValueAsString(myProps.getArrayProps())); 
  System.out.println("listProp1: " + objectMapper.writeValueAsString(myProps.getListProp1())); 
  System.out.println("listProp2: " + objectMapper.writeValueAsString(myProps.getListProp2())); 
  System.out.println("mapProps: " + objectMapper.writeValueAsString(myProps.getMapProps())); 
 } 

測(cè)試結(jié)果:

simpleProp: simplePropValue 
arrayProps: ["1","2","3","4","5"] 
listProp1: [{"name":"abc","value":"abcValue"},{"name":"efg","value":"efgValue"}] 
listProp2: ["config2Value1","config2Vavlue2"] 
mapProps: {"key1":"value1","key2":"value2"} 

方法二:

不用寫(xiě) set 方法,直接給注解。

前提是要裝一個(gè)插件:lombok 。 直接在插件中搜索到它,裝上就行了

直接獲取一個(gè)屬性

image:
 location: D:/images/
 /**
 * 在配置文件中配置的文件保存路徑
 */
 @Value("${image.location}")
 private String location;

以上這篇在SpringBoot 中從application.yml中獲取自定義常量方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • IDEA MyBatis Plugins自動(dòng)生成實(shí)體類和mapper.xml

    IDEA MyBatis Plugins自動(dòng)生成實(shí)體類和mapper.xml

    這篇文章主要介紹了IDEA MyBatis Plugins自動(dòng)生成實(shí)體類和mapper.xml,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • JAVASE系統(tǒng)實(shí)現(xiàn)抽卡功能

    JAVASE系統(tǒng)實(shí)現(xiàn)抽卡功能

    這篇文章主要為大家詳細(xì)介紹了JAVASE系統(tǒng)實(shí)現(xiàn)抽卡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Spring中OpenFeign的使用方法最佳實(shí)踐

    Spring中OpenFeign的使用方法最佳實(shí)踐

    這篇文章主要介紹了Spring中OpenFeign使用的相關(guān)資料,OpenFeign是一個(gè)聲明式的WebService客戶端,簡(jiǎn)化了微服務(wù)之間的調(diào)用,類似于Controller調(diào)用Service,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-02-02
  • java實(shí)現(xiàn)客戶端向服務(wù)器發(fā)送文件

    java實(shí)現(xiàn)客戶端向服務(wù)器發(fā)送文件

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)客戶端向服務(wù)器發(fā)送文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • idea安裝jerbel及文件上傳下載的實(shí)現(xiàn)示例

    idea安裝jerbel及文件上傳下載的實(shí)現(xiàn)示例

    JRebel是一個(gè)Java開(kāi)發(fā)工具,它是一款用于實(shí)時(shí)代碼重載的插件,本文主要介紹了idea安裝jerbel及文件上傳下載的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解下
    2023-09-09
  • java字節(jié)碼框架ASM的深入學(xué)習(xí)

    java字節(jié)碼框架ASM的深入學(xué)習(xí)

    這篇文章主要給大家介紹了java中字節(jié)碼框架ASM的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,有需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-01-01
  • 排查Failed?to?validate?connection?com.mysql.cj.jdbc.ConnectionImpl

    排查Failed?to?validate?connection?com.mysql.cj.jdbc.Connec

    這篇文章主要介紹了Failed?to?validate?connection?com.mysql.cj.jdbc.ConnectionImpl問(wèn)題排查,具有很好的參考價(jià)值,希望對(duì)大家有所幫助
    2023-02-02
  • idea 自動(dòng)生成類注釋和方法注釋的實(shí)現(xiàn)步驟

    idea 自動(dòng)生成類注釋和方法注釋的實(shí)現(xiàn)步驟

    這篇文章主要介紹了idea 自動(dòng)生成類注釋和方法注釋的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Spring實(shí)戰(zhàn)之ResourceLoader接口資源加載用法示例

    Spring實(shí)戰(zhàn)之ResourceLoader接口資源加載用法示例

    這篇文章主要介紹了Spring實(shí)戰(zhàn)之ResourceLoader接口資源加載用法,結(jié)合實(shí)例形式分析了Spring使用ResourceLoader接口加載資源的相關(guān)配置與使用技巧,需要的朋友可以參考下
    2020-01-01
  • MyBatis 接收數(shù)據(jù)庫(kù)中沒(méi)有的字段的解決

    MyBatis 接收數(shù)據(jù)庫(kù)中沒(méi)有的字段的解決

    這篇文章主要介紹了MyBatis 接收數(shù)據(jù)庫(kù)中沒(méi)有的字段的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評(píng)論

襄城县| 赤水市| 茌平县| 加查县| 莱阳市| 资源县| 丹寨县| 子洲县| 曲麻莱县| 永城市| 临泽县| 和田县| 林西县| 社会| 科技| 苍溪县| 凭祥市| 兰考县| SHOW| 青神县| 商城县| 台中县| 龙山县| 霍州市| 阳西县| 称多县| 宁安市| 台东县| 卫辉市| 辛集市| 沂南县| 嘉峪关市| 墨脱县| 永修县| 仁寿县| 阿坝| 葵青区| 湘阴县| 龙州县| 洞口县| 桃江县|