question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple bean definitions (Conflicts)

See original GitHub issue

Your documentation says “latest bean wins” (See https://insert-koin.io/docs/1.0/reference/koin-dsl/?#multiple-definitions)

The source code throws an exception if there are multiple beans: see https://github.com/Ekito/koin/blob/0.9.1/koin-core/src/main/kotlin/org/koin/KoinContext.kt#L121

In case future readers face this,

My use case: I want to override/replace/shadow specific beans with mocks/spies in my unit/integration tests.

My solution was to

  • Register all the normal application beans (eg. via startKoin)
  • Create a BeanDefinition instance for each mock/spy/fake
  • Register them manually koin.beanRegistry.declare( theBeanDef , Scope.root())

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
arnaudgiulianicommented, Apr 4, 2018

If you want to reuse a module and override some definitions for your test, just make a module containing your new definitions and load modules in order: your app + your test module

The last module will override needed definitions

A bean is overriden when: same name, type and scope

You can match multiple bean for a given type if you use bind oeprators then

0reactions
wcarmoncommented, Apr 7, 2018

Of course you are correct about KoinTest being an interface. Clearly I didn’t look into KoinTest at all.

As you mentioned, the root cause appears to be a choice of eager-injection versus lazy-injection.

I still prefer “immutable-after-construction” & eager injection, so I’ll use the workaround above.

Thanks for the helpful information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to resolve this conflicting of two beans with same name in ...
Use the @Primary annotation on the bean defenition on the configuration class @Bean(name = "beanOnA") @Primary public YourInterface ...
Read more >
When two beans collide - Tratif
When you have two beans with the same name in different configuration classes but without qualifiers — in such a case the application...
Read more >
Instantiating Multiple Beans of the Same Class with Spring ...
In this article, we've learned about instantiating multiple beans of the same class using Spring annotations using three different approaches.
Read more >
5. The IoC container - Spring
It can be useful to have bean definitions span multiple XML files. ... It detects configuration problems, such as references to non-existent beans...
Read more >
Failing to run tests because of Conflicting Bean Definition ...
it was heavily due to conflicting bean definitions , that is between xml definitions in pre-2.2 core version and Annotation definition in 2.2 ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found