Spring Boot 3.1.2无法加载Entity类的原因及解决办法
问题:Spring Boot 3.1.2中Spring Data JPA提示"Not a managed type"异常,Spring Boot 2.7.1正常
我创建了一个使用Spring Data JPA的Spring Boot 3.1.2版本项目,已添加标注@Entity的Entity类,项目结构正确,但运行时抛出RuntimeException,报错信息如下:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentManagementSystemApplication': Unsatisfied dependency expressed through field 'studentController': Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'studentService': Error creating bean with name 'studentService': Unsatisfied dependency expressed through field 'studentRepository': Error creating bean with name 'studentRepository' defined in com.marolix.StudentManagementSystem.repository.StudentRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.marolix.StudentManagementSystem.entity.StudentInfo at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:716) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:696) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:483) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:942) ~[spring-context-6.0.11.jar:6.0.11] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.11.jar:6.0.11] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-3.1.2.jar:3.1.2] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[spring-boot-3.1.2.jar:3.1.2] at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-3.1.2.jar:3.1.2] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.2.jar:3.1.2] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.2.jar:3.1.2] at com.marolix.StudentManagementSystem.StudentManagementSystemApplication.main(StudentManagementSystemApplication.java:18) ~[classes/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.1.2.jar:3.1.2]
后续我创建了Spring Boot 2.7.1版本的相同项目,运行正常。请问该问题的原因是什么?有没有解决办法?我使用的是Windows操作系统。
原因分析
Spring Boot 3.x基于Spring Framework 6.x,对包扫描规则做了严格调整:
- Spring Boot 2.7.x及更早版本中,
@SpringBootApplication默认会自动扫描主启动类所在包及其所有子包下的@Entity和Repository类 - 但Spring Boot 3.x中,默认扫描的范围校验更严格,如果你的Entity类、Repository类和主启动类不在同一个包层级(或子包),默认扫描无法识别到这些类;另外Windows系统不区分大小写,但JVM加载类时是大小写敏感的,若包名大小写不一致也会导致扫描失败。
你的错误日志核心是Not a managed type: class com.marolix.StudentManagementSystem.entity.StudentInfo,说明Spring无法将该类识别为JPA管理的实体类,本质就是包扫描范围没覆盖到,而2.7.x的默认扫描刚好覆盖了。
解决办法
- 显式指定实体扫描范围:在主启动类上添加
@EntityScan注解,明确Entity类所在的包路径:@SpringBootApplication @EntityScan(basePackages = "com.marolix.StudentManagementSystem.entity") public class StudentManagementSystemApplication { public static void main(String[] args) { SpringApplication.run(StudentManagementSystemApplication.class, args); } } - 显式指定Repository扫描范围:如果Repository所在包也不在默认扫描范围内,同时添加
@EnableJpaRepositories注解:@SpringBootApplication @EntityScan(basePackages = "com.marolix.StudentManagementSystem.entity") @EnableJpaRepositories(basePackages = "com.marolix.StudentManagementSystem.repository") public class StudentManagementSystemApplication { public static void main(String[] args) { SpringApplication.run(StudentManagementSystemApplication.class, args); } } - 检查包命名一致性:确保注解中指定的包名和实际类的包名完全一致(比如不要出现
Entity和entity大小写不同的情况),避免Windows大小写不敏感但JVM敏感导致的扫描失败。
内容的提问来源于stack exchange,提问作者bharath Reddy
相关产品推荐
相关产品推荐

