Raf*_*ele 5 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"/>
Run Code Online (Sandbox Code Playgroud)
其中yamlProperties是以下bean
<bean id="yamlProperties"
class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources"
value="file:${catalina.home}/properties/test.yml"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
这是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##
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我尝试通过以下方式配置(通过xml)我的数据源时:
<property name="url" value="${db.url}"/>
<property name="username" value="${db.usr}"/>
<property name="password" value="${db.pwd}"/>
Run Code Online (Sandbox Code Playgroud)
Spring始终使用YAML文件中的最后一个配置,而忽略该配置文件。我试图通过web.xml中的contex-parameter传递活动配置文件,或者直接将其传递给JVM(我实现了一个实现EnvironmentAware接口的bean,以获取活动/默认配置文件,并且是正确的),但看起来一切都很好,但是,当尝试注入值将忽略配置文件。
我相信使用property-placeholder上下文(带有订单)可以获得一个Property-placeholder,它是PropertySourcesPlaceholderConfigurer的一个实例,因此可以访问Environment,但是我无法理解为什么配置文件被忽略并且spring获得了最后的yaml文件配置。
我在63.6节http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html中添加了对文档的引用(spring-boot)。
提前致谢
不确定这是否有帮助,但这就是我所做的。
我使用 SpringProfileDocumentMatcher 类 [由 Spring Boot 中的 Dave Syer 提供] 作为我的基本匹配器,并实现了 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>
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
3292 次 |
| 最近记录: |