当前位置: 首页 > 知识库问答 >
问题:

Spring YAML配置文件配置

凌俊名
2023-03-14

我不确定我是否很好地理解了Spring概要文件是如何处理yaml和属性文件的。我试图将这两种类型的配置分开(这两个文件不共享任何配置),但在从yaml配置中读取配置文件时遇到了问题。

<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"/>
    <bean id="yamlProperties" 
    class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
                <property name="resources" 
value="file:${catalina.home}/properties/test.yml"/>
            </bean>
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,以获取活动/默认概要文件和它的corret),看起来一切都很好,但是当试图注入值时,该概要文件被忽略了。

我相信使用property-placeholder上下文(带有orders)可以得到一个property-placeholderConfigurer实例的PropertySourcesPlaceholderConfigurer,但是我不明白为什么忽略这个配置文件而spring得到最后一个yaml文件配置。

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

提前致谢

共有1个答案

白子默
2023-03-14

不确定这是否在这一点上有所帮助,但以下是我所做的。

我使用SpringProfileDocumentMatcher类[spring boot的Dave Syer]作为基本匹配器,并实现EnvironmentAware来获取actives概要文件,并将此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);
}

}

 类似资料:
  • 配置文件使用的是 JSON 格式。 JSON 中使用的数据结构和变量名对应着 Burp UI 中显示选项。生成配置文件的最简单方法是在 Burp UI 中创建所需的配置,然后保存为配置文件。您还可以手动编辑现有的配置文件,其内容是很容易就可以看懂的。 部分配置文件可以在需要时使用。您可以通过每个子工具选项卡的配置面板上的”选项(Options)”按钮来保存该区域的配置,或者通过从完整配置的文件中删

  • 目录: 在配置项目yml文件中: 问题: null 客户端YML: 有没有人知道我怎样才能在这两种情况下只带一个配置文件?

  • Webpack 在执行的时候,除了在命令行传入参数,还可以通过指定的配置文件来执行。默认情况下,会搜索当前目录的 webpack.config.js 文件,这个文件是一个 node.js 模块,返回一个 json 格式的配置信息对象,或者通过 --config 选项来指定配置文件。 继续我们的案例,在根目录创建 package.json 来添加 webpack 需要的依赖: { "name":

  • 本页阐述了Apache服务器的配置文件。 主配置文件 相关模块 相关指令 mod_mime <IfDefine> Include TypesConfig Apache的配置文件是包含若干指令的纯文本文件。主配置文件通常叫httpd.conf ,其位置是编译时确定的,但可以用命令行参数 -f 来改变。另外,还可以用Include指令和通配符附加许多其他配置文件。任何配置文件都可以使用任何指令。只有在

  • 在 Web 开发中配置是一个非常常用的功能,笔者认为没有绝对的零配置,即便是编码也属于配置, 而且编码写入的配置在Java语言中需要重新编码才可修改,而生产环境中去修改代码是很麻烦的一件事。 Blade中配置的概念更加简化,当然即便你不使用配置文件也可以完全保证你的项目正常运行, 但开发程序落地到每位工程师手上就像是魔法一样,保不齐谁会有大胆的想法,我们满足你。 创建配置文件 我们约定在先,创建的