2 xrs870709 xrs870709 于 2013.12.16 16:14 提问

spring MVC注解 + hibernate +jpa配置 出错 具体如下

这个是错误 No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0

下面为XML配置文件

web.xml 导入hibernate

<filter>
    <filter-name>openEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>openEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

applicationContext.xml如下

<beans profile="production,development">
    <context:property-placeholder ignore-unresolvable="true"
        location="classpath*:properties/application.properties,
                  classpath*:properties/application.local.properties" />

    <!-- 数据源配置, 使用DBCP数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!-- Connection Info -->
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@172.16.80.40:1521:ydRIS4T" />
        <property name="username" value="rim_uat" />
        <property name="password" value="password" />

        <!-- Connection Pooling Info -->
        <property name="maxActive" value="100" />
        <property name="maxIdle" value="20" />
        <property name="defaultAutoCommit" value="false" />
        <!-- 连接Idle一个小时后超时 -->
        <property name="timeBetweenEvictionRunsMillis" value="3600000" />
        <property name="minEvictableIdleTimeMillis" value="3600000" />
    </bean>
</beans>

<!-- Jpa Entity Manager 配置 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
    <property name="packagesToScan" value="com.rsxiao.test.entity"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
            <prop key="net.sf.ehcache.configurationResourceName">config/ehcache-hibernate-local.xml</prop>
            <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
        </props>
    </property>
</bean>

<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />

<!-- Spring Data Jpa配置 -->
<jpa:repositories base-package="com.rsxiao.test" transaction-manager-ref="transactionManager"
    entity-manager-factory-ref="entityManagerFactory" />

<!-- Jpa 事务配置 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

Servlet.service() for servlet springServlet threw exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0
at org.springframework.beans.factory.BeanFactoryUtils.beanOfType(BeanFactoryUtils.java:394)
at org.springframework.orm.jpa.EntityManagerFactoryUtils.findEntityManagerFactory(EntityManagerFactoryUtils.java:111)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.lookupEntityManagerFactory(OpenEntityManagerInViewFilter.java:194)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.lookupEntityManagerFactory(OpenEntityManagerInViewFilter.java:170)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:128)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)

1个回答

a_liujian
a_liujian   2013.12.16 16:48
已采纳

No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0 ,把后面的错误日志也发出来看看! 前面的错误信息有可能是从架包底层报了来的。字面意思是bean 注入的name可能重复,可能有某bean未注入。
没用过jpa,但从你配置上来看,问题应该是出在applicationContext.xml下的jap配置,你可以找找它其他的配置方法再试试看。说不定问题就有可能得到解决!

参考帖:
http://www.iteye.com/topic/347882

xrs870709
xrs870709 还什么都没有做 ,只是想先把他们先全部整合好
大约 4 年之前 回复
xrs870709
xrs870709 就是整合spring+hibernate出错
大约 4 年之前 回复
a_liujian
a_liujian org.springframework.beans.factory.NoSuchBeanDefinitionException; spring bean配置有误,未找到所需要的bean。先把spring+hibernate整合好吧
大约 4 年之前 回复
xrs870709
xrs870709 错误贴在原文后面
大约 4 年之前 回复
xrs870709
xrs870709 我还没有添加实体bean 目前还属于搭建环境阶段 entityManagerFactory这个没有重复
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!