当项目结构正常(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无法注入。