当一个人被期待时,发现了两个会议工厂

时间:2012-03-19 03:32:15

标签: java spring hibernate

我已提到Hibernate using multiple databases提出的问题。我的问题很相似,但我遇到了一个不同的问题。我创建了两个xml文件,每个文件都有一个单独的数据源和会话工厂。 在我的web.xml中我有

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

一旦我运行项目,各种加载和绑定都是从两个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" />
 .....

2 个答案:

答案 0 :(得分:4)

错误信息非常清楚:

  

预期单个匹配bean但找到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 :(得分:0)

因为......如果你不提@Primary(注释)/ primary =&#34; true&#34;(在xml配置中),Hiibernate不知道要拾取哪个sessionfactory。在这种情况下它会给你提到的错误。只需尝试使用@Primary注释即可...