英文:
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 = "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>
答案1
得分: 1
如果您正在使用Spring Boot Starter自动配置R2DBC,并且希望通过AbstractR2dbcConfiguration
自定义R2DBC配置。
-
如果您的应用程序中使用了多个连接,并且使用此配置添加另一个连接工厂,请尝试在
@Bean
上添加一个name
属性以标识多个bean。@Bean(name="myConn") @Override public ConnectionFactory connectionFactory() {}
-
如果要覆盖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
.
-
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() {}
-
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 })
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论