org.springframework.beans.factory.UnsatisfiedDependencyException
今天搭環境的時候遇到這個錯,浪費了我好久,類似下面這種錯誤
Error creating bean with name 'userController'
Error creating bean with name 'userService'
No qualifying bean of type 'xyz.nandu.dao.UserDao' available
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userController’: Unsatisfied dependency expressed through field ‘userService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userService’: Unsatisfied dependency expressed through field ‘userDao’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘xyz.nandu.dao.UserDao’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
網上方法很多,我看了半天,註解啊什麼都沒有問題,發現沒有加監聽器
解決方法:
- 檢查對應的註解,確定都沒有問題
- 檢查web.xml裏有沒有加spring的監聽器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 設置配置文件的路徑-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
加上試試,希望可以幫到大家!