如何在Spring-Boot生产期间覆盖application.properties? [英] How to override application.properties during production in Spring-Boot?

查看:368
本文介绍了如何在Spring-Boot生产期间覆盖application.properties?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring boot和 application.properties 在开发期间通过 @Configuration @Profile(dev)选择数据库

I'm using spring boot and application.properties to select a database during development by @Configuration @Profile("dev").

spring.profiles.active=dev
spring.config.location=file:d:/application.properties

在制作期间,我想在应用程序上下文之外创建一个文件应加载,然后激活不同的配置文件,使用d:/application.properties:

During production I'd like to create a file outside of the application context that should be loaded and then active a different configuration profile, with d:/application.properties:

spring.profiles.active=production

结果:当我启动应用程序时,配置仍然是 dev ,因此不会考虑生产属性文件的其他位置。我错过了什么吗?

Result: when I start the app, the configuration is still dev, so somehow the additional location of the productive properties file is not taken into account. Am I missing anything?

spring boot 1.1.0.BUILD-SNAPSHOT

spring boot 1.1.0.BUILD-SNAPSHOT

注意:这个问题是 NOT 关于 tomcat

Note: this question is NOT about tomcat.

推荐答案

我知道你问怎么做。

但答案是,你不应该这样做。

But the answer is, you should not do this.

你可以拥有application.properties应用程序 - default.properties application-dev.properties等

You can have application.properties application-default.properties application-dev.properties etc

您可以通过命令行参数将配置文件切换到jvm

you can switch profiles by command line args to the jvm

你可以在测试时使用@TestPropertySource覆盖一些东西

you can override some things at test time using @TestPropertySource

理想情况下,一切都应该在源代码控制中,这样就不会有任何意外 - 你怎么知道这些属性在哪里?您的服务器位置,以及缺少的位置。如果开发人员介绍新内容会发生什么。

Ideally everything should be in source control so that there are no surprises - how do you know what properties are sitting there in your server location, and which ones are missing. What happens if developers introduce new things.

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Spring boot已经为你提供了足够的方法来做到这一点。

Spring boot is already giving you enough ways to do this right.

这篇关于如何在Spring-Boot生产期间覆盖application.properties?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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