0

I want to register bean (MyBean) only if another bean (anotherBeanThatShouldBePresent) is present in the context.

How I can achieve that?

bean {
    MyBean(
        anotherBeanThatShouldBePresent = ref()
    )
}
pixel
  • 24,905
  • 36
  • 149
  • 251

1 Answers1

1

You can use ObjectProvider to create bean depending on another bean

bean {
    provider<OtherBeanOnWhichIDepend>().ifAvailable {
        bean<MyCustomBean>()
    }
}

With this code I will register MyCustomBean only if OtherBeanOnWhichIDepend bean is available

szymon_prz
  • 540
  • 3
  • 14