今天在整合spring,springMVC,Hibernate启动tomcat7.0的时候出现异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘accountController’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.sram.service.AccountService com.sram.controller.AccountController.accountService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sram.service.AccountService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
probably means that during mapping: my com.sram.service.AccountService interface cannot find its bean during dependency injection. I searched on the Internet and the problem is probably the following: < /span> 2. No annotations were added to the corresponding classes
1. Spring did not find the required class during scanning
But none of these solved my problem. Finally, I found that my problem was that the listener was not configured, causing web.xml to not automatically load the spring.xml configuration file
Solution:
Configure the listener in the web.xml file, and automatically load the spring configuration file when tomcat7.0 starts.
<!--配置监听器 --> <!--以便在服务器启动的时候,加载spring配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>