2012-03-19 21 views
1

Hibernate using multiple databasesで質問されました。私の問題は似ていますが、別の問題に直面しています.2つのXMLファイルを作成し、それぞれに別々のデータソースとセッションファクトリを作成しました。私はプロジェクトを実行すると、私は、様々な負荷とバインディングはXML files.Theさまざまな注釈およびデータベース/テーブルの両方から行われているが、適切にちょうどこの後identified.Butありき2つのセッションファクトリが予想されたときに見つかりました

<context-param> 
<param-name>contextConfigLocation</param-name> 
    <param-value>*The xml files* </param-value> 

私のweb.xmlに コントロールが外に出る前に行われます。次のエラーが表示されます。

main ERROR [org.springframework.web.context.ContextLoader] - Context initialization failed 
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'daoEager' defined in URL [jar:file:/C:/Users/.../.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/infobutton-service/WEB-INF/lib/core-data-1.0.0-SNAPSHOT.jar!/.../DaoHibernateEagerImpl.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [sessionFactory, profilesessionFactory]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [sessionFactory, profilesessionFactory] 

クラスDaoHibernateEagerImplが

@Implementation 
@Repository("daoEager") 
public class DaoHibernateEagerImpl extends DaoHibernateImpl 
{ 
// ========================= CONSTANTS ================================= 

/** 
* A logger that helps identify this class' printouts. 
*/ 
private static final Logger log = getLogger(DaoHibernateEagerImpl.class); 

// ========================= CONSTRUCTORS ============================== 

/** 
* Required for a Spring DAO bean. 
* 
* @param sessionFactory 
*   Hibernate session factory 
*/ 
@Autowired 
public DaoHibernateEagerImpl(final SessionFactory sessionFactory) 
{ 
    super(sessionFactory); 
} 

// ========================= DEPENDENCIES ============================== 

// ========================= IMPLEMENTATION: Dao ======================= 



// ========================= IMPLEMENTATION: SearchEngine ============== 


} 

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:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:jaxws="http://cxf.apache.org/jaxws" 
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/aop 
http://www.springframework.org/schema/aop/spring-aop-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/jee 
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> 

    <context:annotation-config /> 

<!-- 
    Data source: reads a properties file and injects them into a DBCP DS 
    Second datasource for Resource Profiles 
--> 
<bean id="profiledataSource" 
    class=".....ConfigurableBasicDataSource"> 
    <constructor-arg index="0"> 
     <bean class="org.apache.commons.dbcp.BasicDataSource" /> 
    </constructor-arg> 
    <property name="properties"> 
     <bean 

    class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
      <property name="locations"> 
       <list> 
        <value>WEB-INF/datasource-local.properties 
        </value> 
       </list> 
      </property> 
     </bean> 
    </property> 

    <!-- FUR-946: idle connections break. Adding connection testing. --> 
    <property name="testOnBorrow" value="true" /> 
    <property name="testWhileIdle" value="true" /> 
</bean> 




    <!-- Session factory --> 
<!-- Session Factory for the second datasource--> 
<bean id="profilesessionFactory" 


    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="profiledataSource" /> 

    <!-- 
     Hibernate configuration properties (read from a properties file) 
    --> 
    <property name="hibernateProperties"> 
     <bean 

    class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
      <property name="locations"> 
       <list> 
        <value>WEB-INF/hibernate.properties 
        </value> 
        <value>WEB-INF/datasource-local.properties 
        </value> 

       </list> 
      </property> 
     </bean> 
    </property> 

    <!-- Using improved naming strategy --> 
    <property name="namingStrategy"> 
     <bean class="org.hibernate.cfg.DefaultNamingStrategy" /> 
    </property> 




    <!-- Mapping annotated classes using search patterns --> 
    <property name="annotatedClasses"> 
     <list> 
      <value><![CDATA[....profiledb.domain.Profiles]]></value> 
     </list> 
    </property> 
</bean> 

<!-- Hibernate data access template --> 
<bean id="profilehibernateTemplate"  
    class="org.springframework.orm.hibernate3.HibernateTemplate"> 

    <property name="sessionFactory" ref="profilesessionFactory" /> 
</bean> 


<tx:annotation-driven /> 

<!-- a PlatformTransactionManager is still required --> 
<bean id="profiletransactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="profilesessionFactory" /> 
</bean> 


<bean id="profilesdbDao" class="....profiledb.service.ProfilesDaoImpl" > 
    <property name="sessionFactory" ref="profilesessionFactory"></property> 

<context:component-scan base-package="....core.data" /> 

です

他のxmlファイルはかなり明確さと同様であるが、異なるデータソースを持っており、セッションファクトリは

<bean id="sessionFactory" 
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
..... 
+0

Bean定義xmlと、この例外に列挙されているクラス(DaoHibernateEagerImpl)を入力してください。 – raddykrish

+0

私はあなたが示唆したようにいくつかの詳細を追加しました。 – icedek

答えて

4

エラーメッセージです:理解

expected single matching bean but found 2: [sessionFactory, profilesessionFactory]

<bean id="profilesessionFactory" 
<!-- ... --> 
<bean id="sessionFactory" 

修正は簡単です:

@Autowired 
public DaoHibernateEagerImpl(
    @Qualifier("profilesessionFactory") final SessionFactory sessionFactory) 

DaoHibernateEagerImplクラスを変更できない場合は、いつでもXMLベースの構成にフォールバックできます。最初にDaoHibernateEagerImplのCLASSPATHスキャンを無効にして、@Autowiredが取得されないようにします。そして、単に書く:

<bean class="DaoHibernateEagerImpl"> 
    <constructor-arg ref="profilesessionFactory"/> 
    <!-- ... --> 
</bean> 

最後に、あなたは@Primary注釈/ primary="true"ディレクティブを利用することができます。オートワイヤリングし、代わりに例外を投げるの二つの可能性がある場合

<bean id="sessionFactory" primary="true"> 
    <!-- ... --> 

これはsessionFactoryを好むでしょう。

+1

ありがとうございます。しかし、DaoHibernateEagerImplを変更せずにこれを行う方法はありますか?これは、私がmavenの依存関係としてそのクラスを使用しているからです。残りはプロジェクト固有ですが、その特定のクラスは私によって変更できません。 ありがとう – icedek

+0

また、 "sessionFactory"はデフォルトのセッションファクトリであり、 "profilesessionFactory"は特定のdaoimplのセッションファクトリでなければなりません。 – icedek

+0

@AdityaKalluri:私の更新を見て、多分あなたを導くでしょう。あなたが述べたように –

0

そのため、@Primary(注釈)/ primary = "true"(xml config)は言及していませんが、Hiibernateはどのsessionfactoryを選択するかわかりません。その場合、エラーが発生しますと述べた。 @Primaryアノテーションを試してみてください。

関連する問題