开发者社区> 问答> 正文

关于 Spring引入两个数据源配置Hibernate 问题

一个项目需要连两个数据库 如下配置报错

<!-- ② 定义一个数据源 -->
    <bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <property name="url" value="jdbc:mysql://192.168.1.200/db1" />
        <property name="username" value="root" />
        <property name="password" value="1234" />
        <property name="initialSize" value="1" />
        <property name="maxActive" value="20" />
    </bean>
    <!--  定义一个db_bbs数据源 -->
    <bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <property name="url" value="jdbc:mysql://192.168.1.200/db2" />
        <property name="username" value="root" />
        <property name="password" value="1234" />
        <property name="initialSize" value="1" />
        <property name="maxActive" value="20" />
    </bean>
 
 
    <!-- 定义Hibernate的Session工厂 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource1" ref="dataSource1" />
        <property name="dataSource2" ref="dataSource2" />
        <property name="packagesToScan">
            <!-- ③-1 扫描基于JPA 注解PO类目录 -->
            <list>
                <value>com.test.model.entity</value>
            </list>
        </property>

</bean>
 
 
 <!-- ④ 定义HibernateTemplate -->
 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"
 p:sessionFactory-ref="sessionFactory" />
</beans>
@非技术专家群体:严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.orm.hibernate3.HibernateTemplate com.test.dao.BaseDaoImpl.hibernateTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [test-dao.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [test-dao.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'dateSource2' of bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Bean property 'dateSource2' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

展开
收起
爵霸 2016-03-04 14:58:30 2742 0
1 条回答
写回答
取消 提交回答
  • 没写set/get方法或没写对

    2019-07-17 18:52:19
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多