2017-03-19 117 views
0

我與使用具有以下細節Hibernate一同在Eclipse中創建Spring MVC的項目:org.springframework.beans.factory.BeanCreationException:錯誤創建豆

錯誤

org.apache.catalina.core.StandardContext.listenerStart 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 'springRestController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.ipl.services.UserScoreService com.ipl.controller.SpringRestController.userScoreService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userScoreServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.ipl.dao.UserScoreDao com.ipl.services.UserScoreServiceImpl.userScoreDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userScoreDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.hibernate.SessionFactory com.ipl.dao.UserScoreDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.ipl.model.UserScore 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4738) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5181) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717) 
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:945) 
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1798) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.ipl.services.UserScoreService com.ipl.controller.SpringRestController.userScoreService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userScoreServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.ipl.dao.UserScoreDao com.ipl.services.UserScoreServiceImpl.userScoreDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userScoreDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.hibernate.SessionFactory com.ipl.dao.UserScoreDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.ipl.model.UserScore 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) 
    ... 26 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userScoreServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.ipl.dao.UserScoreDao com.ipl.services.UserScoreServiceImpl.userScoreDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userScoreDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.hibernate.SessionFactory com.ipl.dao.UserScoreDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.ipl.model.UserScore 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1017) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) 
    ... 28 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.ipl.dao.UserScoreDao com.ipl.services.UserScoreServiceImpl.userScoreDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userScoreDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.hibernate.SessionFactory com.ipl.dao.UserScoreDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.ipl.model.UserScore 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) 
    ... 39 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userScoreDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.hibernate.SessionFactory com.ipl.dao.UserScoreDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.ipl.model.UserScore 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1017) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) 
    ... 41 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.hibernate.SessionFactory com.ipl.dao.UserScoreDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.ipl.model.UserScore 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) 
    ... 52 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.ipl.model.UserScore 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1017) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) 
    ... 54 more 
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.ipl.model.UserScore 
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:277) 
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:224) 
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:775) 
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788) 
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742) 
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928) 
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:343) 
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:431) 
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:416) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) 
    ... 64 more 



**mvc-dispatcher-servlet.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:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 

<context:component-scan base-package="com.ipl" /> 

<mvc:annotation-driven /> 

<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/ipl" /> 
    <property name="username" value="root" /> 
    <property name="password" value="admin" /> 
</bean> 

    <bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix"> 
    <value>/WEB-INF/jsp/</value> 
    </property> 
    <property name="suffix"> 
    <value>.jsp</value> 
    </property> 
    </bean> 

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="annotatedClasses"> 
    <list> 
    <value>com.ipl.model.UserScore</value> 
    </list> 
    </property> 
    <property name="hibernateProperties"> 
    <props> 
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
    </props> 
    </property> 
</bean> 

<bean id="txManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

<!--<bean id="UserScoreDao" class="com.ipl.dao.UserScoreDaoImpl"></bean> 
<bean id="UserScoreService" class="com.ipl.services.UserScoreServiceImpl"></bean>--> 

<bean id="persistenceExceptionTranslationPostProcessor" 
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 

<bean id="UserScoreDao" class="com.ipl.dao.UserScoreDaoImpl"></bean> 
<bean id="UserScoreService" class="com.ipl.services.UserScoreServiceImpl"></bean> 


</beans> 




**UserScoreDaoImpl.java** 

package com.ipl.dao; 

import java.util.List; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 

import com.ipl.model.UserScore; 

@Component 
public class UserScoreDaoImpl implements UserScoreDao{ 

    @Autowired 
    SessionFactory sessionFactory; 

    Session session = null; 
    Transaction tx = null; 

    @SuppressWarnings("unchecked") 
    @Override 
    public List getEntityList() throws Exception { 
     session = sessionFactory.openSession(); 
      tx = session.beginTransaction(); 
      List userScoreList = session.createCriteria(UserScore.class) 
      .list(); 
      tx.commit(); 
      session.close(); 
      return userScoreList; 
    } 


} 


**UserScoreDao.java** 


package com.ipl.dao; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 

import com.ipl.model.UserScore; 

public interface UserScoreDao { 

    public List getEntityList() throws Exception; 
} 

UserScoreService.java : 

package com.ipl.services; 

import java.util.List; 
import org.springframework.beans.factory.annotation.Autowired; 
import com.ipl.model.UserScore;; 

public interface UserScoreService { 
    public List getEntityList() throws Exception; 

} 


**UserScoreServiceImpl.java** 

package com.ipl.services; 

import java.util.List; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 
import com.ipl.dao.UserScoreDao; 
import com.ipl.model.UserScore; 

public class UserScoreServiceImpl implements UserScoreService{ 
    @Autowired 

    UserScoreDao userScoreDao; 

    @Override 
    public List getEntityList() throws Exception { 
     return userScoreDao.getEntityList(); 
    } 

} 

但是當我嘗試運行該項目我收到錯誤「org.springframework.beans.factory.BeanCreationException:創建名爲'RestController'的bean時出錯'」

+0

在這裏你會得到你的答案... http://stackoverflow.com/questions/7914363/injection-of-autowired-dependencies-failed –

回答

1

該錯誤信息是明確的:

No identifier specified for entity: com.ipl.model.UserScore 

沒有看到你的模型似乎你還沒有指定在您的UserScore實體上註明@Id的字段。每個@Entity需要一個@Id,表示數據庫中的主鍵。


您標記UserScore作爲註解的類:

<property name="annotatedClasses"> 
    <list> 
     <value>com.ipl.model.UserScore</value> 
    </list> 
    </property> 

並在此基礎:

List userScoreList = session.createCriteria(UserScore.class).list(); 

很顯然使用的是UserScore獲得通過休眠從DB數據,所以它是否是你的觀點並不重要。休眠它必須是一個實體。您需要使用@Entity(比使用xml配置更容易)註釋該bean,並將與主鍵匹配的字段標記爲@Id

Marking a POJO as persistent entity

一個例子:

Spring Hibernate Integration

+0

但UserScore是我沒有主鍵的視圖 –

+0

@BeenaPatel我已經更新了我的答案。 – alfcope

+0

使@Id字段解決我的問題 –

0

因爲您已經定義了一個UserS coreDao豆在這樣的XML上下文:

<bean id="UserScoreDao" class="com.ipl.dao.UserScoreDaoImpl"></bean> 

與此同時,該UserScoreDaoImpl與@Component註解,這意味着將有另一個UserScoreDao豆在應用程序上下文和名稱將是userScoreDaoImpl

因此,您在應用程序上下文中有兩個UserScoreDao bean。 Spring不知道應該將哪一個連接到** UserScoreServiceImpl ** bean。

也許你可以嘗試通過評論在XML文件中刪除bean聲明它:

<bean id="persistenceExceptionTranslationPostProcessor" 
     class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 

<!--<bean id="UserScoreDao" class="com.ipl.dao.UserScoreDaoImpl"></bean> 
<bean id="UserScoreService" class="com.ipl.services.UserScoreServiceImpl"></bean> 
<bean id="UserScoreDao" class="com.ipl.dao.UserScoreDaoImpl"></bean> 
<bean id="UserScoreService" class="com.ipl.services.UserScoreServiceImpl"></bean>--> 
相關問題