pull/14950/head
Phillip Webb 6 years ago
parent 34156b2137
commit 1bd1ffdb60

@ -83,27 +83,6 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
this.beanFactory = beanFactory; this.beanFactory = beanFactory;
} }
/**
* Factory method to get the {@link BeanTypeRegistry} for a given {@link BeanFactory}.
* @param beanFactory the source bean factory
* @return the {@link BeanTypeRegistry} for the given bean factory
*/
static BeanTypeRegistry get(ListableBeanFactory beanFactory) {
Assert.isInstanceOf(DefaultListableBeanFactory.class, beanFactory);
DefaultListableBeanFactory listableBeanFactory = (DefaultListableBeanFactory) beanFactory;
Assert.isTrue(listableBeanFactory.isAllowEagerClassLoading(),
"Bean factory must allow eager class loading");
if (!listableBeanFactory.containsLocalBean(BEAN_NAME)) {
BeanDefinition bd = BeanDefinitionBuilder
.genericBeanDefinition(BeanTypeRegistry.class,
() -> new BeanTypeRegistry(
(DefaultListableBeanFactory) beanFactory))
.getBeanDefinition();
listableBeanFactory.registerBeanDefinition(BEAN_NAME, bd);
}
return listableBeanFactory.getBean(BEAN_NAME, BeanTypeRegistry.class);
}
/** /**
* Return the names of beans matching the given type (including subclasses), judging * Return the names of beans matching the given type (including subclasses), judging
* from either bean definitions or the value of {@link FactoryBean#getObjectType()} in * from either bean definitions or the value of {@link FactoryBean#getObjectType()} in
@ -113,7 +92,7 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
* @return the names of beans (or objects created by FactoryBeans) matching the given * @return the names of beans (or objects created by FactoryBeans) matching the given
* object type (including subclasses), or an empty set if none * object type (including subclasses), or an empty set if none
*/ */
Set<String> getNamesForType(Class<?> type) { public Set<String> getNamesForType(Class<?> type) {
updateTypesIfNecessary(); updateTypesIfNecessary();
return this.beanTypes.entrySet().stream() return this.beanTypes.entrySet().stream()
.filter((entry) -> entry.getValue() != null .filter((entry) -> entry.getValue() != null
@ -131,7 +110,7 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
* @return the names of beans (or objects created by FactoryBeans) annotated with the * @return the names of beans (or objects created by FactoryBeans) annotated with the
* given annotation, or an empty set if none * given annotation, or an empty set if none
*/ */
Set<String> getNamesForAnnotation(Class<? extends Annotation> annotation) { public Set<String> getNamesForAnnotation(Class<? extends Annotation> annotation) {
updateTypesIfNecessary(); updateTypesIfNecessary();
return this.beanTypes.entrySet().stream() return this.beanTypes.entrySet().stream()
.filter((entry) -> entry.getValue() != null && AnnotationUtils .filter((entry) -> entry.getValue() != null && AnnotationUtils
@ -352,4 +331,25 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
return null; return null;
} }
/**
* Factory method to get the {@link BeanTypeRegistry} for a given {@link BeanFactory}.
* @param beanFactory the source bean factory
* @return the {@link BeanTypeRegistry} for the given bean factory
*/
static BeanTypeRegistry get(ListableBeanFactory beanFactory) {
Assert.isInstanceOf(DefaultListableBeanFactory.class, beanFactory);
DefaultListableBeanFactory listableBeanFactory = (DefaultListableBeanFactory) beanFactory;
Assert.isTrue(listableBeanFactory.isAllowEagerClassLoading(),
"Bean factory must allow eager class loading");
if (!listableBeanFactory.containsLocalBean(BEAN_NAME)) {
BeanDefinition bd = BeanDefinitionBuilder
.genericBeanDefinition(BeanTypeRegistry.class,
() -> new BeanTypeRegistry(
(DefaultListableBeanFactory) beanFactory))
.getBeanDefinition();
listableBeanFactory.registerBeanDefinition(BEAN_NAME, bd);
}
return listableBeanFactory.getBean(BEAN_NAME, BeanTypeRegistry.class);
}
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.util.Date; import java.util.Date;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
@ -61,40 +62,38 @@ public class ConditionalOnMissingBeanTests {
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@After
public void cleanup() {
this.context.close();
}
@Test @Test
public void testNameOnMissingBeanCondition() { public void testNameOnMissingBeanCondition() {
this.context.register(FooConfiguration.class, OnBeanNameConfiguration.class); load(FooConfiguration.class, OnBeanNameConfiguration.class);
this.context.refresh();
assertThat(this.context.containsBean("bar")).isFalse(); assertThat(this.context.containsBean("bar")).isFalse();
assertThat(this.context.getBean("foo")).isEqualTo("foo"); assertThat(this.context.getBean("foo")).isEqualTo("foo");
} }
@Test @Test
public void testNameOnMissingBeanConditionReverseOrder() { public void testNameOnMissingBeanConditionReverseOrder() {
this.context.register(OnBeanNameConfiguration.class, FooConfiguration.class); load(OnBeanNameConfiguration.class, FooConfiguration.class);
this.context.refresh(); // Ideally this would be false, but the ordering is a problem
// FIXME: ideally this would be false, but the ordering is a problem
assertThat(this.context.containsBean("bar")).isTrue(); assertThat(this.context.containsBean("bar")).isTrue();
assertThat(this.context.getBean("foo")).isEqualTo("foo"); assertThat(this.context.getBean("foo")).isEqualTo("foo");
} }
@Test @Test
public void testNameAndTypeOnMissingBeanCondition() { public void testNameAndTypeOnMissingBeanCondition() {
this.context.register(FooConfiguration.class, load(FooConfiguration.class, OnBeanNameAndTypeConfiguration.class);
OnBeanNameAndTypeConfiguration.class); // Arguably this should be true, but as things are implemented the conditions
this.context.refresh(); // specified in the different attributes of @ConditionalOnBean are combined with
/* // logical OR (not AND) so if any of them match the condition is true.
* Arguably this should be true, but as things are implemented the conditions
* specified in the different attributes of @ConditionalOnBean are combined with
* logical OR (not AND) so if any of them match the condition is true.
*/
assertThat(this.context.containsBean("bar")).isFalse(); assertThat(this.context.containsBean("bar")).isFalse();
} }
@Test @Test
public void hierarchyConsidered() { public void hierarchyConsidered() {
this.context.register(FooConfiguration.class); load(FooConfiguration.class);
this.context.refresh();
AnnotationConfigApplicationContext childContext = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext childContext = new AnnotationConfigApplicationContext();
childContext.setParent(this.context); childContext.setParent(this.context);
childContext.register(HierarchyConsidered.class); childContext.register(HierarchyConsidered.class);
@ -104,8 +103,7 @@ public class ConditionalOnMissingBeanTests {
@Test @Test
public void hierarchyNotConsidered() { public void hierarchyNotConsidered() {
this.context.register(FooConfiguration.class); load(FooConfiguration.class);
this.context.refresh();
AnnotationConfigApplicationContext childContext = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext childContext = new AnnotationConfigApplicationContext();
childContext.setParent(this.context); childContext.setParent(this.context);
childContext.register(HierarchyNotConsidered.class); childContext.register(HierarchyNotConsidered.class);
@ -115,15 +113,13 @@ public class ConditionalOnMissingBeanTests {
@Test @Test
public void impliedOnBeanMethod() { public void impliedOnBeanMethod() {
this.context.register(ExampleBeanConfiguration.class, ImpliedOnBeanMethod.class); load(ExampleBeanConfiguration.class, ImpliedOnBeanMethod.class);
this.context.refresh();
assertThat(this.context.getBeansOfType(ExampleBean.class).size()).isEqualTo(1); assertThat(this.context.getBeansOfType(ExampleBean.class).size()).isEqualTo(1);
} }
@Test @Test
public void testAnnotationOnMissingBeanCondition() { public void testAnnotationOnMissingBeanCondition() {
this.context.register(FooConfiguration.class, OnAnnotationConfiguration.class); load(FooConfiguration.class, OnAnnotationConfiguration.class);
this.context.refresh();
assertThat(this.context.containsBean("bar")).isFalse(); assertThat(this.context.containsBean("bar")).isFalse();
assertThat(this.context.getBean("foo")).isEqualTo("foo"); assertThat(this.context.getBean("foo")).isEqualTo("foo");
} }
@ -131,10 +127,9 @@ public class ConditionalOnMissingBeanTests {
// Rigorous test for SPR-11069 // Rigorous test for SPR-11069
@Test @Test
public void testAnnotationOnMissingBeanConditionWithEagerFactoryBean() { public void testAnnotationOnMissingBeanConditionWithEagerFactoryBean() {
this.context.register(FooConfiguration.class, OnAnnotationConfiguration.class, load(FooConfiguration.class, OnAnnotationConfiguration.class,
FactoryBeanXmlConfiguration.class, FactoryBeanXmlConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.containsBean("bar")).isFalse(); assertThat(this.context.containsBean("bar")).isFalse();
assertThat(this.context.containsBean("example")).isTrue(); assertThat(this.context.containsBean("example")).isTrue();
assertThat(this.context.getBean("foo")).isEqualTo("foo"); assertThat(this.context.getBean("foo")).isEqualTo("foo");
@ -142,130 +137,109 @@ public class ConditionalOnMissingBeanTests {
@Test @Test
public void testOnMissingBeanConditionWithFactoryBean() { public void testOnMissingBeanConditionWithFactoryBean() {
this.context.register(FactoryBeanConfiguration.class, load(FactoryBeanConfiguration.class, ConditionalOnFactoryBean.class,
ConditionalOnFactoryBean.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(ExampleBean.class).toString()) assertThat(this.context.getBean(ExampleBean.class).toString())
.isEqualTo("fromFactory"); .isEqualTo("fromFactory");
} }
@Test @Test
public void testOnMissingBeanConditionWithComponentScannedFactoryBean() { public void testOnMissingBeanConditionWithComponentScannedFactoryBean() {
this.context.register(ComponentScannedFactoryBeanBeanMethodConfiguration.class, load(ComponentScannedFactoryBeanBeanMethodConfiguration.class,
ConditionalOnFactoryBean.class, ConditionalOnFactoryBean.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(ExampleBean.class).toString()) assertThat(this.context.getBean(ExampleBean.class).toString())
.isEqualTo("fromFactory"); .isEqualTo("fromFactory");
} }
@Test @Test
public void testOnMissingBeanConditionWithComponentScannedFactoryBeanWithBeanMethodArguments() { public void testOnMissingBeanConditionWithComponentScannedFactoryBeanWithBeanMethodArguments() {
this.context.register( load(ComponentScannedFactoryBeanBeanMethodWithArgumentsConfiguration.class,
ComponentScannedFactoryBeanBeanMethodWithArgumentsConfiguration.class,
ConditionalOnFactoryBean.class, ConditionalOnFactoryBean.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(ExampleBean.class).toString()) assertThat(this.context.getBean(ExampleBean.class).toString())
.isEqualTo("fromFactory"); .isEqualTo("fromFactory");
} }
@Test @Test
public void testOnMissingBeanConditionWithFactoryBeanWithBeanMethodArguments() { public void testOnMissingBeanConditionWithFactoryBeanWithBeanMethodArguments() {
this.context.register(FactoryBeanWithBeanMethodArgumentsConfiguration.class, load(new Class<?>[] { FactoryBeanWithBeanMethodArgumentsConfiguration.class,
ConditionalOnFactoryBean.class, ConditionalOnFactoryBean.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class }, "theValue:foo");
TestPropertyValues.of("theValue:foo").applyTo(this.context);
this.context.refresh();
assertThat(this.context.getBean(ExampleBean.class).toString()) assertThat(this.context.getBean(ExampleBean.class).toString())
.isEqualTo("fromFactory"); .isEqualTo("fromFactory");
} }
@Test @Test
public void testOnMissingBeanConditionWithConcreteFactoryBean() { public void testOnMissingBeanConditionWithConcreteFactoryBean() {
this.context.register(ConcreteFactoryBeanConfiguration.class, load(ConcreteFactoryBeanConfiguration.class, ConditionalOnFactoryBean.class,
ConditionalOnFactoryBean.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(ExampleBean.class).toString()) assertThat(this.context.getBean(ExampleBean.class).toString())
.isEqualTo("fromFactory"); .isEqualTo("fromFactory");
} }
@Test @Test
public void testOnMissingBeanConditionWithUnhelpfulFactoryBean() { public void testOnMissingBeanConditionWithUnhelpfulFactoryBean() {
this.context.register(UnhelpfulFactoryBeanConfiguration.class, load(UnhelpfulFactoryBeanConfiguration.class, ConditionalOnFactoryBean.class,
ConditionalOnFactoryBean.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
// We could not tell that the FactoryBean would ultimately create an ExampleBean // We could not tell that the FactoryBean would ultimately create an ExampleBean
assertThat(this.context.getBeansOfType(ExampleBean.class).values()).hasSize(2); assertThat(this.context.getBeansOfType(ExampleBean.class).values()).hasSize(2);
} }
@Test @Test
public void testOnMissingBeanConditionWithRegisteredFactoryBean() { public void testOnMissingBeanConditionWithRegisteredFactoryBean() {
this.context.register(RegisteredFactoryBeanConfiguration.class, load(RegisteredFactoryBeanConfiguration.class, ConditionalOnFactoryBean.class,
ConditionalOnFactoryBean.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(ExampleBean.class).toString()) assertThat(this.context.getBean(ExampleBean.class).toString())
.isEqualTo("fromFactory"); .isEqualTo("fromFactory");
} }
@Test @Test
public void testOnMissingBeanConditionWithNonspecificFactoryBeanWithClassAttribute() { public void testOnMissingBeanConditionWithNonspecificFactoryBeanWithClassAttribute() {
this.context.register(NonspecificFactoryBeanClassAttributeConfiguration.class, load(NonspecificFactoryBeanClassAttributeConfiguration.class,
ConditionalOnFactoryBean.class, ConditionalOnFactoryBean.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(ExampleBean.class).toString()) assertThat(this.context.getBean(ExampleBean.class).toString())
.isEqualTo("fromFactory"); .isEqualTo("fromFactory");
} }
@Test @Test
public void testOnMissingBeanConditionWithNonspecificFactoryBeanWithStringAttribute() { public void testOnMissingBeanConditionWithNonspecificFactoryBeanWithStringAttribute() {
this.context.register(NonspecificFactoryBeanStringAttributeConfiguration.class, load(NonspecificFactoryBeanStringAttributeConfiguration.class,
ConditionalOnFactoryBean.class, ConditionalOnFactoryBean.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(ExampleBean.class).toString()) assertThat(this.context.getBean(ExampleBean.class).toString())
.isEqualTo("fromFactory"); .isEqualTo("fromFactory");
} }
@Test @Test
public void testOnMissingBeanConditionWithFactoryBeanInXml() { public void testOnMissingBeanConditionWithFactoryBeanInXml() {
this.context.register(FactoryBeanXmlConfiguration.class, load(FactoryBeanXmlConfiguration.class, ConditionalOnFactoryBean.class,
ConditionalOnFactoryBean.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(ExampleBean.class).toString()) assertThat(this.context.getBean(ExampleBean.class).toString())
.isEqualTo("fromFactory"); .isEqualTo("fromFactory");
} }
@Test @Test
public void testOnMissingBeanConditionWithIgnoredSubclass() { public void testOnMissingBeanConditionWithIgnoredSubclass() {
this.context.register(CustomExampleBeanConfiguration.class, load(CustomExampleBeanConfiguration.class, ConditionalOnIgnoredSubclass.class,
ConditionalOnIgnoredSubclass.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeansOfType(ExampleBean.class)).hasSize(2); assertThat(this.context.getBeansOfType(ExampleBean.class)).hasSize(2);
assertThat(this.context.getBeansOfType(CustomExampleBean.class)).hasSize(1); assertThat(this.context.getBeansOfType(CustomExampleBean.class)).hasSize(1);
} }
@Test @Test
public void testOnMissingBeanConditionWithIgnoredSubclassByName() { public void testOnMissingBeanConditionWithIgnoredSubclassByName() {
this.context.register(CustomExampleBeanConfiguration.class, load(CustomExampleBeanConfiguration.class,
ConditionalOnIgnoredSubclassByName.class, ConditionalOnIgnoredSubclassByName.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeansOfType(ExampleBean.class)).hasSize(2); assertThat(this.context.getBeansOfType(ExampleBean.class)).hasSize(2);
assertThat(this.context.getBeansOfType(CustomExampleBean.class)).hasSize(1); assertThat(this.context.getBeansOfType(CustomExampleBean.class)).hasSize(1);
} }
@Test @Test
public void grandparentIsConsideredWhenUsingAncestorsStrategy() { public void grandparentIsConsideredWhenUsingAncestorsStrategy() {
this.context.register(ExampleBeanConfiguration.class); load(ExampleBeanConfiguration.class);
this.context.refresh();
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
parent.setParent(this.context); parent.setParent(this.context);
parent.refresh(); parent.refresh();
@ -292,13 +266,22 @@ public class ConditionalOnMissingBeanTests {
@Test @Test
public void beanProducedByFactoryBeanIsConsideredWhenMatchingOnAnnotation() { public void beanProducedByFactoryBeanIsConsideredWhenMatchingOnAnnotation() {
this.context.register(ConcreteFactoryBeanConfiguration.class, load(ConcreteFactoryBeanConfiguration.class,
OnAnnotationWithFactoryBeanConfiguration.class); OnAnnotationWithFactoryBeanConfiguration.class);
this.context.refresh();
assertThat(this.context.containsBean("bar")).isFalse(); assertThat(this.context.containsBean("bar")).isFalse();
assertThat(this.context.getBeansOfType(ExampleBean.class)).hasSize(1); assertThat(this.context.getBeansOfType(ExampleBean.class)).hasSize(1);
} }
private void load(Class<?>... configs) {
load(configs, new String[] {});
}
private void load(Class<?>[] configs, String... environmentValues) {
this.context.register(configs);
TestPropertyValues.of(environmentValues).applyTo(this.context);
this.context.refresh();
}
@Configuration @Configuration
protected static class OnBeanInAncestorsConfiguration { protected static class OnBeanInAncestorsConfiguration {

Loading…
Cancel
Save