0

When I first tried to use webflux and r2dbc-mysql, I encountered a strange problem.This is my project structure diagram: enter image description here

package 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> {
}

And the application.yml

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

This is the main funcation:

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.context.annotation.ComponentScan;
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);
    }

}

This is the first time for me to use webflux and r2bdc. I thought this demo could run normally, but the following error was thrown during startup. This operation result has troubled me for a long time, please help me!

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

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


Description:

Field userRepository in com.example.r2dbcdemo.controller.UserController required a bean of type 'com.example.r2dbcdemo.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.example.r2dbcdemo.repo.UserRepository' in your configuration.

Process finished with exit code 1

ps: jdk17,springboot3.1.0,r2dbc-mysql:0.8.2.RELEASE,webflux:3.1.0.pom

MD.Luffy
  • 35
  • 5
  • `exclude = R2dbcAutoConfiguration.class` why have you removed the R2dbcAutoConfiguration? this is exactly what sets up your repos – Toerktumlare Jun 12 '23 at 21:43
  • I didn't add this at first, but it would report an exception like this.to fix it, so I added this, which then led to the above problem – MD.Luffy Jun 13 '23 at 01:38
  • --------------------------------- APPLICATION FAILED TO START --------------------------------- *Description*: Failed to configure a ConnectionFactory: 'url' attribute is not specified and no embedded database could be configured. *Reason*: Failed to determine a suitable R2DBC Connection URL *Action*: Consider the following: If you want an embedded database (H2), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active). – MD.Luffy Jun 13 '23 at 01:41
  • Read that error message, its crystal clear – Toerktumlare Jun 13 '23 at 05:48

0 Answers0