Spring BeanCreationException:无法解析对 bean 异常的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14124228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:37:14  来源:igfitidea点击:

Spring BeanCreationException: cannot resolve reference to bean exception

spring

提问by Mr Morgan

I am trying the example application at the following web site:

我正在以下网站上尝试示例应用程序:

JSF 2, PrimeFaces 3, Spring 3 & Hibernate 4 Integration Project

JSF 2、PrimeFaces 3、Spring 3 和 Hibernate 4 集成项目

But I find that when running the project, I get:

但我发现在运行项目时,我得到:

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 'UserService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'UserDAO' while setting bean property 'userDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserDAO' defined in ServletContext resource [/WEB-INF/applicationContext.xml]

严重:异常将上下文初始化事件发送到 org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException 类的侦听器实例:在 ServletContext 资源 [/WEB-INF/applicationContext] 中创建名称为“UserService”的 bean 时出错.xml]:在设置 bean 属性“userDAO”时无法解析对 bean“UserDAO”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/applicationContext.xml] 中定义名称为“UserDAO”的 bean 创建时出错

However, in the applicationContext.xml file, the relevant code is as follows:

但是在applicationContext.xml文件中,相关代码如下:

<!-- Beans Declaration -->
<bean id="User" class="com.otv.model.User"/>

<!-- User Service Declaration -->
<bean id="UserService" class="com.otv.user.service.UserService">
  <property name="userDAO" ref="UserDAO" />
</bean>

<!-- User DAO Declaration -->
<bean id="UserDAO" class="com.otv.user.dao.UserDAO">
 <property name="sessionFactory" ref="SessionFactory" />
</bean>

<!-- Session Factory Declaration -->
<bean id="SessionFactory"    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
 <property name="dataSource" ref="DataSource" />
 <property name="annotatedClasses">
  <list>
   <value>com.otv.model.User</value>
 </list>
</property>
<property name="hibernateProperties">
 <props>
  <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  <prop key="hibernate.show_sql">true</prop>
 </props>

The classes do exist in the relevant packages as well as can be seen below and the location of the various config files.

这些类确实存在于相关包中,并且可以在下面看到以及各种配置文件的位置。

enter image description here

在此处输入图片说明

The only difference I can see between the tutorial and my implementation of it is that I am using NetBeans 7.2 rather than Eclipse.

我在本教程和我的实现之间看到的唯一区别是我使用的是 NetBeans 7.2 而不是 Eclipse。

Has anyone any idea as to why this is?

有没有人知道为什么会这样?

回答by Martin Nielsen

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 'UserService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'UserDAO' while setting bean property 'userDAO';

This is telling you that UserService can't be created because its missing a property definition

这告诉您无法创建 UserService 因为它缺少属性定义

 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserDAO' defined in ServletContext resource [/WEB-INF/applicationContext.xml]

This tells you that the definition for UserDAO can't be found.

这告诉您无法找到 UserDAO 的定义。

You are missing the UserDao definition, the ref just means that it should be of that type, it still needs a bean definition.

你缺少 UserDao 定义,ref 只是意味着它应该是那种类型,它仍然需要一个 bean 定义。

Basically, whenever you use "ref" you are telling spring to make a property of that type. That type needs to be defined in its own bean definition.

基本上,每当您使用“ref”时,您就是在告诉 spring 创建该类型的属性。该类型需要在其自己的 bean 定义中定义。

So if UserDao uses some other property that again is defined by a "ref" that property will need its own bean definition as well.

因此,如果 UserDao 使用一些其他属性再次由“ref”定义,该属性也将需要自己的 bean 定义。

You have to think of the classes and the spring definitions to be two completely separate entities. The classes might be there and placed where they should, but spring needs its bean definitions in order to invoke them. It doesn't know what a UserDao or SessionFactory is unless you specifically tell it which package/class you want to be invoked.

您必须将类和弹簧定义视为两个完全独立的实体。这些类可能在那里并放置在它们应该放置的地方,但是 spring 需要它的 bean 定义才能调用它们。它不知道 UserDao 或 SessionFactory 是什么,除非你特别告诉它你想调用哪个包/类。

回答by Alessandro Santini

/WEB-INF/applicationContext.xmlshould contain an entry like <bean id="UserDAO" class="com.otv.dao.UserDAO">...</bean>whose properties largely depend on the backend system used.

/WEB-INF/applicationContext.xml应该包含一个条目,<bean id="UserDAO" class="com.otv.dao.UserDAO">...</bean>其属性在很大程度上取决于所使用的后端系统。

I also suspect that the Userbean is a bad copy and past as Userinstances should be retrived from the DAO or created programmatically.

我还怀疑这个Userbean 是一个错误的副本,因为User实例应该从 DAO 中检索或以编程方式创建。

As to why it works in Eclipse and not in Netbeans, it is too strange to be true. There must be some clutter...

至于为什么它在 Eclipse 中工作而不是在 Netbeans 中工作,这太奇怪了。一定有什么乱七八糟的……

回答by Britel

I found the main cause of that error. It is actually quit simple.

我找到了该错误的主要原因。其实很简单。

In the class com.otv.model.User, there is no @Idannotation above the field id.

在类中com.otv.model.User@Id字段上方没有注释id

Here is the link for the answer that lead me to find what was the mistake : hibernate exception: org.hibernate.AnnotationException: No identifier specified for entity: com..domain.idea.MAE_MFEView

这是让我找到错误所在的答案的链接:hibernate exception: org.hibernate.AnnotationException: No identifier specified for entity: com..domain.idea.MAE_MFEView