Spring Boot启动报错:找不到UserRepository类型的Bean
问题描述
我不确定为什么会出现这个错误,但怀疑问题出在TestApplication.java里。试过Stack Overflow上相关问题的多种解决方案,还是没找到问题所在。我跟着Amigoscode的视频搭建这个应用,启动时出现如下错误:
错误信息
*************************** APPLICATION FAILED TO START *************************** Description: Parameter 0 of constructor in com.example.User.UserService required a bean of type 'com.example.User.UserRepository' that could not be found. Action: Consider defining a bean of type 'com.example.User.UserRepository' in your configuration.
项目代码
TestApplication.java
package com.example.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import com.example.User.UserController; @SpringBootApplication @ComponentScan(basePackageClasses = {UserController.class}) public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } }
User.java
package com.example.User; import java.time.LocalDate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SequenceGenerator; import javax.persistence.Table; @Entity @Table(name = "USER") public class User { @Id @SequenceGenerator( name = "user_sequence", sequenceName = "user_sequence", allocationSize = 1 ) @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "user_sequence" ) private Long id; private String PDGA_id; private String first_name; private String last_name; private String email; private boolean email_verified; private String phone_number; private String password; private LocalDate created_date; private LocalDate modified_date; public User(){ } public User(Long id, String PDGA_id, String first_name, String last_name, String email, boolean email_verified, String phone_number, String password, LocalDate created_date, LocalDate modified_date) { this.id = id; this.PDGA_id = PDGA_id; this.first_name = first_name; this.last_name = last_name; this.email = email; this.email_verified = email_verified; this.phone_number = phone_number; this.password = password; this.created_date = created_date; this.modified_date = modified_date; } public User(String PDGA_id, String first_name, String last_name, String email, boolean email_verified, String phone_number, String password, LocalDate created_date, LocalDate modified_date) { this.PDGA_id = PDGA_id; this.first_name = first_name; this.last_name = last_name; this.email = email; this.email_verified = email_verified; this.phone_number = phone_number; this.password = password; this.created_date = created_date; this.modified_date = modified_date; } // Getter-Setter public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getPDGA_id() { return this.PDGA_id; } public void setPDGA_id(String PDGA_id) { this.PDGA_id = PDGA_id; } public String getFirst_name() { return this.first_name; } public void setFirst_name(String first_name) { this.first_name = first_name; } public String getLast_name() { return this.last_name; } public void setLast_name(String last_name) { this.last_name = last_name; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public boolean isEmail_verified() { return this.email_verified; } public void setEmail_verified(boolean email_verified) { this.email_verified = email_verified; } public String getPhone_number() { return this.phone_number; } public void setPhone_number(String phone_number) { this.phone_number = phone_number; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } public LocalDate getCreated_date() { return this.created_date; } public void setCreated_date(LocalDate created_date) { this.created_date = created_date; } public LocalDate getModified_date() { return this.modified_date; } public void setModified_date(LocalDate modified_date) { this.modified_date = modified_date; } @Override public String toString(){ return "User{" + "id =" + id + ", PDGA_id" + PDGA_id + '\'' + ", first_name" + first_name + '\'' + ", last_name" + last_name + '\'' + ", email" + email + '\'' + ", email_verified" + email_verified + ", phone_number" + phone_number + '\'' + ", password" + password + '\'' + ", created_date" + created_date + ", modified_date" + modified_date + "}"; } }
UserController.java
package com.example.User; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(path = "api/v1/user") public class UserController { private final UserService userService; @Autowired public UserController(UserService userService){ this.userService = userService; } @GetMapping() public List<User> getUsers(){ return userService.getUsers(); } }
UserRepository.java
package com.example.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User, Long> { }
UserService.java
package com.example.User; import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; @Service public class UserService { private final UserRepository userRepository; @Autowired public UserService(UserRepository userRepository){ this.userRepository = userRepository; } public List<User> getUsers(){ return userRepository.findAll(); } }
application.properties
spring.datasource.url=jdbc:mariadb://localhost:3306/LiensDiGoLinks spring.datasource.username=root spring.datasource.password=Password123! spring.datasource.driver-class-name=org.mariadb.jdbc.Driver spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.properties.hibernate.format_sql=true
解决方案
问题出在你手动添加的@ComponentScan注解上——它只指定扫描UserController.class,导致Spring找不到UserRepository和UserService,没法创建对应的Bean。
两种解决方法:
- 直接移除
@ComponentScan:@SpringBootApplication本身已经包含组件扫描功能,默认会扫描主类所在包及其子包。修改后的TestApplication.java:
package com.example.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } }
- 扩展扫描范围:如果必须保留
@ComponentScan,要把所有需要扫描的类所在范围加进去,比如直接指定父包com.example:
@ComponentScan(basePackages = {"com.example"})
或者同时包含相关类:
@ComponentScan(basePackageClasses = {UserController.class, UserService.class, UserRepository.class})
优先选第一种方法,最简洁省心。
内容的提问来源于stack exchange,提问作者ImprovedEngineer
相关产品推荐
相关产品推荐

