Spring YAML 配置文件配置

     2023-02-26     303

关键词:

【中文标题】Spring YAML 配置文件配置【英文标题】:Spring YAML profile configuration 【发布时间】:2016-02-05 04:32:38 【问题描述】:

我不确定我是否充分了解 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 的一个实例,因此可以访问环境,但我不明白为什么配置文件被忽略并且 spring 获取最后一个 yaml 文件配置。

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

提前致谢

【问题讨论】:

我通过实现自己的 PropertiesFactory 来解决。我从 Spring Boot 核心(不是来自 Spring 核心)和 SpringProfileDocumentMatcher 导入 YamlProcessor。我的班级还实现了 EnvironmentAware。我更改了 createProperties() 方法以读取活动配置文件并设置正确的 DocumentMatcher。 这里是代码 String [] activeProfiles = env.getActiveProfiles(); if(activeProfiles.length==0) this.setMatchDefault(true); this.setDocumentMatchers(new SpringProfileDocumentMatcher()); else this.setMatchDefault(false); SpringProfileDocumentMatcher 匹配器 = 新 SpringProfileDocumentMatcher(); for(String profile : activeProfiles) matcher.addActiveProfiles(profile); this.setDocumentMatchers(matcher); 我遇到了类似的问题。这是我解决它的方法:***.com/questions/47717871/… 【参考方案1】:

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

我使用 SpringProfileDocumentMatcher 类 [by Dave Syer from spring boot] 作为我的基本匹配器,并实现了 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);

【讨论】:

感谢 Gustavo,基本上我采用了类似的解决方案。再次感谢 ArrayDocumentMatcher,应该从哪里取?

如何使用 spring boot + .yaml 创建配置文件?

】如何使用springboot+.yaml创建配置文件?【英文标题】:HowcanIcreateprofileswithspringboot+.yaml?【发布时间】:2019-03-0816:23:45【问题描述】:我有带有2个属性文件的SpringBoot服务器:application-local.properties和application-test.properties在每个文... 查看详情

如何使用 spring boot + .yaml 创建配置文件?

】如何使用springboot+.yaml创建配置文件?【英文标题】:HowcanIcreateprofileswithspringboot+.yaml?【发布时间】:2019-03-0816:23:45【问题描述】:我有带有2个属性文件的SpringBoot服务器:application-local.properties和application-test.properties在每个文... 查看详情

Spring Boot 无法从配置文件特定的 yaml 文件中读取

】SpringBoot无法从配置文件特定的yaml文件中读取【英文标题】:Springbootunabletoreadfromprofilespecificyamlfile【发布时间】:2018-03-0506:15:34【问题描述】:我在玩springboot并试图创建一个配置文件特定的配置文件。我叫它application-local.yml... 查看详情

如何使用 YAML 文件在 Spring Boot 中配置 Swagger?

】如何使用YAML文件在SpringBoot中配置Swagger?【英文标题】:HowtoconfigureSwaggerinSpringBootusingYAMLfile?【发布时间】:2018-11-2912:41:31【问题描述】:我的Swagger配置有问题。现在Swagger正在扫描包并创建漂亮的文档,但我想使用我的YAML(... 查看详情

spring boot 配置多个yaml文件并根据env参数加载

】springboot配置多个yaml文件并根据env参数加载【英文标题】:springbootconfiguringmutipleyamlfilesandloadthembasedonenvparam【发布时间】:2021-06-0306:21:45【问题描述】:我必须调整一个springboot应用程序,该应用程序具有配置应用程序属性的app... 查看详情

如何通过 yaml 配置文件为 spring 批处理设置隔离级别创建

】如何通过yaml配置文件为spring批处理设置隔离级别创建【英文标题】:HowcanIsetisolation-level-for-createviayamlconfigurationfileforspringbatch【发布时间】:2022-01-1917:13:29【问题描述】:我在这里找到了通过Java代码执行此操作的解决方案:ht... 查看详情

Spring Boot Mongodb .yaml 配置

】SpringBootMongodb.yaml配置【英文标题】:SpringbootMongoDB.ymlconfiguration【发布时间】:2019-10-1517:24:21【问题描述】:当我使用MySQL和hibernate进行SpringBoot时,我在.yml文件中使用以下配置spring:datasource:url:jdbc:mysql://localhost/userName?zeroDateTime... 查看详情

使用 Gradle 管理 Spring Boot 配置文件

】使用Gradle管理SpringBoot配置文件【英文标题】:ManageSpringBootProfileswithGradle【发布时间】:2021-05-0900:57:46【问题描述】:我有一个由Gradle管理的SpringBoot应用程序。到目前为止做了什么:已创建application.yaml、application-dev.yaml和applic... 查看详情

spring配置文件(application.propertiesapplication.ymlapplication.yaml))中的配置项加载到自定义类中的方法

