小编这里介绍两种方式:
第一种: 基于yaml工厂方式
package org.springframework.beans.factory.config;
package java.util;
//获取yml文件环境配置
YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
String path = "application.yml";
factoryBean.setResources(new ClassPathResource(path));
Properties properties = factoryBean.getObject();
String active = String.valueOf(properties.get("spring.想要获取的路径"));
//获取application-???.yml文件内的某信息
YamlPropertiesFactoryBean factoryBeanActive = new YamlPropertiesFactoryBean();
String pathActive = "application-" + active + ".yml";
factoryBeanActive.setResources(new ClassPathResource(pathActive));
Properties propertiesActive = factoryBeanActive.getObject();
//获取aaa下的url
String ecmallAuthUrl = String.valueOf(propertiesActive.get("aaa.url"));
第二种基于注解获取:
@Value("${aaa.url}")
private String url;