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

springboot加載復(fù)雜的yml文件獲取不到值的解決方案

 更新時(shí)間:2022年03月24日 09:24:23   作者:技術(shù)界小學(xué)生  
這篇文章主要介紹了springboot加載復(fù)雜的yml文件獲取不到值的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

springboot加載yml文件獲不到值

今天使用spring boot讀取yml文件,這種多層嵌套的竟然無(wú)法讀取到(value注解spring.redis.pool.max.wait),即便加上全名也不行,然后網(wǎng)上搜到的內(nèi)容也未曾滿意,很多文章內(nèi)容都是一樣且重復(fù)的.最后放棄了查找,突發(fā)奇想之下解決了這個(gè)問(wèn)題.

本文旨在如何讀取多層嵌套的yml文件,希望能幫到眾位.

以下是代碼:

package com.boot.config;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ConfigurationProperties(prefix = "spring.redis;pool.max;pool.min")
@PropertySource(value = "classpath:redis.yml")
public class RedisConfiguration implements ApplicationListener<ApplicationEvent> {
	@Value("${host}")
	private String host;
	@Value("${port}")
	private Long port;
	@Value("${timeout}")
	private Long timeout;
	@Value("${database}")
	private Long database;
	
	@Value("${wait}")
	private Long poolMaxWait;
	@Value("${idle}")
	private Long poolMaxIdle;
	@Value("${idle}")
	private Long poolMinIdle;
	@Value("${active}")
	private Long poolMaxActive;
	public void onApplicationEvent(ApplicationEvent event) {
        // 打印屬性
		System.out.println("============= redisConnect ================");
		System.out.println(this.toString());
    }
	@Override
    public String toString() {
	    return "RedisConfiguration [host=" + host + ", port=" + port + ", timeout=" + timeout
	            + ", database=" + database + ", poolMaxWait=" + poolMaxWait + ", poolMaxIdle="
	            + poolMaxIdle + ", poolMinIdle=" + poolMinIdle + ", poolMaxActive=" + poolMaxActive
	            + "]";
    }
}
#多層配置
spring:
	redis:
		database: 0
		host: localhost
		port: 6379
		timeout: 0
		pool:
		   max:
			  active: 8
			  wait: -1
			  idle: 8
		   min:
			  idle: 0

日志打印如下所示:

============= redisConnect ================
RedisConfiguration [host=localhost, port=6379, timeout=0, database=0, poolMaxWait=-1, poolMaxIdle=0, poolMinIdle=0, poolMaxActive=8]

獲取不到y(tǒng)ml配置文件指定的值

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication?
public class App {
? ? ?public static void main(String[] args) {
? ? ? ? SpringApplication app = new SpringApplication(App.class);
? ? ? ? ConfigurableApplicationContext context = app.run(args);
? ? ? ? System.out.println(context.getEnvironment().getProperty("jdbc.pwd")); ?
? ? ? ? context.close();
? ?}
}

apllication.yml 放置在classpath路徑下

jdbc:
?pwd: 123456 ?#冒號(hào)和數(shù)字之間有一個(gè)空格,沒(méi)有否則獲取失敗,pwd前面有縮進(jìn)兩個(gè)字符

ps:版本spring-4.3.2-release,springboot-1.4

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

绥芬河市| 贺州市| 桑植县| 玉环县| 改则县| 四子王旗| 隆安县| 拉萨市| 太原市| 从化市| 南川市| 贵阳市| 工布江达县| 深水埗区| 兴山县| 都昌县| 晋江市| 鄂托克旗| 磴口县| 库车县| 上蔡县| 正镶白旗| 六盘水市| 日喀则市| 营山县| 尚志市| 巩留县| 惠东县| 安阳县| 怀远县| 沙坪坝区| 周宁县| 冕宁县| 绥化市| 库尔勒市| 栾川县| 康乐县| 绥中县| 郴州市| 碌曲县| 安图县|