5

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?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Mr Morgan
  • 2,215
  • 15
  • 48
  • 78
  • 1
  • Class UserDAO is defined in package package com.otv.user.dao; – Mr Morgan Jan 02 '13 at 15:05
  • I mean the Spring bean definition. If it is not there, there is no chance it will get injected. – Alessandro Santini Jan 02 '13 at 15:06
  • 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 Jan 02 '13 at 15:11
  • 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 Jan 02 '13 at 16:05
  • I was thinking about this. I have not created a SessionFactory class because there is none referred to in the tutorial. I had assumed such a class to be created implicitly because the UserDAO class has a private SessionFactory variable called sessionFactory which makes use of import org.hibernate.SessionFactory; – Mr Morgan Jan 02 '13 at 16:12
  • The `ref` in your `` tag specifies the name of the Spring _bean_ that should be injected as the value of the property, not the name of the _class_ - you need a `bean id="SessionFactory"` with the appropriate `class` and properties, Spring won't magically create a bean of this class for you if you haven't told it to. – Ian Roberts Jan 02 '13 at 16:25
  • Are you saying that I should have a reference in applicationContext.xml? Or to an explicit SessionFactory class of my own creating? – Mr Morgan Jan 02 '13 at 16:28
  • You'll need a bean with id `SessionFactory` and with whatever type your `UserDAO.setSessionFactory(...)` method expects as a parameter. But if it's a Hibernate Session Factory then it'll require various property settings of its own for the dataSource, etc. etc. I suggest you start with a much simpler Spring tutorial such as [this one](http://www.tutorialspoint.com/spring/spring_hello_world_example.htm) to get a better understanding of the basics of Spring before you try and tackle something as complex as a Hibernate setup. – Ian Roberts Jan 02 '13 at 16:48
  • Perhaps you are correct. It may also be that Eclipse is handling some of the configuration of Spring Hibernate and that NetBeans isn't. Thanks. – Mr Morgan Jan 02 '13 at 16:55

3 Answers3

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

Harsh
  • 243
  • 3
  • 20
Martin Nielsen
  • 1,865
  • 6
  • 30
  • 54
  • Ok. I have the UserDAO definition in applicationContext.xml, and the code displayed has been altered. But I still ge tthe same problem. – Mr Morgan Jan 02 '13 at 15:16
  • 1
    That's unfair. The classes are created where they should be, but the messages still appear at run time causing failure. – Mr Morgan Jan 02 '13 at 15:36
  • Spring will always throw these exceptions at runtime, because they are runtime exceptions. Ok that hardly explains anything does it. Spring uses the bean definitions to invoke the classes at run time. Because of this the only exceptions you will see are runtime exceptions. – Martin Nielsen Jan 03 '13 at 06:56
  • I updated my answer a little bit, as i think the problem might be that you haven't fully understood the tutorial. To be fair, Spring is a very hard nut to crack because it's to inherently different from "normal class setup" – Martin Nielsen Jan 03 '13 at 07:03
  • OK: I understand that the ApplicationContext.xml file is to define some Java beans so that they can be retrieved by the application. So, could it be that for some reason, the references to my classes in ApplicationContext.xml are not being found? I include more of my ApplicationContext file above. – Mr Morgan Jan 03 '13 at 14:18
0

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

Alessandro Santini
  • 1,943
  • 13
  • 17
0

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

Community
  • 1
  • 1
hicham
  • 53
  • 1
  • 2
  • 14