【问题标题】:Spring configuration from yaml - combining @ConfigurationEnabled loading for parametrized file name来自 yaml 的 Spring 配置 - 结合 @ConfigurationEnabled 加载参数化文件名
【发布时间】:2019-01-27 16:34:59
【问题描述】:

我遇到的问题可能非常独特。我在同一个项目中有两个应用程序,使用两个不同的 spring 配置文件。

当我运行它们时,我手动加载上下文

context = new ClassPathXmlApplicationContext("hal.context.xml");
context.registerShutdownHook();

我在每个文件中指定了所需的弹簧上下文文件。

问题来自配置,因为我有两个配置文件存储在yaml文件中。

我最好的解决方案是使用 Spring Boot 工具在 POJO 中加载配置(它们具有相同的结构)

@org.springframework.context.annotation.Configuration("configuration")
@ConfigurationProperties
@EnableConfigurationProperties

和

@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
    yaml.setResources(new ClassPathResource("filename"));
    propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());

    return propertySourcesPlaceholderConfigurer;
}

我找不到以参数化方式加载配置的方法......有没有任何选项可以结合这个,也许

<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
    <property name="resources" value="classpath:hal.config.yml"/>
</bean>

但不是使用属性占位符&lt;context:property-placeholder properties-ref="yamlProperties"/&gt; 来寻找一种方法来自动生成带有配置的 POJO。

提前谢谢你

卢卡

【问题讨论】:

    标签: spring spring-boot configuration yaml


    【解决方案1】:

    您可以使用两个不同的前缀(每个配置文件一个)来分隔配置值:

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        propertySourcesPlaceholderConfigurer.setPlaceholderPrefix("${app1:");
        propertySourcesPlaceholderConfigurer.setPlaceholderSuffix("}");
    
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("app1_config_filename"));
        propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
    
        return propertySourcesPlaceholderConfigurer;
    }
    
    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        propertySourcesPlaceholderConfigurer.setPlaceholderPrefix("${app2:");
        propertySourcesPlaceholderConfigurer.setPlaceholderSuffix("}");
    
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("app2_config_filename"));
        propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
    
        return propertySourcesPlaceholderConfigurer;
    }
    

    然后在将配置值注入到您的 bean 中时,您可以这样做:

    @Value("{$app1:myVal}")
    String myVal;
    

    和

    @Value("{$app2:myVal}")
    String myVal;
    

    从 app1 和 app2 进入 bean

    希望对你有帮助

    【讨论】:

    • 您好,感谢您的回答。该问题建议我将应用程序拆分为两个单独的应用程序,但我正在尝试找到一种解决方法。我的第二个选择是将每个属性手动注入到配置文件中(我有两个 spring 上下文 xml 文件)。不幸的是,您的解决方案不起作用,因为 Configuration POJO 只能保存一个配置(我有两个结构相同的配置文件)。我的解释可能不清楚,如果您需要更多信息,请随时告诉我。
    • 我强烈建议将应用程序一分为二。它会更容易维护和配置,您不会遇到另一个应用程序生成的意外错误
    • 我知道,但现阶段还为时过早。我仍然希望有一个解决方法:-)
    猜你喜欢
    • 2021-06-03
    • 1970-01-01
    • 2016-02-05
    • 2019-10-19
    • 2023-03-15
    • 2018-11-21
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多