Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2148,7 +2148,8 @@ private Object resolveValue(BeanResolutionContext resolutionContext, BeanContext
if (isIterable() && getAnnotationMetadata().hasDeclaredAnnotation(EachBean.class)) {
throw new DisabledBeanException("Bean [" + getBeanType().getSimpleName() + "] disabled by parent: " + e.getMessage());
} else {
throw new DependencyInjectionException(resolutionContext, e);
// Propagate DisabledBeanException as-is so upstream logic can handle disabled dependencies
throw e;
}
} catch (NoSuchBeanException e) {
throw new DependencyInjectionException(resolutionContext, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.micronaut.disabledbean

import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Factory
import io.micronaut.context.annotation.Requires
import io.micronaut.context.exceptions.DisabledBeanException
import jakarta.inject.Singleton
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class DisabledBeanRequiresTest {
@Test
fun testConditionalMakeAnotherBeanUnavailable() {
val ctx = ApplicationContext.run()
try {
val opt = ctx.findBean(AnotherBean::class.java)
// Expected: AnotherBean should not be available because MyBean is disabled via DisabledBeanException
assertTrue(opt.isEmpty, "AnotherBean should not be available when MyBean is disabled")
} finally {
ctx.close()
}
}
}

class MyBean

class AnotherBean(val myBean: MyBean)

@Factory
class MyFactory {
@Singleton
fun myBean(): MyBean {
throw DisabledBeanException("MyBean Disabled")
}

@Singleton
@Requires(bean = MyBean::class)
fun anotherBean(myBean: MyBean): AnotherBean {
return AnotherBean(myBean)
}
}