其他分享
首页 > 其他分享> > Spring YAML配置文件配置

Spring YAML配置文件配置

作者:互联网

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

我正在使用Spring 4.1.1

这是代码.
这是上下文: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接口的bean来获取活动/默认配置文件并且它是正确的)并且看起来一切都很好但是,当尝试时注入值将忽略配置文件.

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

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

提前致谢

解决方法:

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

我使用SpringProfileDocumentMatcher类[来自Spring boot的Dave Syer]作为我的基础匹配器并实现了EnvironmentAware以获取活动配置文件并将此bean传递给YamlPropertiesFactoryBean bean.
这是代码:

applicationContext.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,spring-profiles,spring-properties,property-placeholder
来源: https://codeday.me/bug/20190623/1271651.html