I need to configure 2 MongoDB databases with my Spring Boot v3.0.5 application.
I have made 2 MongoDB configurations as follows:
RemindersConfig.java
@Configuration
@EnableReactiveMongoRepositories(basePackages = "com.shourya.learn.repository")
public class RemindersConfig extends AbstractReactiveMongoConfiguration {
@Value("${mongodb.reminders.database}")
private String database;
@Bean(name = "reminders")
public MongoClient mongoClient() {
String uri = "mongodb://localhost:27017/" + database;
return MongoClients.create(uri);
}
@Override
protected String getDatabaseName() {
return database;
}
}
NotificationsConfig.java
@Configuration
@EnableReactiveMongoRepositories(basePackages = "com.shourya.learn.repository")
public class NotificationsConfig extends AbstractReactiveMongoConfiguration {
@Value("${mongodb.notifications.database}")
private String database;
@Bean(name = "notifications")
public MongoClient mongoClient() {
String uri = "mongodb://localhost:27017/" + database;
return MongoClients.create(uri);
}
@Override
protected String getDatabaseName() {
return database;
}
}
Repositories:
NotificationRepository.java
@Repository
public interface NotificationRepository extends ReactiveMongoRepository<NotificationModel, String> {
}
ReminderRepository.java
@Repository("reminderRepository")
public interface ReminderRepository extends ReactiveMongoRepository<ReminderModel, String> {
}
They run fine individually but when run together they give the following error:
Description:
The bean 'reminderRepository', defined in com.shourya.learn.repository.ReminderRepository defined in @EnableReactiveMongoRepositories declared on RemindersConfig, could not be registered. A bean with that name has already been defined in com.shourya.learn.repository.ReminderRepository defined in @EnableReactiveMongoRepositories declared on NotificationsConfig and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true