錯誤
新創建的項目,使用了Mybatis
啓動報錯:
ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'poemController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'poemService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.dao.PoemDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
原因
沒有掃描mapper
接口,所以沒有自動創建這些 bean
,所以注入的時候找不到PoemDao
就報錯了:
Controller 層注入 Service
Service 層注入 Dao
Dao 沒有,拋出異常。
解決方案
方案1. 啓動類添加註解:
@MapperScan("com.example.demo.dao")
方案2. 所有mapper添加註解@Mapper
@Mapper
public interface PoemDao {。。。}