Skip to content
Merged
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 @@ -27,6 +27,7 @@
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -143,10 +144,12 @@ protected String generateDisplayName(String name) {

public <T> T getInstance(String name, Class<T> type) {
AnnotationConfigApplicationContext context = getContext(name);
if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
type).length > 0) {
try {
return context.getBean(type);
}
catch (NoSuchBeanDefinitionException e) {
// ignore
}
return null;
}

Expand Down Expand Up @@ -181,11 +184,8 @@ public <T> T getInstance(String name, ResolvableType type) {

public <T> Map<String, T> getInstances(String name, Class<T> type) {
AnnotationConfigApplicationContext context = getContext(name);
if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
type).length > 0) {
return BeanFactoryUtils.beansOfTypeIncludingAncestors(context, type);
}
return null;

return BeanFactoryUtils.beansOfTypeIncludingAncestors(context, type);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ public void testChildContexts() {
Bar foobar = factory.getInstance("foo", Bar.class);
then(foobar).as("bar was not null").isNull();

Baz fooBaz = factory.getInstance("foo", Baz.class);
then(fooBaz).as("fooBaz was null").isNotNull();

Object fooContainerFoo = factory.getInstance("foo", Container.class, Foo.class);
then(fooContainerFoo).as("fooContainerFoo was null").isNotNull();

Object fooContainerBar = factory.getInstance("foo", Container.class, Bar.class);
then(fooContainerBar).as("fooContainerBar was not null").isNull();

Object barContainerBar = factory.getInstance("bar", Container.class, Bar.class);
then(barContainerBar).as("barContainerBar was null").isNotNull();

Map<String, Baz> fooBazes = factory.getInstances("foo", Baz.class);
then(fooBazes).as("fooBazes was null").isNotNull();
then(fooBazes.size()).as("fooBazes size was wrong").isEqualTo(1);
Expand Down Expand Up @@ -146,6 +158,11 @@ Foo foo() {
return new Foo();
}

@Bean
Container<Foo> fooContainer() {
return new Container<>(new Foo());
}

}

static class Foo {
Expand All @@ -164,10 +181,29 @@ Baz baz2() {
return new Baz();
}

@Bean
Container<Bar> barContainer() {
return new Container<>(new Bar());
}

}

static class Bar {

}

static class Container<T> {

private final T item;

Container(T item) {
this.item = item;
}

public T getItem() {
return this.item;
}

}

}