You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot启动报错:无法找到com.exam.repo.UserRepository类型的Bean

问题:Spring Boot启动失败,找不到UserRepository类型Bean

运行Spring Boot应用时启动失败,报错提示com.exam.service.UserServiceImpl中的userRepo字段需要com.exam.repo.UserRepository类型的Bean,但该Bean未被找到。已给UserRepository添加@Repository注解,项目运行在SpringToolSuite中,使用MySQL数据库,尝试过多种排查步骤仍未解决。

启动类代码

package com.exam.examserver;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

import com.exam.models.Role;
import com.exam.models.User;
import com.exam.models.UserRole;
import com.exam.service.UserService;

@ComponentScan({"com.exam.service"})
@SpringBootApplication
public class ExamserverApplication implements CommandLineRunner {

    @Autowired
    private UserService userService ;
    
    public static void main(String[] args) {
        SpringApplication.run(ExamserverApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("starting");     
        
        User user=new User();
        user.setFirstName("yogender");
        user.setLastName("sharma");
        user.setUserName("yogen139");
        user.setPassword("abc");
        user.setEmail("abc@gmail.com");
 
        Role role1= new Role();
        role1.setRoleId(44L);
        role1.setRoleName("Admin");
        
        
        Set<UserRole> userRoleSet= new HashSet<>();
        UserRole userRole= new UserRole();
        userRole.setRole(role1);
        userRole.setUser(user);
        User user1= this.userService.CreateUser(user, userRoleSet);
        
        System.out.println(user1.getUserName());
    }
}

错误日志

Error starting ApplicationContext. To display the condition 
evaluation report re-run your application with 'debug' enabled.

***************************
APPLICATION FAILED TO START
***************************

Description:
Field userRepo in com.exam.service.UserServiceImpl required a bean 
of type 'com.exam.repo.UserRepository' 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.exam.repo.UserRepository' in 
your configuration.

项目结构

项目包结构如下:

  • com.exam.examserver:存放启动类ExamserverApplication
  • com.exam.models:存放User、Role、UserRole等实体类
  • com.exam.repo:存放UserRepository等数据仓库接口
  • com.exam.service:存放UserService接口及其实现类UserServiceImpl

解决方案

问题根源

启动类上的@ComponentScan({"com.exam.service"})仅指定扫描com.exam.service包,导致Spring无法扫描到com.exam.repo包下的UserRepository,无法自动创建对应的Bean实例。

修复步骤

  1. 移除多余的@ComponentScan注解:@SpringBootApplication注解默认会扫描启动类所在包(com.exam.examserver)及其所有子包,而com.exam.repo、com.exam.service均属于com.exam的子包,默认扫描范围可以覆盖这些包,因此直接删除@ComponentScan({"com.exam.service"})即可。
  2. 若需保留@ComponentScan,扩大扫描范围:如果业务需要自定义扫描范围,将注解修改为@ComponentScan({"com.exam"}),确保包含所有需要扫描的包(repo、service、models等)。
  3. 验证UserRepository的继承关系:确认UserRepository接口继承了Spring Data的仓库接口(如JpaRepository<User, Long>),Spring Data需要通过这种继承关系来自动生成代理Bean。

内容的提问来源于stack exchange,提问作者Yogender Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 07:27:30