0

In my spring mvc+hibernate+annotations project I have these three classes

UserServiceImpl.java

@Service("userService")  
public class UserServiceImpl implements UserService {  
@Autowired  
private UserDAO userDAO;  
//other codes  
}  

UserDAOImpl.java

@Repository("userDAO")  
public class UserDAOImpl implements UserDAO {  
@Autowired  
private SessionFactory sessionFactory;  
//other codes  
}  

RegistrationController.java

@Controller  
@RequestMapping("/registration.htm")  
public class RegistrationController {  
@Autowired  
private UserService userService;  
//other codes  
}  

In my dispatcher-servlet.xml I have added the following

<context:annotation-config />  
<context:component-scan base-package="com.alw.controllers,com.alw.DAOs,com.alw.services" />  

and

<bean id="sessionFactory"  
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  

When I run the project I got the following exceptions:

Error creating bean with name 'registrationController':  
Injection of autowired dependencies failed;  
nested exception is org.springframework.beans.factory.BeanCreationException:  
Could not autowire field: private com.alw.services.UserService  
    com.alw.controllers.RegistrationController.userService;  

AND

Error creating bean with name 'sessionFactory' defined in ServletContext resource  
    [/WEB-INF/dispatcher-servlet.xml]:  
Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError:  
    org/apache/commons/pool/impl/GenericObjectPool  

CAN SOMEBODY POINT OUT THE WHERE I AM MISSING ?
THIS HAS TAKEN MY ENTIRE DAY TODAY.

EDIT:
I added commons.pool but no result.
I have these set of exceptions.

Error creating bean with name 'registrationController':  
Error creating bean with name 'userService':  
Error creating bean with name 'userDAO':  
Error creating bean with name 'sessionFactory' defined in ServletContext  
    resource [/WEB-INF/dispatcher-servlet.xml]:  
Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError:  
Could not initialize class org.hibernate.cfg.AnnotationConfiguration  

THANKS....

mukund
  • 2,866
  • 5
  • 31
  • 41
  • 2
    TURN OFF YOUR CAPS LOCK! IT LOOKS LIKE YOU'RE SHOUTING! IT'S CONSIDERED RUDE ON THE INTERNET! – duffymo May 31 '12 at 11:49

2 Answers2

0

The second one is easy: you're missing the JAR that contains org.apache.commons.pool.impl.GenericObjectPool in your CLASSPATH.

The question is: which class loader? The Java EE app server has a hierarchy of class loaders. I'm guessing that you want to add that JAR to your server /lib directory so it'll be available when the connection pool is set up.

The first one is not clear to me. Try changing your base package to "com.alw" and see if that sorts it out.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

As duffymo points out, you're missing commons pool from your classpath. As for the other problem, when you say you get error 1 and error 2, do you mean you get two, unrelated errors at different times, or do you get error 1 caused by error 2. If you're seeing them both in the log at the same time, they're probably the same thing, where the second is the cause of the first.

On the other hand, you're making the common mistake of putting all of your service beans into the context belonging to the DispatcherServlet. If you're also declaring the beans in the root context, that could be causing problems for you, too. See this other answer and its linked answers to understand the difference between the root and child contexts in a Spring MVC app:

Why DispatcherServlet creates another application context?

Especially:

Spring-MVC: What are a "context" and "namespace"?

Community
  • 1
  • 1
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • Hey Ryan I have completely stuck into this problem so just forget about this.Every site I have visited for examples regarding autowiring beans with annotations found some errors with the code. So Could you please suggest me any site or book where I can get a clear understanding about the same.This will be very useful. thanks... – mukund Jun 01 '12 at 05:25
  • Spring has excellent documentation in the form of its [reference guide](http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/). The [Spring distributions](http://www.springsource.org/download) come with many excellent examples, and the [SpringSource blog](http://blog.springsource.org/) is filled with tons of code examples, too. As for books, check especially [Manning](http://www.manning.com/), [O'Reilly](http://oreilly.com/), and the [Pragmatic Bookshelf](http://pragprog.com/). – Ryan Stewart Jun 01 '12 at 22:47