关于 HotSwappableTargetSource 用法的疑问!
因为公司项目要求登陆时能切换数据源,所以使用了HotSwappableTargetSource这个类,现在问题是根本没办法切换数据源,不知道错在哪了,各位帮我看看:
配置文件:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${datasource.driverClassName}</value>
</property>
<property name="url">
<value>${datasource.url}</value>
</property>
<property name="username">
<value>${datasource.username}</value>
</property>
<property name="password">
<value>${datasource.password}</value>
</property>
</bean>
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${datasource.driverClassName}</value>
</property>
<property name="url">
<value>${datasource.url1}</value>
</property>
<property name="username">
<value>${datasource.username}</value>
</property>
<property name="password">
<value>${datasource.password}</value>
</property>
</bean>
<bean id="swappableDataSource" class="org.springframework.aop.target.HotSwappableTargetSource">
<constructor-arg>
<ref local="dataSource"/>
</constructor-arg>
</bean>
<bean id="swappable" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetSource">
<ref local="swappableDataSource"/>
</property>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="com.strongit.finance.common.support.HibernateHelpSessionFactoryBean" depends-on="hibernateMappingFileSetUtil">
<!--<property name="dataSource">
<ref bean="dataSource"/>
</property>-->
<property name="dataSource">
<ref local="swappable"/>
</property>
<property name="mappingResources">
<!-- 收集所有系统的hibernate配置文件-->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="hibernateMappingFileSetUtil"/>
</property>
<property name="targetMethod">
<value>getLists</value>
</property>
</bean>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.provider_class">net.sf.hibernate.cache.OSCacheProvider</prop>
<prop key="hibernate.query.substitutions">true 1,false 0</prop>
<prop key="hibernate.session_factory_name">hibernate.sessionFactory</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.jdbc.use_streams_for_binary">true</prop>
<prop key="hibernate.jdbc.fetch_size">100</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="hibernate.max_fetch_depth">2</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="jndiName">
<value>hibernate/sessionFactory</value>
</property>
</bean>
代码:
ApplicationContext application = new FileSystemXmlApplicationContext(
"classpath:META-INF/finance/config/applicationContext-datasource.xml");
HotSwappableTargetSource swapper = (HotSwappableTargetSource)application.getBean("swappableDataSource");
DataSource newDataSource = (DataSource)application.getBean("dataSource1");
logger.info("newDataSource:"+newDataSource.toString());
swapper.swap(newDataSource);
info = (Info) bean;
infoManager.addInfo(info);
现在进行数据源切换后数据还是保存到了第一个dataSource里面,百思不得其解!!