問題一:Autowired注解時(shí)報(bào)錯(cuò),提示“Could not autowire. No beans of 'InfconfigDao' type found.”
解決辦法:
@Autowired注入成員變量,前提是成員變量已被注入到spring容器中,注解的這個(gè)類InfconfigDao需要被Spring容器管理,需要在InfconfigDao加上@Component,把普通pojo實(shí)例化到spring容器中

image.png

image.png
問題二:啟動(dòng)項(xiàng)目一直無法注入
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zgl.interfacetest.dao.InfconfigDao' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound
解決辦法:
SpringBoot項(xiàng)目的Bean裝配默認(rèn)規(guī)則是根據(jù)Application類所在的包位置從上往下掃描!Application類是指SpringBoot項(xiàng)目入口類。
這個(gè)類的位置很關(guān)鍵: 如果Application類所在的包為:com.zgl.interfacetest,則只會(huì)掃描com.zgl.interfacetest包及其所有子包,如果dao、service所在包不在com.zgl.interfacetest及其子包下,則不會(huì)被掃描到。
在啟動(dòng)類中加
@ComponentScan(basePackages = "com.zgl.interfacetest.dao")

image.png
- @controller 控制器(注入服務(wù))
用于標(biāo)注控制層,相當(dāng)于struts中的action層 - @service 服務(wù)(注入dao)
用于標(biāo)注服務(wù)層,主要用來進(jìn)行業(yè)務(wù)的邏輯處理 - @repository(實(shí)現(xiàn)dao訪問)
用于標(biāo)注數(shù)據(jù)訪問層,也可以說用于標(biāo)注數(shù)據(jù)訪問組件,即DAO組件 - @component (把普通pojo實(shí)例化到spring容器中,相當(dāng)于配置文件中的<bean id="" class=""/>
)泛指各種組件,就是說當(dāng)我們的類不屬于各種歸類的時(shí)候(不屬于@Controller、@Services等的時(shí)候),我們就可以使用@Component來標(biāo)注這個(gè)類。