Spring Boot - 外化属性 [英] Spring Boot - Externalized properties

查看:59
本文介绍了Spring Boot - 外化属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Spring Boot 项目,它构建了一个可执行的战争,并在 中配置了一个 application.properties 文件和一个 application-dev.propertiessrc/main/resources.

I've got a Spring Boot project that builds an executable war and is configured with an application.properties file and a application-dev.properties in src/main/resources.

application.properties中的第一行是spring.profiles.active=dev,它触发了application-dev.properties 按预期运行时.

The first line in the application.properties is spring.profiles.active=dev, which triggers the inclusion of application-dev.properties when running as expected.

我的问题是我似乎无法用外部属性源覆盖这些属性(在一切都被震动/战争之后).为了简单的调试,我在项目的根目录中创建了一个文件 settings.properties,并在其中指定了 spring.profiles.active=dev,deploy 希望也触发部署"配置文件.

My problem is that I can't seem to override these properties with an external properties source (after everything is jarred / warred up). For simple debugging, I've created a file settings.properties in the root of my project, and specified spring.profiles.active=dev,deploy in it hoping to also trigger the "deploy" profile.

但是,当我运行以下命令时:

However, when I run the following:

java -jar target/project-0.1.0-LOCAL-NA.war --spring.config.location=file:./settings.properties

部署配置文件从未启用.我以为我可以使用以前的版本(现在运行 1.1.6.RELEASE,来自 1.0.2.RELEASE)但回滚也无济于事.

the deploy profile is never enabled. I thought I had this working with a previous version (running 1.1.6.RELEASE now, coming from 1.0.2.RELEASE) but rolling back didn't help either.

我是否遗漏了一些明显的东西?

Am I missing something obvious?

所以在调试时我发现 Environment.getActiveProfiles() 只会返回 1 个项目,即使有多个活动配置文件(在这种情况下是 dev & deploy).所以,虽然我的报告只列出了 dev,但也有 deploy.

So while debugging I discovered that Environment.getActiveProfiles() will only return 1 item, even if there's multiple active profiles (dev & deploy in this case). So, while my reporting was only listing dev, deploy was there also.

我通过使用环境变量而不是配置文件的属性文件使事情再次运行,但我想仅使用文件来设置所有内容...

I've got things running again by using environment variables rather than properties files for profiles, but I'd like to get everything setup with just the files...

推荐答案

当使用 jar 文件启动时,我们发现您不能使用-D"来注入系统变量.您必须使用--".

When starting up with a jar file we discovered that you can't use "-D" to inject system variables. You have to use "--".

假设您将应用编码为使用配置文件,这对我们有用

this worked for us assuming you coded your app to use profiles

    java -jar myapplication.jar
    --spring.profiles.active=override
    --spring.config.location=file:/path/to/file/application-override.properties

这篇关于Spring Boot - 外化属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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