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

springboot項目中PropertySource如何讀取yaml配置文件

 更新時間:2024年01月13日 14:55:43   作者:luffy5459  
這篇文章主要介紹了springboot項目中PropertySource如何讀取yaml配置文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

PropertySource如何讀取yaml配置文件

springboot項目中,當我們使用@Value注解讀取配置屬性,默認的配置文件是properties類型文件,如果一些配置來自yaml格式配置文件,那么就需要做一個配置。

PropertySource注解提供了factory屬性,可以設置yaml格式文件加載工廠類。

下面介紹如何自定義factory實現(xiàn)yaml配置文件加載。

項目準備

maven工程pom.xml增加spring-boot-starter-web依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

自定義YamlPropertySourceFactory

package com.xxx.web.config;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.IOException;
import java.util.List;
 
public class YamlPropertySourceFactory implements PropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        List<PropertySource<?>> list = new YamlPropertySourceLoader().load(resource.getResource().getFilename(),resource.getResource());
        if(list != null && list.size() > 0)
            return list.get(0);
        return null;
    }
}

yml配置文件使用

package com.xxx.web.controller;
import com.xxx.web.config.YamlPropertySourceFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.HashMap;
import java.util.Map;
 
@RestController
@RequestMapping("/test/v1")
@PropertySource(value = {"file:conf/test.yml"},factory = YamlPropertySourceFactory.class)
public class TestController {
 
    @Value("${business.errorCode}")
    private int errorCode;
 
    @Value("${business.msg}")
    private String msg;
 
    @Value("${business.data}")
    private String data;
 
    @RequestMapping(value = "/status",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity queryStatus(){
        Map<String,Object> result = new HashMap(){
            {
                put("msg",msg);
                put("errorCode",errorCode);
                put("data",data);
            }
        };
        return ResponseEntity.ok(result);
    }
 
}

這里為了配合項目配置,配置文件放在工程根目錄下的conf/test.yml

business:
  errorCode: 2
  msg: success,
  data: hallo

啟動項目,我們可以訪問地址http://localhost:8080/test/v1/status,可以看到,返回結果為配置文件中設置的值。

這里值得注意的是,@PropertySource在指定配置文件位置的時候,使用的是file,這種方式指定的路徑,是相對于項目根路徑來的,所以我們的配置文件沒有放在resources目錄下,

如果放在resources目錄下,要通過file訪問到,那么,位置需要設置成這樣:

@PropertySource(value={"file:src/main/resources/conf/test.yml"},factory=YamlPropertySourceFactory.class)

這里還可以通過classpath的方式來指定,如果conf/test.yml放在了resources類路徑下。

就可以這樣設置:

@PropertySource(value = {"classpath:conf/test.yml"},factory = YamlPropertySourceFactory.class)

注意兩種方式的區(qū)別,以及配置文件需要放置的位置。 

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 利用Java Apache POI 生成Word文檔示例代碼

    利用Java Apache POI 生成Word文檔示例代碼

    本篇文章主要介紹了利用Java Apache POI 生成Word文檔示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Java Lambda表達式之從集合到流

    Java Lambda表達式之從集合到流

    這篇文章主要介紹了Java Lambda表達式之從集合到流知識,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-02-02
  • @Data注解在Boolean類型屬性上的大坑及解決

    @Data注解在Boolean類型屬性上的大坑及解決

    在使用@Data注解時,如果類中存在Boolean類型的屬性,且屬性名不是以"is"開頭,那么@Data注解生成的get方法名會默認加上"is",導致屬性值無法成功拷貝,解決方法是手動添加get方法,覆蓋@Data注解生成的方法
    2024-10-10
  • springboot配置log日志按天按類緩存實現(xiàn)過程

    springboot配置log日志按天按類緩存實現(xiàn)過程

    文章介紹了如何配置Spring Boot項目中的`application.yml`文件,并在同級目錄下創(chuàng)建`logback-spring.xml`文件以自定義日志設置,最后,展示了運行結果的圖示
    2026-01-01
  • springboot代碼,注解配置獲取yml,properties文件的map即鍵值對

    springboot代碼,注解配置獲取yml,properties文件的map即鍵值對

    這篇文章主要介紹了springboot代碼,注解配置獲取yml,properties文件的map即鍵值對,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • vue 使用vuex在頁面跳轉的實現(xiàn)方式

    vue 使用vuex在頁面跳轉的實現(xiàn)方式

    這篇文章主要介紹了vue 使用vuex在頁面跳轉的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • springsecurity自定義登錄頁面的實現(xiàn)示例

    springsecurity自定義登錄頁面的實現(xiàn)示例

    本文主要介紹了springsecurity自定義登錄頁面的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-09-09
  • SpringBoot項目速度提升之延遲初始化(Lazy Initialization)詳解

    SpringBoot項目速度提升之延遲初始化(Lazy Initialization)詳解

    延遲初始化(Lazy?Initialization)是一種在需要時才創(chuàng)建或加載對象的策略,以減少啟動時間和資源消耗,本文就來講講延遲初始化的具體使用吧
    2023-05-05
  • knife4j+springboot3.4異常無法正確展示文檔

    knife4j+springboot3.4異常無法正確展示文檔

    本文主要介紹了knife4j+springboot3.4異常無法正確展示文檔,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-01-01
  • Java @Transactional與synchronized使用的問題

    Java @Transactional與synchronized使用的問題

    這篇文章主要介紹了Java @Transactional與synchronized使用的問題,了解內部原理是為了幫助我們做擴展,同時也是驗證了一個人的學習能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會的
    2023-01-01

最新評論

凉城县| 襄汾县| 静乐县| 新兴县| 碌曲县| 云林县| 柘城县| 肇东市| 敖汉旗| 东乡族自治县| 紫阳县| 大港区| 乌兰察布市| 中卫市| 肇东市| 宁蒗| 华宁县| 邵东县| 淮北市| 台安县| 曲松县| 云龙县| 德化县| 栾川县| 房山区| 禹州市| 五常市| 德惠市| 灵武市| 井冈山市| 北票市| 阳春市| 吉木萨尔县| 武威市| 怀来县| 双流县| 高碑店市| 永川市| 巩留县| 通道| 尚义县|