Spring Error org.springframework.beans.factory.BeanCreationException

时间:2014-07-15 07:44:29

标签: spring hibernate spring-mvc jpa javabeans

我有一个简单的Spring Web项目,我没有使用pom.xml(Maven)。我收到这些错误,即使我到处都是。如果您需要更多代码来识别问题,我可以提供它们。顺便说一下,我正在使用Tomcat 7.谢谢。

更新 我添加了slf4J,Jboss-Logging,Hibernate-Jpa和其他一些库,我也更新了我的库图片。现在,我收到了我在之前版本的日志中出现的错误。

我的日志(已更新):

Tem 15, 2014 11:23:17 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files (x86)\Java\jdk1.7.0_60\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\php\php_runningversion;C:\Program Files (x86)\IBM\RationalSDLC\common;C:\Program Files (x86)\Seagate Software\NOTES\;C:\Program Files (x86)\Seagate Software\NOTES\DATA\;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Common Files\Siemens\System;C:\Program Files (x86)\HiPath 4000 Expert Access\;C:\Program Files\OpenVPN\bin;C:\Program Files (x86)\IBM\RationalSDLC\ClearCase\bin;C:\Program Files (x86)\IBM\gsk8\lib;C:\Program Files (x86)\IBM\gsk8\bin;C:\Program Files (x86)\apache-maven-3.1.1\bin;C:\Program Files (x86)\gradle-1.12\bin;C:\mongodb\bin;C:\Program Files (x86)\MySQL\MySQL Utilities 1.4.3\;C:\Program Files (x86)\MySQL\MySQL Utilities 1.4.3\Doctrine extensions for PHP\;.
Tem 15, 2014 11:23:17 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Tem 15, 2014 11:23:17 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Tem 15, 2014 11:23:17 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 731 ms
Tem 15, 2014 11:23:17 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Tem 15, 2014 11:23:17 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.54
Tem 15, 2014 11:23:18 AM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [209] milliseconds.
Tem 15, 2014 11:23:18 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor C:\Users\serhat.can\Documents\workspace-spring2\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\conf\Catalina\localhost\Spring3MVC.xml
Tem 15, 2014 11:23:18 AM org.apache.catalina.startup.SetContextPropertiesRule begin
WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Spring3MVC' did not find a matching property.
Tem 15, 2014 11:23:18 AM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(C:\Users\serhat.can\Documents\workspace-spring2\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\Spring3MVC\WEB-INF\lib\servlet-api-2.5.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class
Tem 15, 2014 11:23:21 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Tem 15, 2014 11:23:21 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Tem 15, 2014 11:23:23 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' defined in ServletContext resource [/WEB-INF/contexts/application-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emf' defined in ServletContext resource [/WEB-INF/contexts/application-context.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: PersistenceProvider [org.hibernate.ejb.HibernatePersistence@e819e9] did not return an EntityManagerFactory for name 'spitterPU'
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:741)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:672)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1862)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emf' defined in ServletContext resource [/WEB-INF/contexts/application-context.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: PersistenceProvider [org.hibernate.ejb.HibernatePersistence@e819e9] did not return an EntityManagerFactory for name 'spitterPU'
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:439)
    at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:277)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:139)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.<init>(PersistenceExceptionTranslationInterceptor.java:79)
    at org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor.<init>(PersistenceExceptionTranslationAdvisor.java:71)
    at org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:85)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1502)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1470)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    ... 23 more
Caused by: java.lang.IllegalStateException: PersistenceProvider [org.hibernate.ejb.HibernatePersistence@e819e9] did not return an EntityManagerFactory for name 'spitterPU'
    at org.springframework.orm.jpa.LocalEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalEntityManagerFactoryBean.java:86)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
    ... 38 more

Tem 15, 2014 11:23:23 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Tem 15, 2014 11:23:23 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/Spring3MVC] startup failed due to previous errors
Tem 15, 2014 11:23:23 AM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Tem 15, 2014 11:23:23 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deployment of configuration descriptor C:\Users\serhat.can\Documents\workspace-spring2\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\conf\Catalina\localhost\Spring3MVC.xml has finished in 4,920 ms
Tem 15, 2014 11:23:23 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Tem 15, 2014 11:23:23 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Tem 15, 2014 11:23:23 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 5632 ms

我的application-context.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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">




    <!-- Root Context: defines shared resources visible to all other web components -->

    <!-- Uncomment and add your base-package here: -->
    <context:component-scan base-package="com.spitter.web" />



    <!-- data source to connect mysql db -->
    <bean name="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/spring_jpa" />
        <property name="username" value="root" />
        <property name="password" value="1q2w3e4r:!" />
    </bean>



    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="MYSQL" />
        <property name="showSql" value="true" />
        <property name="generateDdl" value="false" />
        <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
    </bean>



    <bean id="emf"
        class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="spitterPU" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    </bean>




    <!-- why do we need this! -->
    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <!-- why do we need this! -->
    <bean
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <!-- why do we need this! -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf" />
    </bean>


    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- <tx:advice id="txAdvice" transaction-manager="transactionManager"> 
        <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method 
        name="*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> 
        <aop:config> <aop:advisor pointcut="execution(* *..SpitterService.*(..))" 
        advice-ref="txAdvice" /> </aop:config> -->


</beans>

这是我的图书馆:

enter image description here

2 个答案:

答案 0 :(得分:0)

您需要将hibernate-jpa-2.0-api-1.0.1.Final.jar包含在classpath中。堆栈跟踪中的另一个错误表明您可能还需要jboss-logging-3.1.0.GA.jar

答案 1 :(得分:0)

我添加了一些罐子,并根据我得到的警告添加了更多。现在,我也遇到过这些错误。我认为这可能与我没有persistence.xml文件有关。然而,在另一台计算机中,它起作用,因为应用程序上下文文件包含所有必要似乎我无法创建一个emf(实体管理器)。我将 application-context.xml 文件的emf部分更改为:

<bean id="emf"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    <property name="packagesToScan" value="com.spitter.web.domain"></property>
</bean>

现在,它适用于helloWorld控制器。