Here's a test that illustrates the problem:
class ConditionInheritanceTests {
@Test
void test() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
context.register(Importer.class);
context.refresh();
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> context.getBean(DirectlyAnnotatedConfiguration.class));
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> context.getBean(OnlySuperClassAnnotatedConfiguration.class));
}
}
@Import({ DirectlyAnnotatedConfiguration.class, OnlySuperClassAnnotatedConfiguration.class })
static class Importer {
}
@Configuration
@Conditional(TestCondition.class)
static class BaseConfiguration {
}
@Conditional(TestCondition.class)
static class DirectlyAnnotatedConfiguration extends BaseConfiguration {
}
static class OnlySuperClassAnnotatedConfiguration extends BaseConfiguration {
}
static class TestCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return false;
}
}
}
My expectation is that neither DirectlyAnnotatedConfiguration
nor OnlySuperClassAnnotatedConfiguration
will be registered in the context. The actual behavior is that OnlySuperClassAnnotatedConfiguration
is registered so the second assertion in the test fails.
Comment From: jhoeller
According to the @Conditional
javadoc, it's not supported on superclasses by design. Are we accidentally supporting it on superclasses in some scenario, or is it consistently not detected on superclasses?
Comment From: wilkinsona
Oops. No, sorry. I was discussing it with Stephane and he expressed some surprise that it didn't work. Evidently, neither of us checked the javadoc to refresh our memories.