springboot的注入参数很强大,可以把配置文件中的参数在类中作为占位符就行多重使用。
比如
我定义一个active=dev
ctive=dev
#dev
#read database
dev.read.datasource.url=jdbc:mysql://rm-bp1cr63v22fwwqe5nqr6a4.mysql.rds.aliyuncs.com/ewe-hr
dev.read.datasource.username=eqq
dev.read.datasource.password=1qaqqz_2WSX
#write database
dev.write.datasource.url=jdbc:mysql://rm-bp1cr63v22f5nr6a4.mysql.rds.aliyuncs.com/ewe-hr
dev.write.datasource.username=qeq
dev.write.datasource.password=1qqqqqaz_2WSX
#test
#read database
test.read.datasource.url=jdbc:mysql://localhost/ewe-hr
test.read.datasource.username=123q
test.read.datasource.password=1qazasa_2WSX
#write database
test.write.datasource.url=jdbc:mysql://localhost/ewe-hr
test.write.datasource.username=11we
test.write.datasource.password=aseasqaz_2WSqwX
我在运行项目jar的时候要把active改变test原本是dev
java -jar 项目名.jar --active=test
在javabean中也通过value注入的时候
可以使用如下
@Value("${${active}.read.datasource.url}")
private String readUrl;
@Value("${${active}.read.datasource.username}")
private String readUserName;
@Value("${${active}.read.datasource.password}")
private String readPwd;
@Value("${${active}.write.datasource.url}")
private String writeUrl;
@Value("${${active}.write.datasource.username}")
private String writeUserName;
@Value("${${active}.write.datasource.password}")
private String writePwd;
使用$ 再加$进行参数的动态注入,让我在同一个配置文件 使用不同的配置信息