Spring在Service中找不到Repository bean

问题描述 投票:0回答:1

我在启动应用程序时遇到问题:

Description:

Parameter 0 of constructor in com.backend.services.RoleService required a bean of type 'com.backend.repository.RoleRepository' that could not be found.


Action:

Consider defining a bean of type 'com.backend.repository.RoleRepository' in your configuration.

有人遇到过这个问题吗?

项目结构:

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

实体:

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;
}

枚举:

package com.backend.enums;

public enum ERole {
    USER,
    BUSINESS,
    ADMIN
}

服务:

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);
    }

}

存储库:

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);
}

我尝试过的:

  1. 添加@Autowired,我收到这样的错误

  2. 在BackendApplication中添加包扫描注释,我得到了类似的错误

我使用gradlew:

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

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

java {
    sourceCompatibility = '17'
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

ext {
    set('snippetsDir', file("build/generated-snippets"))
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'me.paulschwarz:spring-dotenv:4.0.0'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-validation:3.1.5'
    implementation 'org.mapstruct:mapstruct-processor:1.6.0.Beta1'
    implementation 'org.mapstruct:mapstruct:1.6.0.Beta1'
    implementation 'com.auth0:java-jwt:4.4.0'
    implementation 'io.jsonwebtoken:jjwt:0.12.3'
    implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
    runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.3'
    runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.3'
    implementation 'org.flywaydb:flyway-core'
    runtimeOnly 'org.flywaydb:flyway-mysql'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'com.h2database:h2'
    runtimeOnly 'com.mysql:mysql-connector-j'
    runtimeOnly 'io.asyncer:r2dbc-mysql'
    runtimeOnly 'io.r2dbc:r2dbc-h2'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
    testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
    testImplementation 'org.springframework.security:spring-security-test'
}

tasks.named('test') {
    outputs.dir snippetsDir
    useJUnitPlatform()
}

tasks.named('asciidoctor') {
    inputs.dir snippetsDir
    dependsOn test
}

和申请文件:

package com.backend;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

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

}
java spring spring-boot spring-jdbc
1个回答
0
投票

好吧,整个问题是 Spring 无法决定如何创建您的 Repository 类,因为您的项目中实现了多个

starter-data
项目。也就是说,您使用 MySQL 作为您的供应商,并且要在 MySQL 中使用 ORM,仅 JPA 就足够了,但是出于任何原因,例如您想要编写清晰的查询,您可能还想添加 JDBC,但删除 R2DBC 实现和不需要的依赖项中的驱动程序解决了您的问题。

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'me.paulschwarz:spring-dotenv:4.0.0'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation:3.1.5'
implementation 'org.mapstruct:mapstruct-processor:1.6.0.Beta1'
implementation 'org.mapstruct:mapstruct:1.6.0.Beta1'
implementation 'com.auth0:java-jwt:4.4.0'
implementation 'io.jsonwebtoken:jjwt:0.12.3'
implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.3'
implementation 'org.flywaydb:flyway-core'
runtimeOnly 'org.flywaydb:flyway-mysql'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
testImplementation 'org.springframework.security:spring-security-test'}

这将运行您的项目

还有一件事,我不确定Spring是否提供特定于引擎的方言,但是5InnoDB方言肯定不会工作,那么你可能想实现一个特定于引擎的驱动程序,否则你可以简单地使用

MySQLDialect
,你的项目就会开始运行,没有任何问题。

问题在于 Spring 不知道应该以哪种风格创建您的存储库类。因此未创建它,这后来反映在未找到 bean 异常中。

希望您理解这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.