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

Webflux+R2dbc-MySQL Demo启动失败:找不到UserRepository Bean

Webflux + R2dbc-MySQL Demo启动异常:找不到UserRepository Bean

环境信息

  • JDK 17
  • SpringBoot 3.1.0
  • r2dbc-mysql: 0.8.2.RELEASE
  • webflux: 3.1.0

问题描述

首次搭建Webflux与R2dbc-MySQL的Demo,启动时出现异常,提示UserController中注入的UserRepository类型Bean无法找到,应用启动失败。

项目结构

项目采用标准SpringBoot分层结构,代码分为以下子包:

  • com.example.r2dbcdemo.controller:控制器层
  • com.example.r2dbcdemo.entity:实体类层
  • com.example.r2dbcdemo.repo:数据访问层
    启动类位于根包com.example.r2dbcdemo下。

代码与配置

控制器代码(com.example.r2dbcdemo.controller)

package com.example.r2dbcdemo.controller;

import com.example.r2dbcdemo.entity.User;
import com.example.r2dbcdemo.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping(path = "/getAllUsers")
    public Flux<User> getAllUsers() {
        Flux<User> users = userRepository.findAll();
        return users;
    }

}

实体类代码(com.example.r2dbcdemo.entity)

package com.example.r2dbcdemo.entity;

import lombok.Data;
import org.springframework.data.relational.core.mapping.Table;

@Data
@Table("user")
public class User {
    private Long id;
    private String name;

}

仓库接口代码(com.example.r2dbcdemo.repo)

package com.example.r2dbcdemo.repo;

import com.example.r2dbcdemo.entity.User;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends ReactiveCrudRepository<User, Long> {
}

application.yml配置

spring:
    r2dbc:
        url: r2dbc:mysql://localhost:3306/my_blog_db?useSSL=false
        username: root
        password: root

启动类代码(com.example.r2dbcdemo)

package com.example.r2dbcdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration;
import org.springframework.web.reactive.config.EnableWebFlux;

@SpringBootApplication(exclude = R2dbcAutoConfiguration.class, scanBasePackages = {"com.example.r2dbcdemo"})
public class R2dbcDemoApplication {

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

}

报错信息

2023-06-12T22:49:11.533+08:00 ERROR 27576 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :

*************************** 应用启动失败 ***************************

描述:
com.example.r2dbcdemo.controller.UserController中的userRepository字段需要一个类型为'com.example.r2dbcdemo.repo.UserRepository'的Bean,但无法找到。

注入点带有以下注解:

  • @org.springframework.beans.factory.annotation.Autowired(required=true)

操作建议:
考虑在配置中定义一个类型为'com.example.r2dbcdemo.repo.UserRepository'的Bean。

进程以退出码1结束

问题排查与解决方案

核心问题原因

启动类中手动排除了R2dbcAutoConfiguration.class,导致SpringBoot无法自动初始化R2DBC相关的核心组件,包括为ReactiveCrudRepository接口生成代理实现Bean。

具体修复步骤

  1. 移除启动类中的R2dbc自动配置排除项
    修改启动类注解,删除exclude = R2dbcAutoConfiguration.class:

    @SpringBootApplication(scanBasePackages = {"com.example.r2dbcdemo"})
    @EnableWebFlux
    public class R2dbcDemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(R2dbcDemoApplication.class, args);
        }
    }
    
  2. 确保Repository接口被正确扫描

    • 启动类的scanBasePackages已经覆盖了com.example.r2dbcdemo包及其子包,SpringBoot会自动扫描该路径下的@Repository注解接口和ReactiveCrudRepository实现类。
    • 若需要更精确的扫描控制,可以在启动类上添加@EnableR2dbcRepositories(basePackages = "com.example.r2dbcdemo.repo")注解。
  3. 验证依赖完整性
    确保项目依赖中包含Spring Data R2DBC和R2DBC MySQL驱动,Maven示例依赖如下:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-r2dbc</artifactId>
    </dependency>
    <dependency>
        <groupId>dev.miku</groupId>
        <artifactId>r2dbc-mysql</artifactId>
        <version>0.8.2.RELEASE</version>
    </dependency>
    

内容的提问来源于stack exchange,提问作者MD.Luffy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 12:44:56