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

Spring启动报错:Service中无法找到Repository Bean问题解决

Spring启动报错:RoleRepository Bean未找到(已解决)

报错信息

描述:
com.backend.services.RoleService构造函数的第0个参数需要类型为'com.backend.repository.RoleRepository'的Bean,但未找到。

操作建议:
考虑在配置中定义类型为'com.backend.repository.RoleRepository'的Bean。

项目结构

com.backend 包结构:

  • entity.Role
  • enums.ERole
  • repository.RoleRepository
  • services.RoleService

相关代码

Application.yml(开发环境)

spring:
  config:
    activate:
      on-profile: "dev"
  datasource:
    url: jdbc:mysql://localhost:3306/${MYSQL_DATABASE}
    username: ${MYSQL_USER}
    password: ${MYSQL_PASSWORD}
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true
    properties:
      hibernate:
        dialect: "org.hibernate.dialect.MySQL5InnoDBDialect"
  flyway:
    enabled: true
    url: jdbc:mysql://localhost:3306/${MYSQL_DATABASE}
    locations: classpath:db/migration
    user: ${MYSQL_USER}
    password: ${MYSQL_PASSWORD}
    create-schemas: true
    default-schema: ${MYSQL_DATABASE}
#logging:
#  level:
#    root: debug

jwtSecret: "superkode"
jwtExpirationMs: 86400000
jwtRefreshExpirationMs: 120000

Entity:Role.java

package com.backend.entity;

import com.backend.enums.ERole;
import jakarta.persistence.*;
import lombok.*;

import java.math.BigInteger;

@Entity
@Table(name = "roles")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(length = 20, nullable = false)
    private ERole name;
}

Enum:ERole.java

package com.backend.enums;

public enum ERole {
    USER,
    BUSINESS,
    ADMIN
}

Service:RoleService.java

package com.backend.services;


import com.backend.entity.Role;
import com.backend.enums.ERole;
import com.backend.repository.RoleRepository;

import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class RoleService {
    private final RoleRepository roleRepository;

    public RoleService(RoleRepository roleRepository) {
        this.roleRepository = roleRepository;
    }

    public Optional<Role> findByName(ERole name) {
        return roleRepository.findByName(name);
    }

}

Repository:RoleRepository.java

package com.backend.repository;

import com.backend.entity.Role;
import com.backend.enums.ERole;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface RoleRepository extends JpaRepository<Role, Long> {
    Optional<Role> findByName(ERole name);
}

尝试过的解决方法

  • 添加@Autowired注解,仍出现错误;
  • 在BackendApplication中添加包扫描注解,仍出现类似错误;

最终解决方案

项目使用Gradle构建,原依赖存在数据库相关冲突,重构Gradle依赖、移除冲突项后问题解决。

重构后的Gradle配置

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.5'
    id 'io.spring.dependency-management' version '1.1.3'
}

group = 'com.backend'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '17'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web:3.1.5'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.1.5'
    implementation 'org.springframework.boot:spring-boot-starter-security:3.1.5'
    implementation 'org.springframework.boot:spring-boot-starter-validation:3.1.5'
    implementation 'com.mysql:mysql-connector-j:8.2.0'
    implementation 'com.auth0:java-jwt:4.4.0'
    implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
    implementation 'me.paulschwarz:spring-dotenv:4.0.0'
    implementation 'org.flywaydb:flyway-core:10.0.0'
    implementation 'org.flywaydb:flyway-mysql:10.0.0'
    compileOnly 'org.projectlombok:lombok:1.18.30'
    runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.3'
    runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.3'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 19:47:40