當項目結構正常(spring管理的Bean在SrpingBoot啟動類平級或下級,支持spring掃描時),實現類上加 @Service注解,在實現類中注入dao層的Bean時,項目無法啟動,無法找到注入dao層的Bean,會報如下錯:
Description:
Field businessInfoBelongRepository in com.example.test.service.impl.QueryHouseListImpl required a bean of type 'com.example.test.repository.BusinessInfoBelongRepository' that could not be found.
Action:
Consider defining a bean of type 'com.example.test.repository.BusinessInfoBelongRepository' in your configuration.
解決辦法:pom文件中需要引入spring-data-jpa的的Maven包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
總結:SpringDataJPA內部使用了類代理的方式讓繼承了(JpaRepository)它接口的子接口都以spring管理的Bean的形式存在,當你沒有引用spring-data-jpa的時,則在實現類中注入dao層Bean會提示dao層的bean無法注入。