Search in sources :

Example 6 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project helio-starters by uncarbon97.

the class AdaptHistoryVersionAutoConfiguration method setEnvironment.

@Override
public void setEnvironment(@NonNull Environment env) {
    try {
        ConfigurableEnvironment c = (ConfigurableEnvironment) env;
        MutablePropertySources sources = c.getPropertySources();
        Map<String, Object> newMap = new LinkedHashMap<>();
        for (PropertySource<?> source : sources) {
            if (source instanceof OriginTrackedMapPropertySource) {
                // 根据类判断是否为 SpringBoot .yml 或者 .properties 的配置
                Map<String, Object> bootProp = ((OriginTrackedMapPropertySource) source).getSource();
                for (String key : bootProp.keySet()) {
                    if (key != null) {
                        for (int i = 0; i < oldPropertyPrefixes.length; i++) {
                            if (key.startsWith(oldPropertyPrefixes[i])) {
                                /*
                                    将配置文件中所有 [helio.crud.tenant.] 开头的配置转移到 [helio.tenant.] 下
                                    实践测试 只对 @Value 注解有效
                                     */
                                String newKey = StrUtil.replace(key, oldPropertyPrefixes[i], newPropertyPrefixes[i]);
                                newMap.put(newKey, bootProp.get(key));
                                // 前缀相同的话,提示信息就保持一条(利用 Set 特性)
                                deprecatedPropertyWarnings.add(StrUtil.format("配置文件属性前缀 {}* 已过时、不向下兼容,请转移到 {}* 下", oldPropertyPrefixes[i], newPropertyPrefixes[i]));
                            }
                        }
                    }
                }
            }
        }
        // 追加到总配置里面
        if (newMap.size() > 0) {
            OriginTrackedMapPropertySource source = new OriginTrackedMapPropertySource("AdaptHistoryVersionAutoConfiguration", newMap);
            // 追加到末尾,优先级最低
            c.getPropertySources().addLast(source);
            System.err.println("\n" + StrUtil.join("\n", deprecatedPropertyWarnings) + "\n");
        }
    } catch (Exception e) {
    // ignored
    }
}
Also used : OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project spring-boot by spring-projects.

the class IntegrationPropertiesEnvironmentPostProcessor method registerIntegrationPropertiesPropertySource.

protected void registerIntegrationPropertiesPropertySource(ConfigurableEnvironment environment, Resource resource) {
    PropertiesPropertySourceLoader loader = new PropertiesPropertySourceLoader();
    try {
        OriginTrackedMapPropertySource propertyFileSource = (OriginTrackedMapPropertySource) loader.load("META-INF/spring.integration.properties", resource).get(0);
        environment.getPropertySources().addLast(new IntegrationPropertiesPropertySource(propertyFileSource));
    } catch (IOException ex) {
        throw new IllegalStateException("Failed to load integration properties from " + resource, ex);
    }
}
Also used : OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) PropertiesPropertySourceLoader(org.springframework.boot.env.PropertiesPropertySourceLoader) IOException(java.io.IOException)

Example 8 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project spring-boot by spring-projects.

the class PropertiesMigrationReporter method mapPropertiesWithReplacement.

private PropertySource<?> mapPropertiesWithReplacement(PropertiesMigrationReport report, String name, List<PropertyMigration> properties) {
    report.add(name, properties);
    List<PropertyMigration> renamed = properties.stream().filter(PropertyMigration::isCompatibleType).collect(Collectors.toList());
    if (renamed.isEmpty()) {
        return null;
    }
    String target = "migrate-" + name;
    Map<String, OriginTrackedValue> content = new LinkedHashMap<>();
    for (PropertyMigration candidate : renamed) {
        OriginTrackedValue value = OriginTrackedValue.of(candidate.getProperty().getValue(), candidate.getProperty().getOrigin());
        content.put(candidate.getMetadata().getDeprecation().getReplacement(), value);
    }
    return new OriginTrackedMapPropertySource(target, content);
}
Also used : OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) OriginTrackedValue(org.springframework.boot.origin.OriginTrackedValue) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project innovation-funding-service by InnovateUKGitHub.

the class StubDevPropertiesPostProcessor method registerPropertySource.

protected void registerPropertySource(ConfigurableEnvironment environment, Resource resource) {
    PropertySourceLoader loader = new YamlPropertySourceLoader();
    try {
        OriginTrackedMapPropertySource propertyFileSource = (OriginTrackedMapPropertySource) loader.load(AUTOCONFIG_STUBDEV_YML, resource).get(0);
        environment.getPropertySources().addFirst(propertyFileSource);
    } catch (IOException ex) {
        throw new IllegalStateException("Failed to load properties from " + resource, ex);
    }
}
Also used : OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) YamlPropertySourceLoader(org.springframework.boot.env.YamlPropertySourceLoader) PropertySourceLoader(org.springframework.boot.env.PropertySourceLoader) YamlPropertySourceLoader(org.springframework.boot.env.YamlPropertySourceLoader) IOException(java.io.IOException)

Example 10 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project innovation-funding-service by InnovateUKGitHub.

the class YamlPropertyLoader method registerPropertySource.

/**
 * Loads yaml properties into the spring context as a property source.
 * @param environment the spring ConfigurableEnvironment
 * @param resource the yaml file
 */
public static void registerPropertySource(ConfigurableEnvironment environment, Resource resource) {
    PropertySourceLoader loader = new YamlPropertySourceLoader();
    try {
        OriginTrackedMapPropertySource propertyFileSource = (OriginTrackedMapPropertySource) loader.load(YamlPropertyLoader.class.getSimpleName() + "->" + resource.getFilename(), resource).get(0);
        environment.getPropertySources().addFirst(propertyFileSource);
    } catch (IOException ex) {
        throw new IllegalStateException("Failed to load properties from " + resource, ex);
    }
}
Also used : OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) YamlPropertySourceLoader(org.springframework.boot.env.YamlPropertySourceLoader) PropertySourceLoader(org.springframework.boot.env.PropertySourceLoader) YamlPropertySourceLoader(org.springframework.boot.env.YamlPropertySourceLoader) IOException(java.io.IOException)

Aggregations

OriginTrackedMapPropertySource (org.springframework.boot.env.OriginTrackedMapPropertySource)20 IOException (java.io.IOException)8 LinkedHashMap (java.util.LinkedHashMap)8 Map (java.util.Map)4 PropertySource (org.springframework.core.env.PropertySource)4 HashMap (java.util.HashMap)3 PropertySourceLoader (org.springframework.boot.env.PropertySourceLoader)3 Environment (org.springframework.cloud.config.environment.Environment)3 MutablePropertySources (org.springframework.core.env.MutablePropertySources)3 NacosException (com.alibaba.nacos.api.exception.NacosException)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 PropertiesPropertySourceLoader (org.springframework.boot.env.PropertiesPropertySourceLoader)2 YamlPropertySourceLoader (org.springframework.boot.env.YamlPropertySourceLoader)2 OriginTrackedValue (org.springframework.boot.origin.OriginTrackedValue)2 PropertySource (org.springframework.cloud.config.environment.PropertySource)2 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)2 MapPropertySource (org.springframework.core.env.MapPropertySource)2 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)2 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)2