Java 如何使用 Spring Boot 提供不同的数据库配置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28007686/
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
How can I provide different database configurations with Spring Boot?
提问by xenoterracide
As I currently see it I have 5 possible database profiles
正如我目前看到的,我有 5 个可能的数据库配置文件
- CI testing -> h2 mem
- developer environment (could be test or app run) -> h2 mem, or h2 file, or postgres
- production -> postgres (ideally credentials not stored in the git/war)
- CI 测试 -> h2 mem
- 开发人员环境(可以是测试或应用程序运行)-> h2 mem,或 h2 文件,或 postgres
- 生产 -> postgres(理想情况下,凭证不存储在 git/war 中)
currently I have postgres configured for running the application, and h2 configured for testing via having a different application.properties
in java/resource
s vs test/resources
目前我已经配置了用于运行应用程序的 postgres,并且配置了 h2 用于通过application.properties
在java/resource
s 与test/resources
what's the simplest way to have the database connection information change for these scenarios?
对于这些场景,更改数据库连接信息的最简单方法是什么?
采纳答案by Bohuslav Burghardt
As M. Deinum mentions in his comment, the simplest way to do this is to use profile specific configuration.
正如 M. Deinum 在他的评论中提到的,最简单的方法是使用profile specific configuration。
Spring Boot allows you to have one common configuration file (application.properties
) and then multiple other files, each specific to a profile (application-${profile}.properties
).
Spring Boot 允许您拥有一个通用配置文件 ( application.properties
) 和多个其他文件,每个文件都特定于一个配置文件 ( application-${profile}.properties
)。
For instance:
例如:
application.properties
- Common configurationapplication-dev.properties
- Configuration for dev profileapplication-ci.properties
- Configuration for ci profiles
application.properties
- 通用配置application-dev.properties
- 开发配置文件的配置application-ci.properties
- ci 配置文件的配置
If your application runs with "ci" profile for instance, the default configuration file as well as the ci configuration file (which would contain the datasource configuration properties for ci profile) will be loaded.
例如,如果您的应用程序使用“ci”配置文件运行,则将加载默认配置文件以及 ci 配置文件(其中将包含 ci 配置文件的数据源配置属性)。
To switch profiles you can use one of the following options:
要切换配置文件,您可以使用以下选项之一:
- JVM property:
-Dspring.profiles.active=ci
- Command line switch:
--spring.profiles.active=dev
- JVM属性:
-Dspring.profiles.active=ci
- 命令行开关:
--spring.profiles.active=dev
For unit tests you can use @ActiveProfiles("test")
annotation on your test classes to tell Spring that unit tests should be run with test profile.
对于单元测试,您可以@ActiveProfiles("test")
在测试类上使用注解来告诉 Spring 应该使用测试配置文件运行单元测试。
Also if you don't want to store production database credentials along with your source code, you can specify external configuration file when you deploy your app in production:
此外,如果您不想将生产数据库凭据与源代码一起存储,则可以在生产中部署应用程序时指定外部配置文件:
- Using command line switch:
--spring.config.location=/srv/myapp/config.properties
- Using a JVM property:
-Dspring.config.location=/srv/myapp/config.properties
- 使用命令行开关:
--spring.config.location=/srv/myapp/config.properties
- 使用 JVM 属性:
-Dspring.config.location=/srv/myapp/config.properties
回答by Santosh Anantharamaiah
Compact answer for the above scenario would be by creating a single application.yml file and creating different profiles based on the requirement, in your case -dev, -ci and -prod and providing the DB information accordingly.
上述情况的紧凑答案是通过创建单个 application.yml 文件并根据需求创建不同的配置文件,在您的情况下 -dev、-ci 和 -prod 并相应地提供数据库信息。
Sample example is:
示例示例是:
spring:
profiles.active: development
---
spring:
profiles: development
datasource:
db-person:
url: jdbc:oracle:thin:@db_person_dev
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
db-contract:
url: jdbc:oracle:thin:@db_contract_dev
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
---
spring:
profiles: test
datasource:
db-person:
url: jdbc:oracle:thin:@db_person_test
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
db-contract:
url: jdbc:oracle:thin:@db_contract_test
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
---
spring:
profiles: production
datasource:
db-person:
url: jdbc:oracle:thin:@db_person_prod
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
db-contract:
url: jdbc:oracle:thin:@db_contract_prod
username: username
password: pwd
driver-class-name: oracle.jdbc.OracleDriver
test-on-borrow: true
validation-query: SELECT 1 FROM dual
---
For further understanding and simple example you can refer this link.
为了进一步理解和简单的例子,你可以参考这个链接。