Spring YAML配置文件配置

时间:2015-11-04 15:41:19

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

我不确定我是否理解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

提前致谢

1 个答案:

答案 0 :(得分:4)

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

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

的applicationContext.xml

for file in $(find /home/omartinez/Downloads -type f -name "*.mkv"); do
    mp4=${file%.mkv}.mp4
    if ! lsof $f && [ ! -e "$mp4" ]; then
        convert $file $mp4
    fi
done

SpringProfileDocumentMatcher.java

onView(withId(R.id.recycler_view)).check(matches(atPosition(0, hasDescendant(withText("Available")))));

}

相关问题