0

在我的 spring mvc+hibernate+annotations 项目中,我有这三个类

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  
}  

注册控制器.java

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

在我的dispatcher-servlet.xml我添加了以下内容

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

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

当我运行该项目时,出现以下异常:

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;  

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  

有人能指出我在哪里失踪吗?
这已经占用了我今天的一整天。

编辑:
我添加了 commons.pool 但没有结果。
我有这些例外。

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  

谢谢....

4

2 回答 2

0

第二个很简单:您缺少包含org.apache.commons.pool.impl.GenericObjectPool在 CLASSPATH 中的 JAR。

问题是:哪个类加载器?Java EE 应用服务器具有类加载器的层次结构。我猜您想将该 JAR 添加到您的服务器 /lib 目录中,以便在设置连接池时可以使用它。

第一个我不清楚。尝试将您的基础包更改为“com.alw”,看看是否可以解决问题。

于 2012-05-31T11:54:55.793 回答
0

正如 duffymo 指出的那样,您的类路径中缺少公共资源池。至于另一个问题,当你说你得到错误 1​​和错误 2 时,你的意思是你在不同的时间得到两个不相关的错误,还是你得到由错误 2 引起的错误 1。如果你在同时登录,它们可能是同一件事,其中第二个是第一个的原因。

另一方面,您犯了一个常见错误,即将所有服务 bean 放入属于 DispatcherServlet 的上下文中。如果您还在根上下文中声明 bean,那也可能会给您带来问题。请参阅此其他答案及其链接答案,以了解 Spring MVC 应用程序中根上下文和子上下文之间的区别:

为什么 DispatcherServlet 创建另一个应用程序上下文?

尤其:

Spring-MVC:什么是“上下文”和“命名空间”?

于 2012-05-31T12:10:51.733 回答