在哪种情况下我需要提供spring类名作为bean id值?

时间:2015-09-26 18:17:32

标签: java spring hibernate spring-mvc jpa

我需要为DAO配置xml文件。所以在我的xml文件中,我已经声明了两个entityManager Factory,我想将其中一个设置为默认持久性单元。我在我的dao.xml

中声明了以下部分
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
    property name="defaultPersistenceUnitName" value="pumps-jpa"/>
</bean>

但是,它并没有对我有用,它没有采用默认持久性单元。我收到了这样的错误

  

没有定义类型的唯一bean:预期的单个bean但找到了2:

经过大量的搜索,我找到了一个代码片段,他们在其中提到了bean id作为spring class name,即 org.springframework.context.annotation.internalPersistenceAnnotationProcessor ,如下所示

<bean id="org.springframework.context.annotation.internalPersistenceAnnotationProcessor"
  class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
     property name="defaultPersistenceUnitName" value="pumps-jpa"/>
</bean>

因此,在提到这个bean id之后,它采用了默认的持久性单元名称。我想知道,为什么我需要提到spring class( org.springframework.context.annotation.internalPersistenceAnnotationProcessor )作为bean id?它是一种黑客还是什么?

整个dao.xml在

下面声明
<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="file:${catalina.base}/conf/pumps-dbconfig.properties"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass">
        <value>${jdbc.driver}</value>
    </property>
    <property name="jdbcUrl">
        <value>${jdbc.url}</value>
    </property>
    <property name="user">
        <value>${jdbc.user}</value>
    </property>
    <property name="password">
        <value>${jdbc.password}</value>
    </property>
    <property name="initialPoolSize"><value>10</value></property>
    <property name="minPoolSize"><value>10</value></property>
    <property name="maxPoolSize"><value>${jdbc.maxConnections}</value></property>
    <property name="maxIdleTimeExcessConnections"><value>600</value></property>
    <!-- <property name="timeout"><value>0</value></property> -->   <!-- 0 means: no timeout -->
    <property name="idleConnectionTestPeriod"><value>60</value></property>
    <property name="acquireIncrement"><value>5</value></property>
    <property name="maxStatements"><value>0</value></property>  <!-- 0 means: statement caching is turned off.  -->
    <property name="numHelperThreads"><value>3</value></property>  <!-- 3 is default -->         
    <property name="unreturnedConnectionTimeout"><value>0</value></property>
    <property name="debugUnreturnedConnectionStackTraces"><value>true</value></property>
    <property name="testConnectionOnCheckout"><value>true</value></property>
</bean>

<!--
    Configuration for Hibernate/JPA
-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="pumps-jpa" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="false" /> 
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
        </bean>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSource" />
</bean>

<!-- DEFAULT PERSISTENCE UNIT DECLARATION -->  

 <bean id="org.springframework.context.annotation.internalPersistenceAnnotationProcessor" 
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
    <property name="defaultPersistenceUnitName" value="pumps-jpa"/>   
</bean>

<bean id="r-dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass">
        <value>${r-jdbc.driver}</value>
    </property>
    <property name="jdbcUrl">
        <value>${r-jdbc.url}</value>
    </property>
    <property name="user">
        <value>${r-jdbc.user}</value>
    </property>
    <property name="password">
        <value>${r-jdbc.password}</value>
    </property>
    <property name="initialPoolSize"><value>10</value></property>
    <property name="minPoolSize"><value>10</value></property>
    <property name="maxPoolSize"><value>${r-jdbc.maxConnections}</value></property>
    <!-- <property name="timeout"><value>0</value></property> -->   <!-- 0 means: no timeout -->
    <property name="maxIdleTimeExcessConnections"><value>600</value></property>
    <property name="idleConnectionTestPeriod"><value>60</value></property>
    <property name="acquireIncrement"><value>5</value></property>
    <property name="maxStatements"><value>0</value></property>  <!-- 0 means: statement caching is turned off.  -->
    <property name="numHelperThreads"><value>3</value></property>  <!-- 3 is default -->    
    <property name="acquireRetryAttempts"><value>3</value></property>      
    <property name="testConnectionOnCheckout"><value>true</value></property>
</bean>

<!--
    Configuration for Hibernate/JPA
-->
 <bean id="r-entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="pumps-jpa" />
    <property name="dataSource" ref="r-dataSource" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="false" /> 
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
        </bean>
    </property>
</bean>

<bean id="BaseDataConnection" class="com.myntra.commons.dao.impl.BaseDataConnection">
        <property name="roEntityManagerFactory" ref="r-entityManagerFactory" />
</bean> 

1 个答案:

答案 0 :(得分:0)

我认为你原来的问题不是你的意思! 通过配置,您可以将您的某个实体管理器现有的持久性单元实现称为默认持久性单元&#39;!您可以在XML配置中执行此操作! JPA中的persistence.xml是您的持久性配置您的bean(配置)xml是另一个指向某些已定义的持久性单元的选项。请发布您的整个配置xml ...

对于Bean ID的其余问题: 容器中已经有一个使用相同名称初始化的Bean。 Container使用Bean Id作为您可识别代码片段的唯一ID!如果使用其他名称,则容器将初始化同一类的另一个bean实例,并为其指定其他名称。但是正如我上面指出的,这与你的问题无关! (即使您没有其他选项,也可以绕过现有的bean实现并重新定义配置!)