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

Spring Boot中ProfilesRepository Bean无法找到问题求助

问题

启动Spring Boot应用时触发Bean找不到的错误,ProfilesRepository明明存在且和启动类、控制器同包,却提示无法找到该类型的Bean。

核心报错

Field profilesRepository in com.test.ProfilesController required a bean of type 'com.test.ProfilesRepository' that could not be found.

完整报错日志

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-02-15T21:21:47.035-05:00 ERROR 32878 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   :

***************************

APPLICATION FAILED TO START

***************************

Description:

Field profilesRepository in com.test.ProfilesController required a bean of type 'com.test.ProfilesRepository' 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.Group3.GeekText.ProfilesRepository' in your configuration.

项目文件结构

src
└── main
    └── java
        └── com.test
            ├── TestApplication.java(启动类)
            ├── Profiles.java(实体类)
            ├── ProfilesController.java(控制器)
            └── ProfilesRepository.java(JPA仓库接口)

相关代码片段

Profiles.java(实体类)

package com.test;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "Profiles")
public class Profiles {

    @Id
    @Column(name = "ID")
    public int id;
    @Column(name = "Username")
    public String username;

    @Column(name = "Name")
    public String name;
    @Column(name = "EmailAddress")
    public String emailAddress;
    @Column(name = "HomeAddress")
    public String homeAddress;


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getEmailAddress() {
        return emailAddress;
    }
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public String getHomeAddress() {
        return homeAddress;
    }
    public void setHomeAddress(String homeAddress) {
        this.homeAddress = homeAddress;
    }

}

ProfilesController.java(控制器)

package com.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProfilesController {

    @Autowired
    public ProfilesRepository profilesRepository;

    @GetMapping("/getAllProfiles")
    public List<Profiles> getAllProfiles() {
        return profilesRepository.findAll();
    }
    @GetMapping("/helloWorld")
    public String helloWorld(){
        return "Hello World!";
    }

}

ProfilesRepository.java(JPA仓库)

package com.test;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ProfilesRepository extends CrudRepository<Profiles, Integer> {
    List<Profiles> findAll();
}

TestApplication.java(启动类)

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@SpringBootApplication(scanBasePackages = {"com.test"})
@EnableJpaRepositories(basePackages = {"com.test"})
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

}

pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.Test</groupId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <properties>
        <java.version>19</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
可能的原因及修复方案
  • JPA注解包版本不兼容:Spring Boot 3.x基于Jakarta EE 9,不再支持javax.persistence包,必须使用jakarta.persistence替代。你引入的javax.persistence-api依赖和Spring Boot 3的依赖冲突,导致实体类无法被正确识别,进而JPA仓库无法生成Bean。

    • 修复:删除pom.xml中的javax.persistence-api依赖;将Profiles.java中的import javax.persistence.*全部替换为import jakarta.persistence.*。
  • 重复JDBC依赖导致冲突:pom.xml中同时引入了spring-boot-starter-data-jdbc和spring-boot-starter-jdbc,两者功能重叠,会干扰Spring对JPA仓库的自动配置逻辑。

    • 修复:移除其中一个重复的JDBC依赖,建议删除spring-boot-starter-jdbc。
  • 包名大小写不一致:pom中的<groupId>com.Test</groupId>首字母大写,而代码中的包名是com.test全小写。虽然Java包名不区分大小写,但Maven和Spring在处理组件扫描、依赖识别时可能出现匹配问题。

    • 修复:将pom中的groupId改为<groupId>com.test</groupId>,保持与代码包名一致。
  • 重复定义CrudRepository方法:CrudRepository接口已经自带findAll()方法,你在ProfilesRepository中重复定义该方法,会导致Spring Data JPA无法正确生成代理实现类。

    • 修复:删除ProfilesRepository中的List<Profiles> findAll();方法,直接使用父接口的默认实现。
  • 启动类配置冗余(可选优化):@SpringBootApplication默认会扫描启动类所在包及其子包的组件和JPA仓库,你当前的类都在com.test包下,显式配置scanBasePackages和@EnableJpaRepositories属于冗余操作。可以简化启动类,只保留@SpringBootApplication注解。


内容的提问来源于stack exchange,提问作者Hello-Seb

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 23:55:21