Spring Boot:尝试使用EnvironmentPostProcessor覆盖application.properties

问题描述 投票:1回答:1

因此,我试图使用存储在Cosul中的键/值来覆盖application.properties中的值。我尝试了两件事。

1)使用Spring Cloud Consul Config。 https://cloud.spring.io/spring-cloud-consul/reference/html/#spring-cloud-consul-config

如果我没有在application.properties中定义相同的密钥,则此方法有效。如果在application.properties中定义,则在所有@Value批注解析中使用属性文件中的值。这与我想要的相反。

2)由于上述操作无效,因此我继续创建了一个自定义的EnvironmentPostProcessor。我首先尝试构建一个MapPropertySource并使用environment.getPropertySources()。addAfter(..)。这与上面的结果相同。然后,我尝试遍历所有属性源,找到一个名称包含“ applicationConfig:[classpath:/ application”的属性,并设置属性值(如果存在)或放置新的属性值。另外,我将MapPropertySource添加到“ applicationConfig:[classpath:/ application”属性源所在的同一EnumerableCompositePropertySource中。

无论采用哪种方法,结果始终相同。如果密钥存在于application.properties中,则使用该值。

什么给了?我实际上是重写属性源中的值,并且在PostProcessor完成其工作之前,我可以在调试器中看到这些值。 application.properties值仍如何到达@Value批注?

这是我当前的PostProcessor。

@Order(Ordered.LOWEST_PRECEDENCE)
public class ConsulPropertyPostProcessor implements EnvironmentPostProcessor {


    private static final String PROPERTY_SOURCE_NAME = "applicationConfigurationProperties";

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        PropertySource<?> system = environment.getPropertySources().get(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);

        ConsulKVService consulKVService = new ConsulKVServiceImpl().instantiateConsulKVServiceImpl((String)system.getProperty("CONSUL_HOST"), (String)system.getProperty("CONSUL_TOKEN"));
        Map<String, Object> map = consulKVService.getConsulKeysAndValuesByPrefix((String)system.getProperty("CONSUL_PREFIX"));


        addOrReplace(environment.getPropertySources(), map);
    }

    private void addOrReplace(MutablePropertySources propertySources, Map<String, Object> map) {
        MapPropertySource target = new MapPropertySource("applicationConfig: [consulKVs]", map);
        if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
            PropertySource<?> applicationConfigurationPropertySources = propertySources.get(PROPERTY_SOURCE_NAME);

            for(EnumerableCompositePropertySource applicationPropertySource : (ArrayList<EnumerableCompositePropertySource>)applicationConfigurationPropertySources.getSource()){
                if(applicationPropertySource.getName() != null
                        && applicationPropertySource.getName().contains("applicationConfig: [profile=")) {

                    for(PropertySource singleApplicationPropertySource : applicationPropertySource.getSource()){
                        if(singleApplicationPropertySource.getName().contains("applicationConfig: [classpath:/application")){

                            for (String key : map.keySet()) {
                                if(map.get(key) != null) {
                                    if (singleApplicationPropertySource.containsProperty(key)) {
                                        ((Properties) singleApplicationPropertySource.getSource())
                                                .setProperty(key, (String) map.get(key));
                                    } else {
                                        ((Properties) singleApplicationPropertySource.getSource()).put(key, (String) map.get(key));
                                    }
                                }
                            }
                            break;
                        }
                    }

                    applicationPropertySource.add(target);

                    break;


                }
            }

        }

    }
}

提前感谢大家。

编辑:尝试覆盖ApplicationListener类的onApplicationEvent方法,其结果与上述相同。这是代码。

@Log4j
public class ConsulProperties implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

    static ConfigurableEnvironment configurableEnvironment;
    private static final String PROPERTY_SOURCE_NAME = "applicationConfigurationProperties";

    public static ConfigurableEnvironment getConfigurableEnvironment() {
        return configurableEnvironment;
    }

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        log.info("Received ApplicationEnvironmentPreparedEvent...");
        ConfigurableEnvironment environment = event.getEnvironment();
        configurableEnvironment = environment;
        Properties props = new Properties();

        ConsulKVService consulKVService = new ConsulKVServiceImpl()
                .instantiateConsulKVServiceImpl((String) configurableEnvironment.getProperty("CONSUL_HOST"),
                        (String) configurableEnvironment.getProperty("CONSUL_TOKEN"));
        Map<String, Object> map = consulKVService.getConsulKeysAndValuesByPrefix((String) configurableEnvironment.getProperty("CONSUL_PREFIX"));
        while(map.values().remove(null));
        addOrReplace(environment.getPropertySources(), map);
    }


    private void addOrReplace(MutablePropertySources propertySources, Map<String, Object> map) {
        MapPropertySource target = new MapPropertySource("applicationConfig: [consulKVs]", map);
        if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
            PropertySource<?> applicationConfigurationPropertySources = propertySources.get(PROPERTY_SOURCE_NAME);

            for(EnumerableCompositePropertySource applicationPropertySource : (ArrayList<EnumerableCompositePropertySource>)applicationConfigurationPropertySources.getSource()){
                if(applicationPropertySource.getName() != null
                        && applicationPropertySource.getName().contains("applicationConfig: [profile=")) {

                    for(PropertySource singleApplicationPropertySource : applicationPropertySource.getSource()){
                        if(singleApplicationPropertySource.getName().contains("applicationConfig: [classpath:/application")){

                            for (String key : map.keySet()) {
                                if (singleApplicationPropertySource.containsProperty(key)) {
                                    ((Properties) singleApplicationPropertySource.getSource())
                                            .setProperty(key, (String) map.get(key));
                                } else {
                                    ((Properties) singleApplicationPropertySource.getSource()).put(key,
                                            map.get(key));
                                }
                            }


                            applicationPropertySource.add(target);

                            Properties properties = new Properties();
                            properties.putAll(map);
                            propertySources.addLast(new PropertiesPropertySource("consulKVs", properties));

                            break;
                        }
                    }


                    break;


                }
            }
        }
    }
}
java spring spring-boot properties-file
1个回答
0
投票

似乎您正在尝试更改spring并非为之设计的约定。您提供的代码不太容易维护,并且需要对Spring内部结构有深入的了解。坦白说,如果不调试,我无法告诉您如何实现所需的目标,但是我想到了另一种方法:

您可以通过以下方式使用弹簧轮廓:

假设您在db.name=abc中具有属性application.properties,在领事中具有db.name=xyz,并且我认为您的目标是在春季之前解决db.name=xyz

在这种情况下,如果要从本地文件中获取属性,请将db.name=abc移至application-local.properties并以--spring.profiles.active=local启动应用程序,如果要使用领事,则不使用此配置文件。

您甚至可以在EnvironmentPostProcessor中动态添加一个活动配置文件(无论如何您已经到那里了,但这是EnvironmentPostProcessor中的一行代码。

© www.soinside.com 2019 - 2024. All rights reserved.