Spring项目启动报UnsatisfiedDependencyException的排查与解决
解决Spring Boot UnsatisfiedDependencyException Bean创建失败问题
问题场景
开发包含StudentConfig、Student实体类、StudentController、StudentService、StudentRepository的基础Spring Boot项目时,启动抛出UnsatisfiedDependencyException,无法完成相关Bean的创建。
错误根源
排查后确认,问题出在StudentRepository的@Query注解语句中:语句里使用了大写的EMAIL,但Student实体类中对应的属性是小写的email,Hibernate无法解析这个不匹配的属性名,最终导致Bean依赖注入失败。
修复方案
修改StudentRepository中@Query语句内的属性名,使其与实体类属性名保持一致:
错误示例
@Query("SELECT s FROM Student s WHERE s.EMAIL = ?1") Optional<Student> findByEmail(String email);
正确示例
@Query("SELECT s FROM Student s WHERE s.email = ?1") Optional<Student> findByEmail(String email);
修改后重启项目,即可正常创建Bean并启动服务。
内容的提问来源于stack exchange,提问作者program
相关产品推荐
相关产品推荐

