SpringBoot经常读取配置文件

目前常见的配置文件有两种格式:properties和. yml。 这些差异主要是书写形式的差异

1 ) .属性

app.user.name=javastack

2 ).yml

app:

用户:

name : Java堆栈

此外, yml格式不支持@PropertySource注释的导入配置文件。

今天,我们主要介绍使用第三方工具读取配置文件

SnakeYaml是完整的YAML1.1规范处理器,支持UTF-8/UTF-16,支持Java对象序列化/序列化,以及所有YAML定义类型[map,ootf-16 ]

快速使用

要使用SnakeYaml,首先要部署maven依赖关系。

org.yaml

snakeyaml

1.17

看看最简单的yaml分析示例。

@Test

公共语音测试

stringyamlstr=’ key : hello YAML ‘;

Yaml yaml=new Yaml (;

对象ret=YAML.load (YAML str );

system.out.println(ret );

}

结果输出: {key=hello yaml}

个人资料说明:

1 )使用Yaml类创建所有分析操作都从此对象开始的Yaml对象;

2 )声明yaml字符串并定义了对象(当然也可以使用yaml文档)的key: hello yaml;

3 ) Yaml对象的加载方法使用公共对象加载(字符串Yaml )加载Yaml字符串并返回分析后的对象;

您可以看到实际创建的是Map:LinkedHashMap。

使用加载/加载全部/加载全部方法

YML的load方法可以传递给各种类型的参数。

公共对象加载(字符串YAML )。

publicobjectload (输入流io ) )。

公共对象加载(reader io ) )。

这三种方法都是从输入的不同类型内容分析结果对象而获得的。 需要注意的是,SnakeYaml根据导入的内容是否包含BOM标头来确定输入流的编码格式。 如果不包含物料清单标头,则默认值为UTF-8编码。

以下代码:

公共类yml util {

privatestaticmapymls=new hashmap (;

privatestaticthreadlocalnowfilename=new thread local (;

publicstaticloggerlog=logger factory.getlogger (yml util.class );

publicstaticvoidloadyml (字符串文件名) throws IOException { )。

nowfilename.set(filename;

try {

stringoutpath=system.getproperty (user.dir ) ) File.separator;

inputstream in=new file inputstream (new file (outpathstring.format (‘ % s.yml ‘,fileName ) )

if (! ymls.containskey(filename ) }{

ymls.put(filename,new Yaml ).loadas ) in,LinkedHashMap.class );

}

}catch(ioexceptione ) {

inputstream in=yml util.class.getclass loader (.getresourceasstream (string.format (‘ % s.yml ‘,fileName ) ) );

if (! ymls.containskey(filename ) }{

ymls.put(filename,new Yaml ).loadas ) in,LinkedHashMap.class );

}

}

}

publicstaticobjectgetvalue (字符串密钥) throws Exception { )

string[]keys=key.split(‘[.] );

mapymlinfo=(map ) ymls.get now filename.get () ) ).clone );

for(intI=0; i keys.length; I ) {

String keyStr=keys[i];

对象值=null;

if (string utils.isnumeric space (keystr ) ) }

value=yml info.get (integer.parseint ) keystr );

}else{

value=ymlinfo.get(keystr;

}

if(Ikeys.Length-1 ) {

ymlInfo=(Map ) value;

} else if (值==null ) ) )。

返回空值;

} else {

返回值;

}

}

thrownewruntimeexception (‘ it ‘ simpossibletogethere . ‘;

}

publicstaticstringgetvalue (字符串文件名,字符串密钥)。

对象obj=null;

try {

加载yml (文件名);

obj=getvalue(key;

}catch(filenotfoundexceptione ) {

e .打印堆栈跟踪(;

}catch(exceptione ) {

e .打印堆栈跟踪(;

}

string value=string util.null tostring (obj;

log.info (string.format (getvalue (%s ) bykey ) % s ),value,key );

返回值;

}

publicstaticstringcalibrationmap (字符串文件名、字符串第一密钥、映射参数映射) {

对象obj=null;

字符串提示=null;

try {

加载yml (文件名);

obj=getvalue (第一密钥;

if(objinstanceofmap ) {

prompt=calibration((map ) obj,parameterMap );

}else{

thrownewruntimeexception (‘ checkthecalibration.ymlfileforformaterrors ‘ );

}

}catch(filenotfoundexceptione ) {

e .打印堆栈跟踪(;

}catch(exceptione ) {

e .打印堆栈跟踪(;

}

返回提示;

}

publicstaticstringcalibration (映射参数映射,映射参数映射) {

迭代器=calibration map.entryset ().iterator );

字符串提示=null;

while(iterator.Hasnext () ) )。

Map.Entry entry=iterator.next (;

String key=entry.getKey (;

if (参数映射. contains key (key ) ) }

objectparameterkey=parameter map.get (key;

boolean flag=string util.isempty (参数密钥);

if (标志) {

prompt=entry.getValue (;

布雷克;

}

}else{

prompt=entry.getValue (;

布雷克;

}

}

返回提示;

}

publicstaticvoidmain (字符串[ ] args ) {

string path=yml util.class.getclass loader (.get resource ) ‘ application.yml ‘ ).getPath );

系统. err.println (path );

}

}