2

I'm trying to setup, a PoC of Spring Boot application with multiple databases. Which db should be used is determined by a Spring Profile. One of this DB's is MongoDb to which I am connecting using Spring Data Reactive.

Because I want the application to connect to mongo only when a certain profile is active, I've disabled MongoAutoconfiguration and MongoReactiveAutoConfiguration.

@SpringBootApplication(exclude = {
        MongoAutoConfiguration.class,
        MongoDataAutoConfiguration.class,
        MongoReactiveAutoConfiguration.class
})
public class DndApplication {}

I've set up a DbConfig, which extends AbstractReactiveMongoConfiguration and is only used when a mongo profile is active and has EnableReactiveMongoRepositories annotation

@Profile(value = Array("mongo"))
@Configuration
@EnableReactiveMongoRepositories
class DbConfig @Autowired()(private val mongoProperties: MongoProperties) extends AbstractReactiveMongoConfiguration {

  override def reactiveMongoClient(): MongoClient = {
    MongoClients.create()
  };

  override def getDatabaseName: String = "dnd-character"
}

With only that, the application works fine, the connection to mongo is created only when mongo profile is active. However, when I try to use a ReactiveCrudRepository, for example:

@Component
@Profile(value = Array("mongo"))
class MongoCharacterDao @Autowired()(private val repository: CharacterCrudRepository) extends CharacterDao {}

and

@Profile(value = Array("mongo"))
trait CharacterCrudRepository extends ReactiveCrudRepository[MongoCharacterEntity, MongoCharacterId]

then it cannot find the CharacterCrudRepository Bean and fails on startup

Parameter 0 of constructor in com.velocit.dnd.character.persistence.mongo.MongoCharacterDao required a bean of type 'com.velocit.dnd.character.persistence.mongo.CharacterCrudRepository' that could not be found.

When I remove the exclusion of autoconfiguration, the repository works fine, but I cannot limit it only to mongo profile (the application tries to connect to mongo even if all of the mongo beans are restricted to a profile).

MF57
  • 21
  • 1

0 Answers0