Spring多数据源配置
Catalog so that it can dynamically get connections from the 3 different databases at runtime based on the current customer's type. As I mentioned, the AbstractRoutingDataSource can be rather simple to implement. Here is my implementation:
? ?private Catalog catalog;
? ?public void setCatalog(Catalog catalog) {
? ? ? this.catalog = catalog;
? ?}
? ?public void testDataSourceRouting() {
? ? ? CustomerContextHolder.setCustomerType(CustomerType.GOLD);
? ? ? List<Item> goldItems = catalog.getItems();
? ? ? assertEquals(3, goldItems.size());
? ? ? System.out.println("gold items: " + goldItems);
? ? ? CustomerContextHolder.setCustomerType(CustomerType.SILVER);
? ? ? List<Item> silverItems = catalog.getItems();
? ? ? assertEquals(2, silverItems.size());
? ? ? System.out.println("silver items: " + silverItems);
? ? ? ?
? ? ? CustomerContextHolder.clearCustomerType();
? ? ? List<Item> bronzeItems = catalog.getItems();
? ? ? assertEquals(1, bronzeItems.size());
? ? ? System.out.println("bronze items: " + bronzeItems);? ? ? ?? ? ? ?
? ?}
? ?protected String[] getConfigLocations() {
? ? ? return new String[] {"/blog/datasource/beans.xml"};
? ?}? ?
}