spring多数据源的配置。希望大家指正

阅读更多
像这样配置是否支持两个数据源,现在项目要连接两个数据源,是否有更好的方法
代码



	
		
			springwork/core/aop/targetsource/init.properties
		
	
	
	
		
			${datasource.driverClassName}
		
		
			${datasource.url}
		
		
			${datasource.username}
		
		
			${datasource.password}
		
	
	
	
		
			${datasource.driverClassName}
		
		
			${datasource.url1}
		
		
			${datasource.username}
		
		
			${datasource.password}
		
	
	
	
		
			
		
	
	
		
			
		
	
	
		
			
		
		
			
				springwork/core/aop/targetsource/Cat.hbm.xml
			
		
		
			
				 ${hibernate.dialect} 
				true
				true
				
					net.sf.ehcache.hibernate.SingletonEhCacheProvider
			
		
	
	
	
		
			
		
	
	

	
		
			
		
	

	
	
	
	
		
			
		
		
			
				PROPAGATION_REQUIRED,readOnly
				PROPAGATION_REQUIRED
				PROPAGATION_REQUIRED
			
		
	
	
		
			true
		
		
			
				transactionInterceptor
			
		
		
			
				catDAO
			
		
	





代码
	public void testWithTwoDataSource()
	{
		Cat cat=new Cat("mimi");
		catDAO.save(cat);
		Assert.assertNotNull(cat.getId());
		Cat cat2=new Cat("xixi");
		HotSwappableTargetSource swapper=(HotSwappableTargetSource) application.getBean("swappableDataSource");
		DataSource newDataSource=(DataSource)application.getBean("dataSource1");
		swapper.swap(newDataSource);
		catDAO.save(cat2);
		Assert.assertNotNull(cat2.getId());
	}

你可能感兴趣的:(Spring,数据结构,Hibernate,AOP,Bean)