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

springboot如何讀取application.yml文件

 更新時(shí)間:2020年12月10日 09:26:39   作者:迷茫中守候  
這篇文章主要介紹了springboot如何讀取application.yml文件的方法,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下

現(xiàn)在開(kāi)發(fā)主要使用微服務(wù)框架springboot,在springboot中經(jīng)常遇到讀取application.yml文件的情形。

一、概述

開(kāi)發(fā)過(guò)程中經(jīng)常遇到要讀取application.yml文件中的屬性值,本文總結(jié)幾種讀取的方式,供參考。

二、詳述

我這里使用的是springboot-2.1.2.RELEASE版本,這里使用的是application.properties的配置方式,和使用application.yml的方式是一樣的。下面是application.properties文件的內(nèi)容

cn.com.my.test1=test1
cn.com.my.test2=test2

1、@Value注解

 這種方式是spring最早提供的方式,通過(guò)@Value注解的方式,該注解用在屬性上,但是要求該屬性所在的類必須要被spring管理。

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

  @Value("${cn.com.my.test1}")
  private String test1;
  @Value("${cn.com.my.test2}")
  private String test2;

  @RequestMapping("/test1/test")
  @ResponseBody
  public String getTest(){
    return "hello:"+test1+",test2:"+test2;
  }

}

在標(biāo)記有@Controller類中使用了帶有@Value注解的test1和test2的屬性,首先標(biāo)記有@Controller注解便可以使該類被spring管理。其次,使用@Value標(biāo)記了屬性,則可以獲得application.properties(application.yml)文件中的屬性,這里使用${cn.com.my.test1},屬性的名稱必須是全部的名稱,測(cè)試結(jié)果如下,

2、@ConfigurationProperties

@ConfigurationProperties注解是springboot提供的,在springboot中大量使用,下面看其用法,

使用@Component注解

這里需要定義一個(gè)類,

package com.example.demo.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "cn.com.my")
public class ApplicationPro {
  private String test1;
  private String test2;  private String testName;

  //必須有set方法
  public void setTest1(String test1) {
    this.test1 = test1;
  }

  //必須有set方法
  public void setTest2(String test2) {
    this.test2 = test2;
  }

  public String getTest1() {
    return test1;
  }

  public String getTest2() {
    return test2;
  }
public void setTestName(String testName) {  this.testName = testName;}public String getTestName() {  return testName;}
}

該類上使用了@ConfigurationProperties注解,且配置了prefix屬性,指定了要獲取屬性的前綴,這里的前綴是cn.com.my,在類中定義的屬性名最好和application.properties文件中的一致,不過(guò)這種方式可以采用稀疏匹配,把a(bǔ)pplication.properties修改為下面的內(nèi)容,

cn.com.my.test1=test1
cn.com.my.test2=test2
cn.com.my.test-name="hello world"

另外,在ApplicationPro類上標(biāo)記有@Component注解,標(biāo)記該注解的意思是要把該類交給spring管理,也就是說(shuō)要讓spring管理此類,其實(shí)也可以使用其他注解,如,@Service等

下面看測(cè)試類,

package com.example.demo.controller;

import com.example.demo.properties.ApplicationPro;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController3 {
  @Autowired
  private ApplicationPro ap;
  @RequestMapping("test3/test")
  @ResponseBody
  public String getTest(){
    return ap.getTest1()+","+ap.getTest2()+","+ap.getTestName();
  }
}

看測(cè)試結(jié)果,

從上面的結(jié)果可以看出已經(jīng)獲得了application.properties文件中的值,并且獲得了test-name的值。具體匹配規(guī)則可以自行百度,這里強(qiáng)烈建議配置文件中的屬性和類中的保持一致。

使用@EnableConfigurationProperties注解
使用該注解在ApplicationPro類中便不需要使用@Component注解,

package com.example.demo.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

//@Component
@ConfigurationProperties(prefix = "cn.com.my")
public class ApplicationPro {
  private String test1;
  private String test2;
  private String testName;

  //必須有set方法
  public void setTest1(String test1) {
    this.test1 = test1;
  }

  //必須有set方法
  public void setTest2(String test2) {
    this.test2 = test2;
  }

  public String getTest1() {
    return test1;
  }

  public String getTest2() {
    return test2;
  }

  public void setTestName(String testName) {
    this.testName = testName;
  }

  public String getTestName() {
    return testName;
  }
}

再看啟動(dòng)類,在啟動(dòng)類上標(biāo)記了@EnableConfigurationProperties({ApplicationPro.class}),也就是使@ConfigurationProperties注解生效,并標(biāo)記了標(biāo)有@ConfigurationProperties注解的類Application.class

package com.example.demo;

import com.example.demo.properties.ApplicationPro;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties({ApplicationPro.class})
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

}

下面看測(cè)試結(jié)果,

