R2dbcDataAutoConfiguration conflicts with AbstractR2dbcConfiguration on bean 'r2dbcDatabaseClient' bean creation

huangapple go评论96阅读模式
英文:

R2dbcDataAutoConfiguration conflicts with AbstractR2dbcConfiguration on bean 'r2dbcDatabaseClient' bean creation

问题

@刷新范围
@配置
@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;

    /**
     * 用于创建使用 R2DBC 连接到 Microsoft SQL Server 数据库的连接工厂的实现。
     *
     * @return 用于创建连接的工厂。
     */
    @Override
    public ConnectionFactory connectionFactory() {
        return new MssqlConnectionFactory(
            MssqlConnectionConfiguration.builder()
                .host(host)
                .port(port)
                .database(database)
                .username(username)
                .password(password).build());
    }
}

我遇到了以下问题:

>引用错误:在类路径资源 [org/springframework/boot/autoconfigure/data/r2dbc/R2dbcDataAutoConfiguration.class] 中定义的 bean 'r2dbcDatabaseClient' 无法注册。已经在类路径资源 [com/paymentservice/configurations/PaymentR2dbcConfiguration.class] 中定义了具有该名称的 bean,并且禁用了覆盖。

我使用了以下依赖项:

<依赖>
    <组ID>org.springframework.data</组ID>
    <构件ID>spring-data-r2dbc</构件ID>
    <版本>1.1.1.RELEASE</版本>
</依赖>

<依赖>
    <组ID>io.r2dbc</组ID>
    <构件ID>r2dbc-mssql</构件ID>
    <版本>0.8.4.RELEASE</版本>
</依赖>
英文:

Need help

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

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

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

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

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

    @Value(&quot;${payment.data.mssql.password}&quot;)
    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

       &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
            &lt;artifactId&gt;spring-data-r2dbc&lt;/artifactId&gt;
            &lt;version&gt;1.1.1.RELEASE&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;io.r2dbc&lt;/groupId&gt;
            &lt;artifactId&gt;r2dbc-mssql&lt;/artifactId&gt;
            &lt;version&gt;0.8.4.RELEASE&lt;/version&gt;
        &lt;/dependency&gt;

答案1

得分: 1

如果您正在使用Spring Boot Starter自动配置R2DBC,并且希望通过AbstractR2dbcConfiguration自定义R2DBC配置。

  1. 如果您的应用程序中使用了多个连接,并且使用此配置添加另一个连接工厂,请尝试在@Bean上添加一个name属性以标识多个bean。

     @Bean(name="myConn")
     @Override
     public ConnectionFactory connectionFactory() {}
    
  2. 如果要覆盖Spring Boot Starter创建的默认ConnectionFactory,请在其上额外添加@Primary注解。

     @Bean
     @Primary
     @Override
     public ConnectionFactory connectionFactory() {}
    
英文:

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=&quot;myConn&quot;)
     @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() {}
    

答案2

得分: 0

你忘记为方法 connectionFactory 添加 Bean 注解。
请查阅文档 此处

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

如果问题仍然存在,假如你在任何配置类中定义了覆盖的 bean,那么你应该使用此属性:

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

从 Spring 5.1 开始,引入了 BeanDefinitionOverrideException 来允许开发者自动抛出异常,以防止意外的 bean 覆盖。

英文:

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.

答案3

得分: -1

这是在Spring Boot和spring-data-r2dbc中的一个开放问题,请查看帖子:

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

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

对我有效的解决方案是使用默认的 R2dbcDataAutoConfiguration 并移除自定义配置 AbstractR2dbcConfiguration,直到Spring团队修复为止。

以下属性用于初始化 R2dbcDataAutoConfiguration

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

第二个对我有效的解决方案是使用以下代码排除 R2dbcDataAutoConfiguration

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

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 })

huangapple
  • 本文由 发表于 2020年7月23日 17:05:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63050683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定