'Spring BeanCreationException: cannot resolve reference to bean exception

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

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

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]

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

<!-- 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.

Has anyone any idea as to why this is?



Solution 1:[1]

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

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

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

Solution 2:[2]

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

In the class com.otv.model.User, there is no @Id annotation above the field 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

Solution 3:[3]

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

 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.

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

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.

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

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alessandro Santini
Solution 2 Community
Solution 3 Harsh