Spring data r2dbc: Problem connecting to MySQL - Repository bean not found

2.5k Views Asked by At

I am trying to integrate MySql to spring boot application using r2dbc driver. While doing so encountering issue wherein the repository bean is not getting created. I saw similar questions but the approaches mentioned in those didn't help.

Error message below:

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

Description:

Parameter 4 of constructor in com.abc.studentservice.utils.impl.HostelImpl required a bean of type 'com.abc.studentservice.repository.StudentRepository' that could not be found.


Action:

Consider defining a bean of type 'com.abc.studentservice.repository.StudentRepository' in your configuration.

application.yaml: Tried to define spring.r2dbc.pool.enabled: false and spring.r2dbc.pool.enabled:. But both of this didn't help

spring:
  profiles:
    active: devo
  r2dbc:
    url: r2dbc:pool:mysql://localhost/student
    username: mysql
    password: mysql
    pool:
      initial-size: 10
      max-size: 50
      max-idle-time: 30m
      validation-query: SELECT 1
  data:
    r2dbc:
      repositories:
        enabled: true

Maven dependencies


    <!--  Springboot data -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-r2dbc</artifactId>
      <version>2.4.5</version>
    </dependency>

    <!--  Enable connection pooling -->
    <dependency>
      <groupId>io.r2dbc</groupId>
      <artifactId>r2dbc-pool</artifactId>
      <version>0.8.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.3.6</version>
    </dependency>


    <!--  Reactive Mysql -->
    <dependency>
      <groupId>dev.miku</groupId>
      <artifactId>r2dbc-mysql</artifactId>
      <version>0.8.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.23</version>
    </dependency>

Repository

@Repository
public interface StudentRepository extends ReactiveCrudRepository<Student, UUID> {
}

Student entity


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table("student")
public class Student {
    @Id
    @Column("id")
    private UUID id;

    @Column("first_name")
    private String firstName;

    @Column("last_name")
    private String lastName;
}

Main class I used @EnableR2dbcRepositories as well below but it didn't help much and getting same issue

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

Any help would be appreciated.

2

There are 2 best solutions below

1
On

You are using UUID as id in the Student POJO, but in repository definition you use Long. Change it to UUID as follows

 ReactiveCrudRepository<Student, UUID>
1
On

In your StudentRepository interface, extend R2dbcRepository instead of ReactiveCrudRepository.

I recently had the same issue and that fixed it.