package com.common.test.utils;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.util.Properties;
/**
* Created by admin on 2019/1/4.
*/
public class PropertiesUtils {
private static String PROPERTY_NAME = "application-common.yml";
public static Object getCommonYml(Object key){
Resource resource = new ClassPathResource(PROPERTY_NAME);
Properties properties = null;
try {
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
yamlFactory.setResources(resource);
properties = yamlFactory.getObject();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return properties.get(key);
}
public static void main(String[] args) {
System.out.println(getCommonYml("file.upload.path"));
}
}
application.yml配置信息:
spring:
profiles:
include: jdbc,common
特别注意要将application-common.yml 加入到classpath 中去,不然可能读取不到文件,这边例子只是使用的classPath中加载配置,各位也可以从文件系统获取,具体还得看各自需要了,建议使用classPath加载。