spring boot: 构建项目时报错Not a managed type

今天在学习使用Spring Data JPA的时候,将bean和JpaRepository放在了不同的package中,导致无法构建项目,报以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readingListRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class bean.Book

异常是逐层抛出的,所以要定位到错误的地方,应该看最底下的内容,

java.lang.IllegalArgumentException: Not a managed type: class bean.Book

也就是我们没有按照SpringBoot的约定,默认扫描(application.java 入口类相对的兄弟包及其子包)
解决方法1:将bean和JpaRepository放在同一个package中。
解决方法2:在JpaRepository上添加注释,使得它能找到bean

@EntityScan("bean.Book")

这里整合以下常见的配置

@ComponentScan(basePackages = "com.boot.demo.xxx.*.*")
用于扫描@Controller @Service


 @EnableJpaRepositories(basePackages = "com.boot.demo.xxx.*.dao") 
用于扫描Dao @Repository


@EntityScan("com.boot.demo.xxx.*.*")
用于扫描JPA实体类 @Entity

好吧,现在完全不知道spring-boot的文件结构要如何,多创建了几个包,现在各种找不到类,controller也无法配置,明天继续吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值