在多人协作开发一个spring boot的项目的时候,不同开发者用于测试的本地服务器配置可能是不相同的,例如:开发者A本地数据库的密码是123456 B开发者本地数据库的密码是root,如果他们修改的是同一个application.yaml的话,代码就会起冲突。
想知道能不能单独的写一份自己的数据库配置,这份配置添加到.gitignore中,不会上传到git上,然后spring boot连接数据库时判断有没有这样一份配置,如果有的话优先使用这份配置。
在多人协作开发一个spring boot的项目的时候,不同开发者用于测试的本地服务器配置可能是不相同的,例如:开发者A本地数据库的密码是123456 B开发者本地数据库的密码是root,如果他们修改的是同一个application.yaml的话,代码就会起冲突。
想知道能不能单独的写一份自己的数据库配置,这份配置添加到.gitignore中,不会上传到git上,然后spring boot连接数据库时判断有没有这样一份配置,如果有的话优先使用这份配置。
java -jar my_api.jar --spring.profiles.active=xxx
可以使用多个 yml文件来配置
application-dev.yml
使用 spring.profiles.active
application.yml
spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
4 回答1.2k 阅读✓ 已解决
4 回答1.2k 阅读✓ 已解决
1 回答2.5k 阅读✓ 已解决
1 回答976 阅读✓ 已解决
2 回答705 阅读✓ 已解决
2 回答1.7k 阅读
2 回答1.6k 阅读
利用
profiles
只能说确实能达到这样的目的。实际上 spring-boot 加载配置会查找几个目录:
Config locations are searched in reverse order. By default, the configured locations are
classpath:/
,classpath:/config/
,file:./
,file:./config/
. The resulting search order is the following:加载顺序是反过来的,所以你只需要在
workDir
中创建相同的配置文件即可覆盖classpath
目录中相同的配置。如上图 config 目录中的配置会覆盖 classpath 中的配置,并且这个配置无需使用版本控制器管理,可以实现完全的定制化并且无任何冲突。