Skip to content

Add convenient method to get generic beans by type reference #34669

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 1 commit 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
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 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.
Expand All @@ -19,6 +19,7 @@
import org.jspecify.annotations.Nullable;

import org.springframework.beans.BeansException;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.ResolvableType;

/**
Expand Down Expand Up @@ -99,6 +100,7 @@
* @author Rod Johnson
* @author Juergen Hoeller
* @author Chris Beams
* @author Yanming Zhou
* @since 13 April 2001
* @see BeanNameAware#setBeanName
* @see BeanClassLoaderAware#setBeanClassLoader
Expand Down Expand Up @@ -201,6 +203,25 @@ public interface BeanFactory {
*/
<T> T getBean(Class<T> requiredType) throws BeansException;

/**
* Return the bean instance that uniquely matches the given object type, if any.
* <p>This method goes into {@link ListableBeanFactory} by-type lookup territory
* but may also be translated into a conventional by-name lookup based on the name
* of the given type. For more extensive retrieval operations across sets of beans,
* use {@link ListableBeanFactory} and/or {@link BeanFactoryUtils}.
* @param typeReference the reference to obtain type the bean must match
* @return an instance of the single bean matching the required type
* @throws NoSuchBeanDefinitionException if no bean of the given type was found
* @throws NoUniqueBeanDefinitionException if more than one bean of the given type was found
* @throws BeansException if the bean could not be created
* @since 7.0
* @see #getBean(Class)
*/
default <T> T getBean(ParameterizedTypeReference<T> typeReference) throws BeansException {
ObjectProvider<T> provider = getBeanProvider(ResolvableType.forType(typeReference));
return provider.getObject();
}

/**
* Return an instance, which may be shared or independent, of the specified bean.
* <p>Allows for specifying explicit constructor arguments / factory method arguments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.MethodParameter;
import org.springframework.core.Ordered;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.annotation.Order;
Expand Down Expand Up @@ -1651,6 +1652,22 @@ void getBeanByTypeWithAmbiguity() {
lbf.getBean(TestBean.class));
}

@Test
void getBeanByTypeReference() {
RootBeanDefinition bd1 = new RootBeanDefinition(StringTemplate.class);
RootBeanDefinition bd2 = new RootBeanDefinition(NumberTemplate.class);
lbf.registerBeanDefinition("bd1", bd1);
lbf.registerBeanDefinition("bd2", bd2);

Template<String> stringTemplate = lbf.getBean(new ParameterizedTypeReference<>() {
});
Template<Number> numberTemplate = lbf.getBean(new ParameterizedTypeReference<>() {
});

assertThat(stringTemplate).isInstanceOf(StringTemplate.class);
assertThat(numberTemplate).isInstanceOf(NumberTemplate.class);
}

@Test
void getBeanByTypeWithPrimary() {
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
Expand Down Expand Up @@ -3814,4 +3831,16 @@ public Class<?> getObjectType() {
}
}

private static class Template<T> {

}

private static class StringTemplate extends Template<String> {

}

private static class NumberTemplate extends Template<Number> {

}

}