java Spring 将一个 application.properties 用于生产,另一个用于调试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34845990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring use one application.properties for production and another for debug
提问by Prichmp
I have a Spring application and I would like to be able to switch between configurations depending if I'm debugging the server or if the server is running in production. (the difference in configurations being things like database location.)
我有一个 Spring 应用程序,我希望能够在配置之间切换,具体取决于我是在调试服务器还是在生产中运行服务器。(配置的不同之处在于数据库位置之类的东西。)
Ideally, I'd like to pass in a command line argument to my Spring application on boot-up and set the application configuration.
理想情况下,我想在启动时将命令行参数传递给我的 Spring 应用程序并设置应用程序配置。
I have two separate application.properties files, one with the production values, and another with the debug values. How can I switch between the two of them?
我有两个单独的 application.properties 文件,一个是生产值,另一个是调试值。如何在两者之间切换?
回答by diyoda_
You can have 3 properties files, application-dev.properties
, application-prod.properties
and application.properties
. And you can specify all the development properties in your devproperties file and production cionfiguration properties in your prodfile
您可以拥有 3 个属性文件application-dev.properties
、application-prod.properties
和application.properties
. 你可以指定你的所有开发物业开发中的属性文件和生产cionfiguration性能督促文件
and specify the profile in your application.properties
files as below
并在您的application.properties
文件中指定配置文件,如下所示
spring.profiles.active=dev
or you can select/override the profile using -Dprofile=
argument in command line.
或者您可以使用-Dprofile=
命令行中的参数选择/覆盖配置文件。
回答by Maarten Brak
Spring profiles seem the way to go. You can start your application with something like -Dprofile=. Have a look at this example.
Spring 配置文件似乎是要走的路。您可以使用 -Dprofile= 之类的内容启动您的应用程序。看看这个例子。
EDIT: after re-reading your question, I came to the conclusion that you might actually want something more basic: put your database properties externally. Depending on your application you could use @Value of a property configurator. Have a look at the spring docs.
编辑:在重新阅读您的问题后,我得出的结论是您可能实际上想要更基本的东西:将您的数据库属性放在外部。根据您的应用程序,您可以使用属性配置器的 @Value。看看 spring 文档。