Spring YAML 配置文件配置

标签 spring yaml spring-profiles spring-properties property-placeholder

我不确定我是否充分理解 Spring 配置文件如何与 yaml 和属性文件一起使用。
我试图混合这两种类型的配置(这两个文件不共享任何配置)但是我在从 yaml 配置中读取配置文件时遇到了问题。

我正在使用 Spring 4.1.1

这是代码。
这是 context:property-placeholder 配置:

<context:property-placeholder location="classpath:/job-config.properties" order="1" 
ignore-unresolvable="true" ignore-resource-not-found="false"/>


<context:property-placeholder properties-ref="yamlProperties" order="2"
ignore-resource-not-found="false" ignore-unresolvable="true"/>

其中 yamlProperties 是以下 bean
    <bean id="yamlProperties" 
    class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
                <property name="resources" 
value="file:${catalina.home}/properties/test.yml"/>
            </bean>

这是test.yml
spring:
  profiles.default: default
---
spring:
  profiles: default
db:
  url: jdbc:oracle:thin:@##hostname##:##port##:##SID##
  usr: ##USER##
  pwd: ##PWD##
---
spring:
  profiles: development
db:
  url: jdbc:oracle:thin:@##hostname##:##port##:##SID_DEVELOPMENT##
  usr: ##USER_DEVELOPMENT##
  pwd: ##PWD_DEVELOPMENT##

我的问题是,当我尝试通过这样做来配置(通过 xml)我的数据源时:
<property name="url" value="${db.url}"/>
<property name="username" value="${db.usr}"/>
<property name="password" value="${db.pwd}"/>

Spring 总是使用 YAML 文件中的最后一个配置,而忽略配置文件。我试图通过 web.xml 中的 contex-parameter 或直接将事件配置文件传递给 JVM(我实现了一个实现 EnvironmentAware 接口(interface)的 bean 以获取事件/默认配置文件,并且它是正确的),这似乎一切都很好,但是,当试图注入(inject)配置文件的值被忽略。

我相信使用属性占位符上下文(带有订单)我得到一个属性占位符,它是 PropertySourcesPlaceholderConfigurer 的一个实例,因此可以访问 Environment 但我不明白为什么配置文件被忽略并且 spring 获取最后一个 yaml 文件配置。

我在第 63.6 节中添加了对文档(spring-boot)的引用
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html

提前致谢

最佳答案

目前不确定这是否有帮助,但这就是我所做的。

我使用 SpringProfileDocumentMatcher 类 [by Dave Syer from spring boot] 作为我的基本匹配器并实现 EnvironmentAware 以获取事件配置文件并将此 bean 传递给 YamlPropertiesFactoryBean bean。
这是代码:

应用程序上下文.xml

<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources" value="classpath:application.yml" />
<property name="documentMatchers">
  <bean class="com.vivastream.quant.spring.SpringProfileDocumentMatcher" />
</property>


SpringProfileDocumentMatcher.java
public class SpringProfileDocumentMatcher implements DocumentMatcher, EnvironmentAware {

private static final String[] DEFAULT_PROFILES = new String[] {
        "^\\s*$"
};

private String[] activeProfiles = new String[0];

public SpringProfileDocumentMatcher() {
}

@Override
public void setEnvironment(Environment environment) {
    if (environment != null) {
        addActiveProfiles(environment.getActiveProfiles());
    }
}

public void addActiveProfiles(String... profiles) {
    LinkedHashSet<String> set = new LinkedHashSet<String>(Arrays.asList(this.activeProfiles));
    Collections.addAll(set, profiles);
    this.activeProfiles = set.toArray(new String[set.size()]);
}

@Override
public MatchStatus matches(Properties properties) {
    String[] profiles = this.activeProfiles;
    if (profiles.length == 0) {
        profiles = DEFAULT_PROFILES;
    }
    return new ArrayDocumentMatcher("spring.profiles", profiles).matches(properties);
}

}

关于Spring YAML 配置文件配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33525951/

相关文章:

java - 如何使用 Spring Security 从失败的登录中获取用户名?

Java Spring 投影内部投影

android - 在 System.getenv 中找不到 Github Secret

spring-boot - Spring Boot 以编程方式设置配置文件

java - 如何修复直接写入 HTTP header 输出的 Findbugs HTTP 参数

java - Spring Boot 应用程序无法使用 SSH 启动

python - 在 python 中读取 YAML 配置文件并使用变量

visual-studio-code - 折叠/展开 yaml 文件的部分

java - 如何在 Spring Autowiring 具有特定配置文件的实现者?

java - Spring-boot 2 中的多个配置文件属性配置