springboot項(xiàng)目中PropertySource如何讀取yaml配置文件
PropertySource如何讀取yaml配置文件
springboot項(xiàng)目中,當(dāng)我們使用@Value注解讀取配置屬性,默認(rèn)的配置文件是properties類型文件,如果一些配置來自yaml格式配置文件,那么就需要做一個(gè)配置。
PropertySource注解提供了factory屬性,可以設(shè)置yaml格式文件加載工廠類。
下面介紹如何自定義factory實(shí)現(xiàn)yaml配置文件加載。
項(xiàng)目準(zhǔn)備
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);
}
}這里為了配合項(xiàng)目配置,配置文件放在工程根目錄下的conf/test.yml
business: errorCode: 2 msg: success, data: hallo
啟動(dòng)項(xiàng)目,我們可以訪問地址http://localhost:8080/test/v1/status,可以看到,返回結(jié)果為配置文件中設(shè)置的值。

這里值得注意的是,@PropertySource在指定配置文件位置的時(shí)候,使用的是file,這種方式指定的路徑,是相對(duì)于項(xiàng)目根路徑來的,所以我們的配置文件沒有放在resources目錄下,
如果放在resources目錄下,要通過file訪問到,那么,位置需要設(shè)置成這樣:
@PropertySource(value={"file:src/main/resources/conf/test.yml"},factory=YamlPropertySourceFactory.class)這里還可以通過classpath的方式來指定,如果conf/test.yml放在了resources類路徑下。
就可以這樣設(shè)置:
@PropertySource(value = {"classpath:conf/test.yml"},factory = YamlPropertySourceFactory.class)注意兩種方式的區(qū)別,以及配置文件需要放置的位置。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot輕松實(shí)現(xiàn)ip解析(含源碼)
IP地址一般以數(shù)字形式表示,如192.168.0.1,IP解析是將這個(gè)數(shù)字IP轉(zhuǎn)換為包含地區(qū)、城市、運(yùn)營商等信息的字符串形式,如“廣東省深圳市 電信”,這樣更方便人理解和使用,本文給大家介紹了SpringBoot如何輕松實(shí)現(xiàn)ip解析,需要的朋友可以參考下2023-10-10
舉例講解Java中synchronized關(guān)鍵字的用法
這篇文章主要介紹了Java中synchronized關(guān)鍵字的用法,針對(duì)synchronized修飾方法的使用作出了簡(jiǎn)單講解和演示,需要的朋友可以參考下2016-04-04
Springboot中的@ConditionalOnBean注解詳細(xì)解讀
這篇文章主要介紹了Springboot中的@ConditionalOnBean注解詳細(xì)解讀,@ConditionalOnMissingBean注解兩個(gè)類,一個(gè)Computer類,一個(gè)配置類,想要完成;如果容器中沒有Computer類,就注入備用電腦Computer類,如果有Computer就不注入,需要的朋友可以參考下2023-11-11
Java實(shí)現(xiàn)公用實(shí)體類轉(zhuǎn)Tree結(jié)構(gòu)
這篇文章主要為大家介紹了一個(gè)Java工具類,可以實(shí)現(xiàn)Java公用實(shí)體類轉(zhuǎn)Tree結(jié)構(gòu),文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以參考一下2024-10-10
Java設(shè)計(jì)模式之代理模式詳細(xì)解讀
這篇文章主要介紹了Java設(shè)計(jì)模式的代理模式,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)Java設(shè)計(jì)模式的小伙伴有很大的幫助,感興趣的小伙伴可以參考一下2021-08-08
SpringBoot快速整合RabbitMq小案例(使用步驟)
這篇文章主要介紹了SpringBoot快速整合RabbitMq小案例,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
Java處理異常2種機(jī)制關(guān)鍵字區(qū)別解析
這篇文章主要介紹了java處理異常2種機(jī)制關(guān)鍵字區(qū)別解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01

