springboot加載復(fù)雜的yml文件獲取不到值的解決方案
springboot加載yml文件獲不到值
今天使用spring boot讀取yml文件,這種多層嵌套的竟然無(wú)法讀取到(value注解spring.redis.pool.max.wait),即便加上全名也不行,然后網(wǎng)上搜到的內(nèi)容也未曾滿意,很多文章內(nèi)容都是一樣且重復(fù)的.最后放棄了查找,突發(fā)奇想之下解決了這個(gè)問題.
本文旨在如何讀取多層嵌套的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è)空格,沒有否則獲取失敗,pwd前面有縮進(jìn)兩個(gè)字符
ps:版本spring-4.3.2-release,springboot-1.4
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)讀取帶合并單元格的Excel
這篇文章主要為大家詳細(xì)介紹了java如何實(shí)現(xiàn)讀取帶合并單元格的Excel,文中的示例代碼講解詳細(xì), 感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
SpringBoot執(zhí)行異步任務(wù)Async介紹
這篇文章主要為大家介紹了SpringBoot執(zhí)行異步任務(wù)Async示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Tomcat Cannot assign requested address: JVM_Bind 非端口占用沖突
這篇文章主要介紹了 Tomcat Cannot assign requested address: JVM_Bind 非端口占用沖突的相關(guān)資料,需要的朋友可以參考下2017-01-01
java對(duì)象對(duì)比之comparable和comparator的區(qū)別
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著comparable和comparator的區(qū)別展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
MyBatis-plus數(shù)據(jù)庫(kù)字段排序不準(zhǔn)確的解決
這篇文章主要介紹了MyBatis-plus數(shù)據(jù)庫(kù)字段排序不準(zhǔn)確的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring中的注解@Value("#{}")與@Value("${}")的區(qū)別
這篇文章主要介紹了Spring中的注解@Value(“#{}“)與@Value(“${}“)的區(qū)別到底是什么,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06

