获取properties文件中的内容
最近做项目一些值需要放在配置文件中,一开始使用的是properties文件,我这里没有使用注解@Value获取。
获取:
InputStream in = MessageUtils.class.getClassLoader().getResourceAsStream("application.properties");
Properties properties = new Properties();
properties.load(in);
System.out.println(properties.getProperty("spring.profiles.active"));
结果:
获取yml文件中的内容
Yaml yaml = new Yaml();
URL url = MessageUtils.class.getClassLoader().getResource("application-dev.yml");
if (url != null) {
//获取test.yaml文件中的配置数据,然后转换为obj,
Object obj = yaml.load(new FileInputStream(url.getFile()));
System.out.println(obj);
//也可以将值转换为Map
Map map = (Map) yaml.load(new FileInputStream(url.getFile()));
System.out.println(map);
Map map2 = (Map) map.get("aliyun");
System.out.println(map2.get("accessKeyId"));
//通过map我们取值就可以了.
}
结果: