#由于spingboot2.x取消了1.x的RelaxedPropertyResolver,原始实现EnvironmentAware获取配置失效;
解决办法
@Configuration
public class TestConfiguration implements EnvironmentAware {
private Properties properties;
@Bean(name = "testBeanName")
TestBean getTestBean(){
//获取属性,这里就可以得到yml中的www.baidu.com
String url = properties.getProperty("url");
}
@Override
public void setEnvironment(Environment environment) {
Iterable<ConfigurationPropertySource> sources = ConfigurationPropertySources.get(environment);
Binder binder = new Binder(sources);
//这里设置你的前缀
BindResult<Properties> bindResult = binder.bind("test", Properties.class);
this.properties = bindResult.get();
}
}
test:
url: www.baidu.com