Using Spring configuration to instanciate a service several times with different dependencies

46 Views Asked by At

Hello Stackoverflowers (Spring is coming, better bloom )

While trying to make my application modular, I encountered a lot of problem which I can't get my head arround.

Let's say that I have my domain which is already implemented and this domain is requiring some dependencies to be provided.

What I'm trying to achieve is having this domain (and its services) to be instanciated several times but with different dependencies.

I declared a configuration class for my domain.

class MyDomainConfig {

    @Bean
    fun doSomethingcommandHandler(
        eventBus: EventBus,
        commandBus: CommandBus,
        useCase: UseCase<DoSomething>,
        someRepo: SomeRepository
    ): CommandHandler<DoSomething> {
        return CommandHandler(eventBus, commandBus, useCase, DoSomething::class.java, someRepo)
    }

    @Bean
    fun doSomethingElseCommandHandler(
    eventBus: EventBus,
    commandBus: CommandBus,
    useCase: UseCase<DoSomethingElse>,
    someRepo: SomeRepository
    ): CommandHandler<DoSomethingElse> {
        return CommandHandler(eventBus, commandBus, useCase, DoSomethingElse::class.java, someRepo)
    }

}

Basically, the required dependencies should always be provided. I would have seen this configuration class as a skeleton or prototype for their instanciation. Note that I did not declare the Configuration class as a configuration. This is because I thought about using it as an importable class in my actual configuration.

Now I created dedicated configuration which I thought would instanciate my domain with the provided dependencies.

Like so

@Configuration
@Import(
    MyDomainConfig::class,
)
class OfflineConfig {

    @Bean
    fun eventBus(): EventBus {
        return InMemoryEventBus()
    }

    @Bean
    fun commandBus(): CommandBus {
        return InMemoryCommandBus()
    }

}


@Configuration
@Import(
    MyDomainConfig::class,
)
class OnlineConfig {

    @Bean
    fun eventBus(): EventBus {
        return OnlineEventBus()
    }

    @Bean
    fun commandBus(): CommandBus {
        return OnlineCommandBus()
    }

}

It obviously does not work as I expected. Spring complains because it sees 2 beans eventBus. I get why, but I can't figure out a way of achieving what I want. Here is the error:

The bean 'eventBus', defined in class path resource [com/demo/app/online/OnlineConfig.class], could not be registered. A bean with that name has already been defined in class path resource [com/demo/app/offline/OfflineConfig.class] and overriding is disabled.

Profiles are not an options as I actually need both Configuration to generate an up an running domain with a given set of dependencies.

How would you instanciate services of the domain several time with different dependencies and make those available for application layer ? Or having one isolated contexts per declared configuration ?

I also tried Modulith and having different runner but I still encounter bean instanciations issues.

Thanks

0

There are 0 best solutions below