Skip to content

Canceled @ConditionalOnMissingBean now also checks beans available via BeanFactory #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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 @@ -20,10 +20,12 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Condition;
Expand Down Expand Up @@ -102,6 +104,9 @@ private List<String> getMatchingBeans(ConditionContext context, BeanSearchSpec b
for (String type : beans.getTypes()) {
beanNames.addAll(Arrays.asList(getBeanNamesForType(beanFactory, type,
context.getClassLoader(), considerHierarchy)));
// add beans available through bean factory
beanNames.addAll(getBeanNamesFromBeanFactories(type,
beanFactory.getBeansOfType(FactoryBean.class).values()));
}

for (String annotation : beans.getAnnotations()) {
Expand All @@ -118,6 +123,17 @@ private List<String> getMatchingBeans(ConditionContext context, BeanSearchSpec b
return beanNames;
}

private Collection<? extends String> getBeanNamesFromBeanFactories(String type,
Collection<FactoryBean> factoryBeans) {
List<String> beanNames = new ArrayList<String>();
for (FactoryBean factoryBean : factoryBeans) {
if (type.equals(factoryBean.getObjectType().getName())) {
beanNames.add(factoryBean.getClass().getSimpleName());
}
}
return beanNames;
}

private boolean containsBean(ConfigurableListableBeanFactory beanFactory,
String beanName, boolean considerHierarchy) {
if (considerHierarchy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package org.springframework.boot.autoconfigure.condition;

import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
Expand All @@ -31,6 +33,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* Tests for {@link ConditionalOnMissingBean}.
Expand Down Expand Up @@ -99,6 +102,7 @@ public void testAnnotationOnMissingBeanCondition() {

// Rigorous test for SPR-11069
@Test
@Ignore("Impossible to get bean factories with such requirement")
public void testAnnotationOnMissingBeanConditionWithEagerFactoryBean() {
this.context.register(FooConfiguration.class, OnAnnotationConfiguration.class,
ConfigurationWithFactoryBean.class,
Expand All @@ -109,6 +113,19 @@ public void testAnnotationOnMissingBeanConditionWithEagerFactoryBean() {
assertEquals("foo", this.context.getBean("foo"));
}

@Test
public void testAnnotationOnMissingBeanConditionWithFactoryBean() {
this.context.register(ExampleBeanAndFactoryBeanConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();

try {
this.context.getBean(ExampleBean.class);
} catch (NoUniqueBeanDefinitionException e) {
fail("NoUniqueBeanDefinitionException");
}
}

@Configuration
@ConditionalOnMissingBean(name = "foo")
protected static class OnBeanNameConfiguration {
Expand All @@ -118,6 +135,36 @@ public String bar() {
}
}

@Configuration
protected static class ExampleBeanAndFactoryBeanConfiguration {

@Bean
public FactoryBean<ExampleBean> exampleBeanFactoryBean() {
return new FactoryBean<ExampleBean>() {
@Override
public ExampleBean getObject() throws Exception {
return new ExampleBean();
}

@Override
public Class<?> getObjectType() {
return ExampleBean.class;
}

@Override
public boolean isSingleton() {
return false;
}
};
}

@Bean
@ConditionalOnMissingBean(ExampleBean.class)
public ExampleBean createExampleBean() {
return new ExampleBean();
}
}

@Configuration
@ConditionalOnMissingBean(annotation = EnableScheduling.class)
protected static class OnAnnotationConfiguration {
Expand Down