单个弹簧轮廓的多个属性文件 [英] Multiple properties file for a single spring profile

查看:122
本文介绍了单个弹簧轮廓的多个属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用弹簧靴2.0.0.我们有三个环境devstagingproduction.我们当前的配置结构

dev

application-dev.yml
application-dev.properties

同样,对于每个环境,我们都有一个ymlproperties文件.经过一年的发展,现在,配置文件的单个yml文件成为一个大型的整体配置.

是否可以为以下配置文件提供多个配置文件?

application-dev.yml
application-dev-sqs.yml
application-dev-redis.yml

解决方案

我正在处理类似的问题,因此我建议使用yaml配置.

让我们描述.properties文件:

初始方法

一个人可以这样使用它:

@Component
@PropertySources({
    @PropertySource("classpath:application.properties"),
    @PropertySource("classpath:application-${spring.profiles.active}.properties")
})
public class AppProperties {
}

这很容易配置.限制是,您不能合并配置文件.我的意思是,当您要将配置文件用作dev,local时,其中local只是更改了dev配置文件的某些配置属性,Spring会尝试加载application-dev,local.properties文件,这很可能不是您想要的.

顺便说一句,这是Spring会自动为您执行的操作,这对于您所描述的主题很有用.

无法按配置文件(并非针对整个列表)配置它.另一种可能是,可以在spring.config.name中指定列表,目前情况并非如此.

更好的方法

简而言之,使用:

@Profile("dev")
@Configuration
@PropertySources({
        @PropertySource("classpath:topic1-dev.properties"),
        @PropertySource("classpath:topic2-dev.properties")
})
public class AppPropertiesDev {
}

缺点是,您必须具有多个这样的配置类(dev,staging),但是知道您有主题.另外,您也可以使用多个配置文件,根据我的测试,这些配置文件是( a>)按您指定的顺序加载.这样,您的开发人员就可以轻松使用开发人员配置并更改其测试所需的内容.

Yaml方法

您可以看到我之前问过的使用Yaml的方法-解析多个Spring配置文件(yaml配置)的属性,好处是文件数量更少-yaml将所有配置文件都放在一个文件中,这可能是您想要的,也可能不是您想要的.

We are using spring boot 2.0.0. We have three environments dev, staging, production. Our current config structure

dev

application-dev.yml
application-dev.properties

Likewise, we have a yml and properties file for each environment. After a year of development now the single yml file for a profile become a large monolithic config.

is it possible to have a multiple config files for a profile like below?

application-dev.yml
application-dev-sqs.yml
application-dev-redis.yml

解决方案

I was dealing with a similar problem and I'd recommend using yaml configuration.

Let's describe .properties file:

Initital approach

One can use it like this:

@Component
@PropertySources({
    @PropertySource("classpath:application.properties"),
    @PropertySource("classpath:application-${spring.profiles.active}.properties")
})
public class AppProperties {
}

This is very easy to configure. Limitation is, that you cannot combine profiles. I mean, that when you want to use profile as dev,local where local just alters some config properties for dev profile, Spring will try to load application-dev,local.properties file, which is very likely not what you want.

Btw, this is what Spring will do for you automatically, this is useful for topics as you described.

There is no way to configure it per profile (and not for whole list). Other possibility would be, that one can specify the list in spring.config.name which is not the case at the moment.

Better approach

In short, use:

@Profile("dev")
@Configuration
@PropertySources({
        @PropertySource("classpath:topic1-dev.properties"),
        @PropertySource("classpath:topic2-dev.properties")
})
public class AppPropertiesDev {
}

Disadvantage is, you have to have several such config classes (dev, staging), but know you have the topics. Also you can use mutliple profiles, which are (as of my testing) loaded in order you specified. That way, your developer can easily use dev configuration and alter just what's needed for his/her testing.

Yaml approach

You can see the approach with yaml in question I asked earlier - Property resolving for multiple Spring profiles (yaml configuration), benefit is smaller amount of files - yaml has all the profiles in one file, which may or may not be what you want.

这篇关于单个弹簧轮廓的多个属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