首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring多数据源配备

2012-10-08 
Spring多数据源配置Catalog so that it can dynamically get connections from the 3 different databases

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:

public class CatalogTests extends AbstractDependencyInjectionSpringContextTests {

? ?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"};
? ?}? ?
}

热点排行