方式一
public class TestClass{
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) {
String str = getCommonYml("");
}
}
方式二
public class TestClass{
@Value("${address}")
private String address;
}
参考文章
SpringBoot工具类获取yml配置文件内容