4
votes

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?

3
Where is the <bean id="UserDAO" definition?Alessandro Santini
Class UserDAO is defined in package package com.otv.user.dao;Mr Morgan
I mean the Spring bean definition. If it is not there, there is no chance it will get injected.Alessandro Santini
I'm a complete novice at Spring so I'm not quite sure of the class you mean. But applicationContext.xml refers to the UserService bean which is present in the package stated.Mr Morgan
Keep following the chain of causes down the exception trace and you will probably find the real underlying error at the bottom. The trace you've posted so far says that Spring couldn't create UserService because it depends on UserDAO and there was an error creating UserDAO. You need to find out what the error was when creating UserDAO (which may in turn be an error creating SessionFactory, and so on).Ian Roberts

3 Answers

0
votes

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

0
votes

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

0
votes
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.