搭了一個簡單的SSM項目,啟動報錯:
org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type 'com.xxx.ssmdemo.service.IUserService' available: expected at least 1 bean
which qualifies as autowire candidate.Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
提示很清楚,找不到IUserService這個Bean的定義,所以自動注入出錯。那么找不到Bean,可能有兩種情況,一是真的沒有定義這個接口(不太可能),二就是Spring沒有掃描到相關的類或者接口。
原因:項目中有spring-mvc.xml,spring-mybatis.xml,applicationContext.xml共三個配置文件。而我在前兩個配置文件中都添加了<context : compoment-scan>,在applicationContext.xml中卻沒有配。
解決:將其余配置文件中的<context : compoment-scan>刪除,在applicationContext中添加。
啟動成功。