私の春のアプリケーションでは、アプリケーション設定で設定された値に基づいて複数のデータソースを動的に初期化する必要があります。 spring jdbcライブラリで提供されているAbstractRoutingDataSourceクラスを認識していますが、一度に1つのルックアップキー値に基づいて単一のデータソースを初期化する必要がある場合にのみ役立ちます。Springで複数のデータソースを動的に初期化する
AbstractRoutingDataSourceを拡張し、複数のキーのルックアップとデータソースの解決をサポートするようにその動作を変更できますか?他の代替アプローチはありますか?
Referenceは基本的に私はAbstractDataSourceRouterクラスを介して、このような何かを達成しようとしています:
public class DataSourceRouter extends AbstractRoutingDataSource {
@Value("${com.listdb.datasource.switch}")
private short listDBSwitch;
@Value("${com.scoringdb.datasource.switch}")
private short scoringDbSwitch;
@Value("${com.configmaster.datasource.switch}")
private short configDbSwitch;
private List<String> configuredDataSources;
/**
* Determine the current lookup key. This will typically be
* implemented to check a thread-bound transaction context.
* <p>Allows for arbitrary keys. The returned key needs
* to match the stored lookup key type, as resolved by the
* {@link #resolveSpecifiedLookupKey} method.
*/
@Override
protected Object determineCurrentLookupKey() {
if(ListUtil.isListNotEmpty(configuredDataSources)) {
configuredDataSources =new ArrayList<String>();
String listDBString = (listDBSwitch == 1)?DataSources.LIST.toString() : null;
String configDBString = (configDbSwitch == 1) ? DataSources.CONFIGMASTER.toString() :null;
String scoringDBString = (scoringDbSwitch == 1) ? DataSources.SCORING.toString() : null;
/**
* Add all configured data source keys for look up
*/
configuredDataSources.add(listDBString);
configuredDataSources.add(configDBString);
configuredDataSources.add(scoringDBString);
}
return configuredDataSources;
}
}
すべてのヘルプ/提案を?