@primary
@bean
@configurationproperties(prefix = "spring.seconddatasource")
public datasource datasource() {
return datasourcebuilder.create().build();
}
@bean(name = "secoddatasource")
@configurationproperties(prefix = "spring.seconddatasource")
public datasource secoddatasource() {
return datasourcebuilder.create().build();
}
@primary
@bean
public jdbctemplate jdbctemplate(datasource datasource) {
return new jdbctemplate(datasource);
}
@bean(name = "secondjdbctemplate")
public jdbctemplate secondjdbctemplate(@qualifier(value = "secoddatasource") datasource datasource) {
return new jdbctemplate(datasource);
}
来创建两个不同的jdbctemplate,到这里老版本这样干没有啥问题,能够正常的得到数据;而升级未v2.03版本的时候提示:异常:jdbcurl is required with driverclassname.
很显然配置节点不能使用导致的,配置节点名字变了,要解决这问题这里使用了托管datasourceproperties的方式来对数据配置从新赋值,具体代码如:
@bean
@primary
@configurationproperties(prefix = "spring.datasource")
public datasourceproperties datasourceproperties(){
return new datasourceproperties();
}
@bean("secondproperties")
@configurationproperties(prefix = "spring.seconddatasource")
public datasourceproperties secondproperties(){
return new datasourceproperties();
}
@primary
@bean
public datasource datasource(datasourceproperties datasourceproperties) {
return datasourceproperties.initializedatasourcebuilder().build();
}
@bean(name = "secoddatasource")
public datasource secoddatasource(@qualifier(value = "secondproperties") datasourceproperties datasourceproperties) {
return datasourceproperties.initializedatasourcebuilder().build();
}