When I first tried to use webflux and r2dbc-mysql, I encountered a strange problem.This is my project structure diagram:
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