use of org.springframework.cloud.bootstrap.config.BootstrapPropertySource in project xuxiaowei-cloud by xuxiaowei-cloud.
the class EnvironmentTests method getEnvironment.
/**
* 获取环境变量
*/
@Test
void getEnvironment() {
Environment environment = nacosConfigProperties.getEnvironment();
MutablePropertySources mutablePropertySources = configurableEnvironment.getPropertySources();
mutablePropertySources.forEach(propertySource -> {
if (propertySource instanceof OriginTrackedMapPropertySource) {
OriginTrackedMapPropertySource originTrackedMapPropertySource = (OriginTrackedMapPropertySource) propertySource;
String name = originTrackedMapPropertySource.getName();
Map<String, Object> source = originTrackedMapPropertySource.getSource();
System.out.println("# 配置文件:" + name);
for (String key : source.keySet()) {
System.out.println(key + "= " + source.get(key));
}
System.out.println();
} else if (propertySource instanceof MapPropertySource) {
MapPropertySource mapPropertySource = (MapPropertySource) propertySource;
String name = mapPropertySource.getName();
Object property = mapPropertySource.getProperty(name);
} else if (propertySource instanceof BootstrapPropertySource) {
PropertySource<?> delegate = ((BootstrapPropertySource<?>) propertySource).getDelegate();
if (delegate instanceof NacosPropertySource) {
String name = delegate.getName();
if (environment instanceof StandardServletEnvironment) {
StandardServletEnvironment standardServletEnvironment = (StandardServletEnvironment) environment;
MutablePropertySources propertySources = standardServletEnvironment.getPropertySources();
PropertySource<?> bootstrapProperties = propertySources.get("bootstrapProperties-" + name);
assert bootstrapProperties != null;
Object source = bootstrapProperties.getSource();
if (source instanceof LinkedHashMap) {
LinkedHashMap<?, ?> linkedHashMap = (LinkedHashMap<?, ?>) source;
System.out.println("# 配置文件:" + name);
for (Object key : linkedHashMap.keySet()) {
System.out.println(key + "= " + linkedHashMap.get(key));
}
System.out.println();
}
}
}
}
});
}
use of org.springframework.cloud.bootstrap.config.BootstrapPropertySource in project spring-cloud-kubernetes by spring-cloud.
the class ConfigurationChangeDetector method findPropertySources.
/**
* @param <S> property source type
* @param sourceClass class for which property sources will be found
* @return finds all registered property sources of the given type
*/
public <S extends PropertySource<?>> List<S> findPropertySources(Class<S> sourceClass) {
List<S> managedSources = new LinkedList<>();
LinkedList<PropertySource<?>> sources = toLinkedList(this.environment.getPropertySources());
this.log.debug("findPropertySources");
this.log.debug(String.format("environment: %s", this.environment));
this.log.debug(String.format("environment sources: %s", sources));
while (!sources.isEmpty()) {
PropertySource<?> source = sources.pop();
if (source instanceof CompositePropertySource) {
CompositePropertySource comp = (CompositePropertySource) source;
sources.addAll(comp.getPropertySources());
} else if (sourceClass.isInstance(source)) {
managedSources.add(sourceClass.cast(source));
} else if (source instanceof BootstrapPropertySource) {
PropertySource<?> propertySource = ((BootstrapPropertySource<?>) source).getDelegate();
if (sourceClass.isInstance(propertySource)) {
sources.add(propertySource);
}
}
}
return managedSources;
}
Aggregations