没有bean命名为' entityManagerFactory#39;已定义,bean引用问题

时间:2014-05-27 09:58:23

标签: spring jpa spring-data

我正在使用spring-framework 4.0.2.RELEASEhibernate-3.6.4.Final

此春季数据jpa配置给出了一个错误:

No bean named 'entityManagerFactory' is defiend.  

但是我发现,如果我将bean名称emf更改为entityManagerFactory,那么就没有问题了。

有人可以解释为什么bean引用在这里不起作用吗?

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <!-- http://drypot.com/post/95?p=6 spring + hibernate: with JPA -->

    <tx:annotation-driven />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >

        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />

    </bean>

    <bean id="emf"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:packagesToScan="${entitymanager.packagesToScan}" >
        <property name="dataSource" ref="dataSource" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <!-- validate | update | create | create-drop -->
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <!--prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop-->

                <!-- hibernate ehcache
                    http://stackoverflow.com/questions/5270998/spring-hibernate-ehcache
                 --> 
                <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
                <prop key="hibernate.cache.factory_class">${hibernate.cache.factory_class}</prop>

            </props>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
    </bean>

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

</beans>

2 个答案:

答案 0 :(得分:3)

使用Spring Data JPA时,默认情况下会查找名为entityManagerFactory的bean。这是基于xml命名空间的配置(<jpa:repositories />)和java base配置(@EnableJpaRepositories)的默认设置。

如果你有一个带有其他名称的bean,你必须清楚地知道Spring Data JPA,以便它可以选择合适的EntityManagerFactory来使用。

<task:repositories entity-manager-factory-ref="emf" />

或java config

@EnableJpaRepositories(entityManagerFactoryRef="emf")

链接

  1. Spring Data JPA reference guide
  2. @EnableJpaRepositories javadoc

答案 1 :(得分:0)

尝试注入指定bean名称的EntityManagerFactory,如下所示:

@Autowired
@Qualifier(value = "emf")
private EntityManagerFactory entityManagerFactory;