3、Environment對(duì)象

使用Environment對(duì)象,該對(duì)象是spring提供的一個(gè)對(duì)象,且是spring內(nèi)部創(chuàng)建的對(duì)象,

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController2 {
  @Autowired
  private Environment environment;

  @RequestMapping("/test2/test")
  @ResponseBody
  public String getTest(){
    return "hello,"+environment.getProperty("cn.com.my.test1")+","+"test2:"+environment.getProperty("cn.com.my.test2");
  }
}

可以看到,可以直接注入該對(duì)象的實(shí)例,通過(guò)其getProperty方法獲得相應(yīng)的屬性值。

三、總結(jié)

 本文總結(jié)了,在使用springboot的過(guò)程中獲取配置文件中的幾種方式,

@Value

@ConfigurationProperties

Environment對(duì)象

有不當(dāng)之處,歡迎指正,謝謝。

以上就是springboot如何讀取application.yml文件的詳細(xì)內(nèi)容,更多關(guān)于springboot 讀取application.yml文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java實(shí)現(xiàn)的KNN算法示例

    Java實(shí)現(xiàn)的KNN算法示例

    這篇文章主要介紹了Java實(shí)現(xiàn)的KNN算法,結(jié)合實(shí)例形式分析了KNN算法的原理及Java定義與使用KNN算法流程、訓(xùn)練數(shù)據(jù)相關(guān)操作技巧,需要的朋友可以參考下
    2018-06-06
  • java中Map如何根據(jù)key的大小進(jìn)行排序詳解

    java中Map如何根據(jù)key的大小進(jìn)行排序詳解

    這篇文章主要給大家介紹了關(guān)于java中Map如何根據(jù)key的大小進(jìn)行排序的相關(guān)資料,有時(shí)候我們業(yè)務(wù)上需要對(duì)map里面的值按照key的大小來(lái)進(jìn)行排序的時(shí)候我們就可以利用如下方法來(lái)進(jìn)行排序了,需要的朋友可以參考下
    2023-09-09
  • JAVA中常見(jiàn)異常類

    JAVA中常見(jiàn)異常類

    本文主要介紹了JAVA中的常見(jiàn)異常類。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-01-01
  • Required?request?body?is?missing的問(wèn)題及解決

    Required?request?body?is?missing的問(wèn)題及解決

    這篇文章主要介紹了Required?request?body?is?missing的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java中對(duì)list元素進(jìn)行排序的方法詳解

    Java中對(duì)list元素進(jìn)行排序的方法詳解

    這篇文章主要介紹了Java中對(duì)list元素進(jìn)行排序的方法詳解,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09
  • Kafka使用Java客戶端進(jìn)行訪問(wèn)的示例代碼

    Kafka使用Java客戶端進(jìn)行訪問(wèn)的示例代碼

    本篇文章主要介紹了Kafka使用Java客戶端進(jìn)行訪問(wèn)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • 使用IDEA配置Mybatis-Plus框架圖文詳解

    使用IDEA配置Mybatis-Plus框架圖文詳解

    這篇文章主要介紹了使用IDEA配置Mybatis-Plus框架,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • Java連接數(shù)據(jù)庫(kù),及增刪改查的示例

    Java連接數(shù)據(jù)庫(kù),及增刪改查的示例

    這篇文章主要介紹了Java連接數(shù)據(jù)庫(kù),及增刪改查的示例,幫助大家更好的利用Java處理數(shù)據(jù),感興趣的朋友可以了解下
    2020-10-10
  • mybatis 集合嵌套查詢和集合嵌套結(jié)果的區(qū)別說(shuō)明

    mybatis 集合嵌套查詢和集合嵌套結(jié)果的區(qū)別說(shuō)明

    這篇文章主要介紹了mybatis 集合嵌套查詢和集合嵌套結(jié)果的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java全能工具類之Hutool的用法詳解

    Java全能工具類之Hutool的用法詳解

    Hutool是一個(gè)Java工具類庫(kù),由國(guó)內(nèi)的程序員loolly開(kāi)發(fā),目的是提供一些方便、快捷、實(shí)用的工具類和工具方法,本文就來(lái)詳細(xì)聊聊它的使用吧
    2023-03-03

最新評(píng)論

沙湾县| 宜兴市| 鹤庆县| 长宁区| 任丘市| 宣化县| 通化县| 页游| 嘉义县| 增城市| 辽宁省| 黄龙县| 天津市| 邮箱| 随州市| 湟源县| 安化县| 博乐市| 化州市| 新宁县| 措勤县| 兴安盟| 平和县| 贡嘎县| 林口县| 金门县| 莱芜市| 东莞市| 富顺县| 德保县| 武汉市| 集贤县| 北安市| 萝北县| 武乡县| 搜索| 梨树县| 台州市| 台东市| 凌云县| 繁峙县|