0

Hibernate で Spring MVC を使用しています。2 つの異なるデータベース (2 つの異なるサーバー上) を使用しようとしています。1 つは MySQL で、もう 1 つは DB2 です。

これが私のdispatcher-context.xmlです

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- Load JDBC Properties -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!-- Enable Annotations (MVC and Hibernate Transactions) -->
    <context:component-scan base-package="com.example" />
    <mvc:annotation-driven />
    <tx:annotation-driven transaction-manager="hibernateTransactionManager" />

    <!-- Tell Spring where to find static files and not use a controller -->
    <mvc:resources mapping="/css/**" location="/WEB-INF/css/*" />
    <mvc:resources mapping="/js/**" location="/WEB-INF/js/*" />

    <!-- Create a ViewResolver -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- Create a DataSource for MySQL -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${mysql.database.driver}" />
        <property name="url" value="${mysql.database.url}" />
        <property name="username" value="${mysql.database.user}" />
        <property name="password" value="${mysql.database.password}" />
    </bean>

    <!-- Create a DataSource for DB2 -->
    <bean id="db2DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db2.database.driver}" />
        <property name="url" value="${db2.database.url}" />
        <property name="username" value="${db2.database.user}" />
        <property name="password" value="${db2.database.password}" />
    </bean>

    <!-- Create a Session Factory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.example.model.entities.User</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${mysql.hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${mysql.hibernate.show_sql}</prop>
                <prop key="hibernate.c3p0.min_size">${mysql.hibernate.c3p0.min_size}</prop>
                <prop key="hibernate.c3p0.max_size">${mysql.hibernate.c3p0.max_size}</prop>
                <prop key="hibernate.c3p0.timeout">${mysql.hibernate.c3p0.timeout}</prop>
                <prop key="hibernate.c3p0.max_statements">${mysql.hibernate.c3p0.max_statements}</prop>
                <prop key="hibernate.c3p0.idle_test_period">${mysql.hibernate.c3p0.idle_test_period}</prop>
            </props>
        </property>
    </bean>

    <bean id="sessionFactoryDB2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="db2DataSource" ref="db2DataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.example.model.entities.Transfer</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${db2.hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${db2.hibernate.show_sql}</prop>
                <prop key="hibernate.c3p0.min_size">${db2.hibernate.c3p0.min_size}</prop>
                <prop key="hibernate.c3p0.max_size">${db2.hibernate.c3p0.max_size}</prop>
                <prop key="hibernate.c3p0.timeout">${db2.hibernate.c3p0.timeout}</prop>
                <prop key="hibernate.c3p0.max_statements">${db2.hibernate.c3p0.max_statements}</prop>
                <prop key="hibernate.c3p0.idle_test_period">${db2.hibernate.c3p0.idle_test_period}</prop>
            </props>
        </property>
    </bean>



    <!-- Create a Transaction Manager -->
    <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
        <property name="sessionFactoryDB2" ref="sessionFactoryDB2" />
    </bean>

</beans>

しかし、実行すると次のエラーが発生します。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transfersController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.example.model.service.UserService....

コントローラー/モデルで @Autowired を使用しています。したがって、自動配線に使用する Bean がわからないのは理にかなっています。しかし、どうすればこれを解決できますか?

ありがとう

4

2 に答える 2

1
    <bean id="sessionFactoryDB2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="db2DataSource" ref="db2DataSource" />
    <property name="annotatedClasses">

db2DataSourceで名前が付けられたプロパティはないと思いますAnnotationSessionFactoryBean。それが失敗している理由の1つかもしれません。<property name="dataSource" ref="db2DataSource" />代わりにすべきです。

于 2012-09-18T21:36:09.377 に答える
1

あなたのデータソースには問題はないと思います。サービス クラスを自動配線するには、@Serviceステレオタイプ アノテーションを使用する必要があります (サービス レイヤー クラスに推奨)。

また、投稿されたxmlには表示されなかったコンポーネントスキャンを使用して、これらの注釈付きクラスをスキャンする必要があります。

 <context:component-scan base-package="com.example, com.example.model.service" />
于 2012-09-18T20:54:12.500 に答える