下面列出了怎么用org.springframework.boot.env.OriginTrackedMapPropertySource的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource encodedResource) throws IOException {
if (encodedResource == null) {
return emptyPropertySource(name);
}
Resource resource = encodedResource.getResource();
String fileName = resource.getFilename();
List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(fileName, resource);
if (sources.isEmpty()) {
return emptyPropertySource(fileName);
}
// yaml 数据存储,合成一个 PropertySource
Map<String, Object> ymlDataMap = new HashMap<>(32);
for (PropertySource<?> source : sources) {
ymlDataMap.putAll(((MapPropertySource) source).getSource());
}
return new OriginTrackedMapPropertySource(getSourceName(fileName, name), ymlDataMap);
}
@Test
public void originTrackedPropertySourceWithoutOriginWorks() {
MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setProperty("normalKey", "normalValue");
mockEnvironment.getPropertySources()
.addFirst(new OriginTrackedMapPropertySource("myorigintrackedsource",
Collections.singletonMap("keyNoOrigin", "valueNoOrigin")));
PassthruEnvironmentRepository repository = new PassthruEnvironmentRepository(
mockEnvironment);
Environment environment = repository.findOne("testapp", "default", "master",
true);
assertThat(environment).isNotNull();
List<PropertySource> propertySources = environment.getPropertySources();
assertThat(propertySources).hasSize(2);
for (PropertySource propertySource : propertySources) {
Map source = propertySource.getSource();
if (propertySource.getName().equals("myorigintrackedsource")) {
assertThat(source).containsEntry("keyNoOrigin", "valueNoOrigin");
}
else if (propertySource.getName().equals("mockProperties")) {
assertThat(source).containsEntry("normalKey", "normalValue");
}
}
}
public void add(PropertySource<?> source) {
// Only add map property sources added by boot, see gh-476
if (source instanceof OriginTrackedMapPropertySource
&& !this.names.contains(source.getName())) {
this.sources.addPropertySource(source);
this.names.add(source.getName());
}
}