Search in sources :

Example 1 with BootstrapPropertySource

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();
                    }
                }
            }
        }
    });
}
Also used : BootstrapPropertySource(org.springframework.cloud.bootstrap.config.BootstrapPropertySource) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) LinkedHashMap(java.util.LinkedHashMap) OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) NacosPropertySource(com.alibaba.cloud.nacos.client.NacosPropertySource) OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with BootstrapPropertySource

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;
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) BootstrapPropertySource(org.springframework.cloud.bootstrap.config.BootstrapPropertySource) LinkedList(java.util.LinkedList) PropertySource(org.springframework.core.env.PropertySource) CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) BootstrapPropertySource(org.springframework.cloud.bootstrap.config.BootstrapPropertySource)

Aggregations

BootstrapPropertySource (org.springframework.cloud.bootstrap.config.BootstrapPropertySource)2 NacosPropertySource (com.alibaba.cloud.nacos.client.NacosPropertySource)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Test (org.junit.jupiter.api.Test)1 OriginTrackedMapPropertySource (org.springframework.boot.env.OriginTrackedMapPropertySource)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 CompositePropertySource (org.springframework.core.env.CompositePropertySource)1 MapPropertySource (org.springframework.core.env.MapPropertySource)1 PropertySource (org.springframework.core.env.PropertySource)1 StandardServletEnvironment (org.springframework.web.context.support.StandardServletEnvironment)1