场景:工具类读取application.yml配置文件信息
记录:NO.237
本例环境:
spring-boot:2.2.0.RELEASE
jdk:1.8
1.加载单个配置文件
public static Object getYmlPropertyValue(String key) {
Properties properties = null;
try {
Resource resource = new ClassPathResource(YML_FILE_01);
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
yamlFactory.setResources(resource);
properties = yamlFactory.getObject();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return properties.get(key);
}
2.加载多个配置文件
public static Properties loadProperties(String... ymlFliePaths) {
Resource[] resources = null;
Properties properties = null;
List<Resource> resourcesList = new ArrayList<Resource>();
for (String location : ymlFliePaths) {
Resource resource = new ClassPathResource(location);
resourcesList.add(resource);
}
resources = resourcesList.toArray(new Resource[resourcesList.size()]);
try {
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
yamlFactory.setResources(resources);
properties = yamlFactory.getObject();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return properties;
}
3.测试main函数
public static void main(String [] args){
System.out.println("测试开始......");
System.out.println("单个文件,加载application.yml,打印server.servlet.context-path值:");
System.out.println(getYmlPropertyValue("server.servlet.context-path").toString());
System.out.println("加载多个文件:");
Properties properties = loadProperties(YML_FILE_01,YML_FILE_02);
System.out.println("打印application.yml中的server.servlet.context-path值:");
System.out.println(properties.get("server.servlet.context-path").toString());
System.out.println("打印application-common.yml中的plat.node.service值:");
System.out.println(properties.get("plat.node.service").toString());
System.out.println("测试结束......");
}
测试结果:
4.完整测试用例
public class YmlPropertiesLoader {
private static String YML_FILE_01 = "application.yml";
private static String YML_FILE_02 = "application-common.yml";
/**
* 根据key获取value值
* 加载一个配置文件
*/
public static Object getYmlPropertyValue(String key) {
Properties properties = null;
try {
Resource resource = new ClassPathResource(YML_FILE_01);
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
yamlFactory.setResources(resource);
properties = yamlFactory.getObject();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return properties.get(key);
}
/**
* 根据key获取value值
* 加载多个配置文件
*/
public static Properties loadProperties(String... ymlFliePaths) {
Resource[] resources = null;
Properties properties = null;
List<Resource> resourcesList = new ArrayList<Resource>();
for (String location : ymlFliePaths) {
Resource resource = new ClassPathResource(location);
resourcesList.add(resource);
}
resources = resourcesList.toArray(new Resource[resourcesList.size()]);
try {
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
yamlFactory.setResources(resources);
properties = yamlFactory.getObject();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return properties;
}
//测试主函数
public static void main(String [] args){
System.out.println("测试开始......");
System.out.println("单个文件,加载application.yml,打印server.servlet.context-path值:");
System.out.println(getYmlPropertyValue("server.servlet.context-path").toString());
System.out.println("加载多个文件:");
Properties properties = loadProperties(YML_FILE_01,YML_FILE_02);
System.out.println("打印application.yml中的server.servlet.context-path值:");
System.out.println(properties.get("server.servlet.context-path").toString());
System.out.println("打印application-common.yml中的plat.node.service值:");
System.out.println(properties.get("plat.node.service").toString());
System.out.println("测试结束......");
}
}
以上,感谢