spring配置文件(application.properties、application.yml、application.yaml))中的配置项加载到自定义类中的方法 application.properties文件内容:util.app.name=testutil.app.version=1.0 1.通过@Value注解单个获取1@Component2publicclass 查看详情

Spring Boot YAML 配置和列表

】SpringBootYAML配置和列表【英文标题】:SpringBootYAMLconfigandlist【发布时间】:2016-01-1715:17:23【问题描述】:我必须在SpringBoot的YAML配置文件中集成一个列表,但不知道如何继续。我已经看到了其他相关的问题:SpringBootyamlconfiguratio... 查看详情

Yaml 配置对象列表到属性文件

...将以下用于SAML2安全性的yaml配置转换为等效的属性文件。spring:security:saml2:relyingparty:registration:xyz:signing:credentials:-certificat 查看详情

springboot项目中propertysource读取yaml配置文件(代码片段)

  springboot项目中,当我们使用@Value注解读取配置属性,默认的配置文件是properties类型文件,如果一些配置来自yaml格式配置文件,那么就需要做一个配置。PropertySource注解提供了factory属性,可以设置yaml格... 查看详情

基于spring boot读取配置yaml hashmap

】基于springboot读取配置yamlhashmap【英文标题】:Springbootreadingconfigurationyamlhashmapbased【发布时间】:2020-05-3118:54:56【问题描述】:是否可以在SpringBoot中使用@configurationproperties读取yaml文件?app:X1:key1:value1key2:value2X2:key1:value3key2:value4... 查看详情

springboot之spring.profiles

...elopment.yaml如production环境,则可配置application-production.yamleg:spring.profiles.active=local可以在本地创建一个application-default.yaml的文件,springboot提供了读取defaultprofile的特性我们先看一下在application-local配置文件中问题来了,怎样在类中访... 查看详情

Spring Boot MockMVC 测试不加载 Yaml 文件

】SpringBootMockMVC测试不加载Yaml文件【英文标题】:SpringBootMockMVCTestdoesnotloadYamlfile【发布时间】:2015-11-0607:52:49【问题描述】:我的配置位于类路径根目录(src/main/resources/)的application.yml文件中。当我正常启动应用程序时,配置加... 查看详情

springboot的配置文件有哪几种格式?

SpringBoot中的配置文件主要有三种格式,properties、yaml、和xml方式。-其中properties格式配置文件后缀是.properties,配置项为:server.port=9090-yaml格式配置文件后缀是.yml,配置项是:server.port:9090在SpringBoot中,使用最广泛的配置文件是ya... 查看详情

yaml的简单说明与使用

...aml作为属性配置文件,需要将SnakeYAML库添加到classpath下,SpringBoot对SnakeYAML库也做了集成,例如使用spring-boot-starter-web或spring-boot-starter都会自动添加SnakeYAML库到classpath下。一个注意的点,假如application.properties与application.yaml两份文... 查看详情

Spring Boot 使用 Yaml 而不是属性文件

】SpringBoot使用Yaml而不是属性文件【英文标题】:SpringBootusingYamlinsteadofpropertiesfile【发布时间】:2017-01-2107:08:06【问题描述】:我使用SpringBoot。我想使用YAML而不是属性来编写我的配置。由于我使用spring-boot-starter,SnakeYAML库已经... 查看详情