这是我的sessionFactory配置。
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="packagesToScan">
<beans:list>
<beans:value>package</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
<beans:prop key="hibernate.show_sql">false</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url"
value="jdbc:mysql://localhost:3306/msell" />
<beans:property name="username" value="root" />
<beans:property name="password" value="" />
<beans:property name="initialSize" value="10" />
<beans:property name="maxActive" value="5" />
<beans:property name="maxWait" value="5000" />
<beans:property name="testOnBorrow" value="true"/>
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
<tx:annotation-driven mode="aspectj"
transaction-manager="transactionManager" />
<beans:bean id="sessionFactorySports"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSourceSports" />
<beans:property name="packagesToScan">
<beans:list>
<beans:value>package</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
<beans:prop key="hibernate.show_sql">false</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="dataSourceSports" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url"
value="jdbc:mysql://localhost:3306/msell_sports" />
<beans:property name="username" value="root" />
<beans:property name="password" value="" />
<beans:property name="initialSize" value="10" />
<beans:property name="maxActive" value="5" />
<beans:property name="maxWait" value="5000" />
<beans:property name="testOnBorrow" value="true"/>
</beans:bean>
<beans:bean id="transactionManagerSports"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactorySports" />
</beans:bean>
<tx:annotation-driven mode="aspectj"
transaction-manager="transactionManagerSports" />
我尝试过像下面这样注入sessionFactorySports。
@Autowired(required=false)
@Qualifier(value="sessionFactorySports")
SessionFactory sessionFactory;
但仍然只会注入原始的sessionFactory对象。配置有什么问题?为什么sessionFactorySports没有注入?
编辑1:
我尝试将第一个sessionFactory名称更改为sessionFactoryFashion.But现在我得到了#34;没有定义类型为[org.hibernate.SessionFactory]的限定bean:期望的单个匹配bean但找到2:sessionFactoryFashion, sessionFactorySports&#34;这个错误
答案 0 :(得分:1)
不应该是@Qualifier(“sessionFactorySports”)??
以下是参考资料; https://softwaredevtips.wordpress.com/2012/05/25/springhibernate-multiple-datasources/
答案 1 :(得分:1)
如果您打算使用多个数据源,那么正确的方法是拥有多个数据源而不是sessionfactory。
您可以通过使用或实现org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource来实现此目的。
你可以看看这个