Properly honor conditions on overriding bean methods

Issue: SPR-12694
This commit is contained in:
Juergen Hoeller
2015-02-10 19:26:19 +01:00
parent 918bc3b103
commit 981aefc2c0
3 changed files with 73 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -39,6 +39,7 @@ import static org.junit.Assert.*;
* Test for {@link Conditional} beans.
*
* @author Phillip Webb
* @author Juergen Hoeller
*/
@SuppressWarnings("resource")
public class ConfigurationClassWithConditionTests {
@@ -117,6 +118,19 @@ public class ConfigurationClassWithConditionTests {
ctx.refresh();
}
@Test
public void conditionOnOverriddenMethodHonored() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigWithBeanSkipped.class);
assertEquals(0, context.getBeansOfType(ExampleBean.class).size());
}
@Test
public void noConditionOnOverriddenMethodHonored() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigWithBeanReactivated.class);
assertEquals(1, context.getBeansOfType(ExampleBean.class).size());
}
@Configuration
static class BeanOneConfiguration {
@Bean
@@ -197,6 +211,7 @@ public class ConfigurationClassWithConditionTests {
}
static class NeverCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return false;
@@ -261,4 +276,32 @@ public class ConfigurationClassWithConditionTests {
static class ExampleBean {
}
@Configuration
private static class ConfigWithBeanActive {
@Bean
public ExampleBean baz() {
return new ExampleBean();
}
}
private static class ConfigWithBeanSkipped extends ConfigWithBeanActive {
@Override
@Bean
@Conditional(NeverCondition.class)
public ExampleBean baz() {
return new ExampleBean();
}
}
private static class ConfigWithBeanReactivated extends ConfigWithBeanSkipped {
@Override
@Bean
public ExampleBean baz() {
return new ExampleBean();
}
}
}