TransWikia.com

R2dbcDataAutoConfiguration conflicts with AbstractR2dbcConfiguration on bean 'r2dbcDatabaseClient' bean creation

Stack Overflow Asked by Shantanu Singh on December 9, 2021

Need help

@Configuration
@EnableR2dbcRepositories(basePackages = "com.paymentservice.repository", databaseClientRef = "databaseClient")
public class PaymentR2dbcConfiguration extends AbstractR2dbcConfiguration {

    @Value("${payment.data.mssql.host}")
    private String host;

    @Value("${payment.data.mssql.port}")
    private int port;

    @Value("${payment.data.mssql.database}")
    private String database;

    @Value("${payment.data.mssql.username}")
    private String username;

    @Value("${payment.data.mssql.password}")
    private String password;

    /**
     * An implementation of {@link ConnectionFactory} for creating connections to
     * a Microsoft SQL Server database using R2DBC.
     *
     * @return A factory for creating {@link Connection}s.
     */
    @Override
    public ConnectionFactory connectionFactory() {
        return new MssqlConnectionFactory(
            MssqlConnectionConfiguration.builder()
                .host(host)
                .port(port)
                .database(database)
                .username(username)
                .password(password).build());
    }
}

I m getting

quote The bean ‘r2dbcDatabaseClient’, defined in class path resource [org/springframework/boot/autoconfigure/data/r2dbc/R2dbcDataAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/paymentservice/configurations/PaymentR2dbcConfiguration.class] and overriding is disabled.

I m using

       <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-r2dbc</artifactId>
            <version>1.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-mssql</artifactId>
            <version>0.8.4.RELEASE</version>
        </dependency>

3 Answers

If you are using Spring Boot starter to configure R2dbc automatically.And you want to customize the R2dbc configuration via AbstractR2dbcConfiguration.

  1. If you are using more than one connection in your application, and use this config to add another connection factory, try to add a name attribute to @Bean to identify multiple beans.

     @Bean(name="myConn")
     @Override
     public ConnectionFactory connectionFactory() {}
    
  2. If you want to override the default ConnectionFactory created by Spring Boot starter, add an extra @Primary on it.

     @Bean
     @Primary
     @Override
     public ConnectionFactory connectionFactory() {}
    

Answered by Hantsy on December 9, 2021

this is an open issue in Spring-boot and spring-data-r2dbc see post:

https://github.com/spring-projects/spring-data-r2dbc/issues/296

and https://github.com/spring-projects/spring-boot/issues/21586

The solution which works for me is to use default R2dbcDataAutoConfiguration and remove the custom configuration AbstractR2dbcConfiguration until Spring team fixed it.

Below properties are used to initialize the R2dbcDataAutoConfiguration

spring:
  r2dbc:
    url: r2dbc:mssql://xx.xxx.xxx.xxx:6515/*****
    username: xxxx_xxxx
    password: xxxxx

The second solution which works for me is to exclude R2dbcDataAutoConfiguration using below code.

@SpringBootApplication(exclude = { R2dbcDataAutoConfiguration.class,
 R2dbcAutoConfiguration.class })

Answered by Shantanu Singh on December 9, 2021

You have missed to annotate the method connectionFactory with Bean. Check the documentation here

    @Bean
    @Override
    public ConnectionFactory connectionFactory() {
        return new MssqlConnectionFactory(
            MssqlConnectionConfiguration.builder()
                .host(host)
                .port(port)
                .database(database)
                .username(username)
                .password(password).build());
    }

If the problem still persists in case you have any overriding bean defined in any of your configuration class, then you should use this property:

spring.main.allow-bean-definition-overriding=true

Starting in Spring 5.1, the BeanDefinitionOverrideException was introduced to allow developers to automatically throw the exception to prevent any unexpected bean overriding.

Answered by Abhinaba Chakraborty on December 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP