SpringBoot集成MySQL启动报错找不到StudentRepository类型Bean如何解决?
Spring Boot集成MySQL启动报错问题及解决方案
问题现象
开发Spring Boot集成MySQL功能时,项目启动失败,错误日志如下:
2021-09-23 01:28:31.193 INFO 1196 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 1.8.0_144 on DESKTOP-PFH9867 with PID 1196 (C:\Users\Admin\IdeaProjects\demo\target\classes started by Admin in C:\Users\Admin\IdeaProjects\demo) 2021-09-23 01:28:31.196 INFO 1196 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default 2021-09-23 01:28:33.014 INFO 1196 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2021-09-23 01:28:33.027 INFO 1196 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021-09-23 01:28:33.027 INFO 1196 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.52] 2021-09-23 01:28:33.145 INFO 1196 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021-09-23 01:28:33.146 INFO 1196 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1875 ms 2021-09-23 01:28:33.218 WARN 1196 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'studentRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.repository.StudentRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2021-09-23 01:28:33.221 INFO 1196 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2021-09-23 01:28:33.242 INFO 1196 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2021-09-23 01:28:33.269 ERROR 1196 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Field studentRepository in com.example.demo.cotroller.StudentController required a bean of type 'com.example.demo.repository.StudentRepository' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.example.demo.repository.StudentRepository' in your configuration. Process finished with exit code 1
按照Spring Boot官方规则,Repository接口创建后Spring会自动生成首字母小写的同名Bean,已排查application.properties配置和项目目录结构,均无异常。
相关代码
DemoApplication.java
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Student.java
package com.example.demo.entity; import javax.persistence.Entity; @Entity // 告知Hibernate将该类映射为数据库表 public class Student { private Integer studentID; private String name; private String department; private String major; public Integer getStudentID() { return studentID; } public void setStudentID(Integer studentID) { this.studentID = studentID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } }
StudentController.java
package com.example.demo.cotroller; import com.example.demo.entity.Student; import com.example.demo.repository.StudentRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @Controller // 标记该类为Controller层组件 @RequestMapping(path="/api/v1/student") // 接口公共前缀 public class StudentController { // 注入Spring自动生成的studentRepository Bean @Autowired private StudentRepository studentRepository; @PostMapping(path="/add") // 仅接收POST请求 public @ResponseBody String addNewUser (@RequestBody Student student) { studentRepository.save(student); return "Saved"; } }
StudentRepository.java
package com.example.demo.repository; import com.example.demo.entity.Student; import org.springframework.data.repository.CrudRepository; // Spring会自动实现该接口为对应的Bean,内置CRUD操作能力 public interface StudentRepository extends CrudRepository<Student, Integer> { }
项目目录结构
java |--com.example.demo |--controller |--StudentController.java |--entity |--Student.java |--repository |--StudentRepository.java DemoApplication.java
根因与解决方案
问题根因
创建Spring Boot项目时漏选了必需的Spring Data JPA依赖,导致Spring无法识别Repository接口、自动生成对应的Bean。
解决步骤
- 在IDEA中重新创建Spring Boot项目,勾选
Spring Web、Spring Data JPA、MySQL Driver三个必要依赖 - 将原有代码文件迁移到新项目中即可正常启动
内容的提问来源于stack exchange,提问作者prettypig
相关产品推荐
相关产品推荐

