2012-03-19 64 views
1

我提到的问题在Hibernate using multiple databases。我的问题是类似的,但我面临一个不同的问题。我创建了两个XML文件,每个文件都有一个单独的数据源和会话工厂。 在我的web.xml我有两个会话期间发现一个工厂

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

当我运行该项目,各种载荷和绑定同时从XML文件可以files.The各种注释和数据库/表是正确identified.But紧接在该完成之前控制甚至超出。我得到以下错误。

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 2012-03-19 06:04:20

+0

我已经按照你的建议添加了一些更多的细节。 – icedek 2012-03-19 06:44:40

回答

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的配置。首先禁用CLASSPATH扫描DaoHibernateEagerImpl,以便@Autowired未被拾取。然后简单的写:

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

最后,你可以采取@Primary注解/ primary="true"指令的优势:

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

自动装配,有两种可能性,而不是抛出一个异常时,这会更喜欢sessionFactory

+1

谢谢。但有没有什么办法可以在不修改DaoHibernateEagerImpl的情况下完成?这是因为我使用该类作为maven中的依赖项。其余项目都是特定项目,但该特定类别不能由我修改。 谢谢 – icedek 2012-03-19 14:51:34

+0

另外,“sessionFactory”应该是默认的会话工厂,“profilesessionFactory”只能是特定daoimpl的会话工厂。 – icedek 2012-03-19 15:02:38

+0

@AdityaKulluri:看到我的更新,也许它会引导你。 – 2012-03-19 22:47:06

0

因为......如果你没有提及@Primary(annotation)/ primary =“true”(在xml配置中),那么Hiibernate不知道应该选择哪个sessionfactory。在这种情况下,它会给你错误提及。只要尝试使用@Primary注释它将工作...