【发布时间】: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>
但不是使用属性占位符<context:property-placeholder properties-ref="yamlProperties"/> 来寻找一种方法来自动生成带有配置的 POJO。
提前谢谢你
卢卡
【问题讨论】:
标签: spring spring-boot configuration yaml