Update @Conditional processing logic
Defer @Conditional processing on @Configuration classes until the bean definitions are loaded, rather than when the @Configuration class is parsed. This provides better support for @Conditional implementations that inspect bean definitions. This commit also fixes some minor problems with original implementation and replaces the ConditionalAnnotationHelper class with ConditionEvaluator. Issue: SPR-10534
This commit is contained in:
@@ -46,20 +46,41 @@ public class ConfigurationClassWithConditionTests {
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void conditionalOnBeanMatch() throws Exception {
|
||||
public void conditionalOnMissingBeanMatch() throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(BeanOneConfiguration.class, BeanTwoConfiguration.class);
|
||||
ctx.refresh();
|
||||
assertTrue(ctx.containsBean("bean1"));
|
||||
assertFalse(ctx.containsBean("bean2"));
|
||||
assertFalse(ctx.containsBean("configurationClassWithConditionTests.BeanTwoConfiguration"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void conditionalOnMissingBeanNoMatch() throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(BeanTwoConfiguration.class);
|
||||
ctx.refresh();
|
||||
assertFalse(ctx.containsBean("bean1"));
|
||||
assertTrue(ctx.containsBean("bean2"));
|
||||
assertTrue(ctx.containsBean("configurationClassWithConditionTests.BeanTwoConfiguration"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void conditionalOnBeanMatch() throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(BeanOneConfiguration.class, BeanThreeConfiguration.class);
|
||||
ctx.refresh();
|
||||
assertTrue(ctx.containsBean("bean1"));
|
||||
assertTrue(ctx.containsBean("bean3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void conditionalOnBeanNoMatch() throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(BeanTwoConfiguration.class);
|
||||
ctx.register(BeanThreeConfiguration.class);
|
||||
ctx.refresh();
|
||||
assertTrue(ctx.containsBean("bean2"));
|
||||
assertFalse(ctx.containsBean("bean1"));
|
||||
assertFalse(ctx.containsBean("bean3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -105,6 +126,15 @@ public class ConfigurationClassWithConditionTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Conditional(HasBeanOneCondition.class)
|
||||
static class BeanThreeConfiguration {
|
||||
@Bean
|
||||
public ExampleBean bean3() {
|
||||
return new ExampleBean();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@MetaConditional("test")
|
||||
static class ConfigurationWithMetaCondition {
|
||||
@@ -135,6 +165,14 @@ public class ConfigurationClassWithConditionTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class HasBeanOneCondition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return context.getBeanFactory().containsBeanDefinition("bean1");
|
||||
}
|
||||
}
|
||||
|
||||
static class MetaConditionalFilter implements Condition {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
Reference in New Issue
Block a user