【问题标题】:need to understand autowiring beans in spring mvc project?需要了解 spring mvc 项目中的自动装配 bean?
【发布时间】:2012-05-31 11:47:17
【问题描述】:

在我的 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  
}  

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

谢谢....

【问题讨论】:

  • 关闭大写锁定!看起来你在大喊大叫!在互联网上被认为是粗鲁的!

标签: spring hibernate annotations autowired


【解决方案1】:

正如 duffymo 指出的那样,您的类路径中缺少 commons pool。至于另一个问题,当你说你得到错误 1 ​​错误 2 时,你的意思是你在不同的时间得到两个不相关的错误,还是你得到错误 1 ​​引起em> 错误2。如果您同时在日志中看到它们,它们可能是同一件事,其中第二个是第一个的原因。

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

Why DispatcherServlet creates another application context?

特别是:

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

【讨论】:

  • Hey Ryan 我已经完全陷入了这个问题,所以忘记这一点。我访问过的每个站点都发现了一些关于带有注释的自动装配 bean 的示例。所以你能否建议我任何网站或书籍,我可以清楚地了解相同的内容。这将非常有用。谢谢...
  • Spring 以reference guide 的形式提供了出色的文档。 Spring distributions 带有许多优秀的示例,SpringSource blog 也充满了大量的代码示例。至于书籍,请特别检查ManningO'ReillyPragmatic Bookshelf
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 2018-06-15
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多