Skip to content

Add typesafe method to get generic bean by name with type reference#35808

Merged
jhoeller merged 1 commit intospring-projects:mainfrom
quaff:patch-112
Mar 24, 2026
Merged

Add typesafe method to get generic bean by name with type reference#35808
jhoeller merged 1 commit intospring-projects:mainfrom
quaff:patch-112

Conversation

@quaff
Copy link
Copy Markdown
Contributor

@quaff quaff commented Nov 13, 2025

Fix GH-34687

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 13, 2025
@jhoeller jhoeller self-assigned this Nov 18, 2025
@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 18, 2025
@jhoeller jhoeller added this to the 7.1.x milestone Nov 18, 2025
@jhoeller jhoeller modified the milestones: 7.1.x, 7.1.0-M1 Mar 13, 2026
@jhoeller
Copy link
Copy Markdown
Contributor

Hey @quaff - revisiting this PR for 7.1, could you please:

  • Remove the accidental changes to MultiReleaseJarPluginTests.
  • Move the getBean(String, ParameterizedTypeReference) implementation to AbstractBeanFactory, declaring the method in the BeanFactory interface as non-default (since the entire interface does not use default methods so far).
  • In BeanNotOfRequiredTypeException, change the existing Class<?> requiredType field to Type genericRequiredType (instead of adding a new field). In the existing getRequiredType() method, you could adapt the genericRequiredType field accordingly, for example: return (this.genericRequiredType instanceof Class<?> clazz ? clazz : ResolvableType.forType(this.genericRequiredType).toClass());
  • Finally, update the @since tags to 7.1 please. I'm happy to merge it for 7.1 M1 then.

@quaff quaff force-pushed the patch-112 branch 2 times, most recently from 1b7daa0 to 37a334e Compare March 23, 2026 01:14
@quaff
Copy link
Copy Markdown
Contributor Author

quaff commented Mar 23, 2026

@jhoeller Updated as you suggested.

Move the getBean(String, ParameterizedTypeReference) implementation to AbstractBeanFactory, declaring the method in the BeanFactory interface as non-default (since the entire interface does not use default methods so far).

This result in code duplication since not every implementation extends AbstractBeanFactory.

@jhoeller
Copy link
Copy Markdown
Contributor

jhoeller commented Mar 23, 2026

Point taken - that said, a little bit of duplication is expected and acceptable, in particular in the rather special StaticListableBeanFactory and SimpleJndiBeanFactory cases.

However, in AbstractApplicationContext and StubWebApplicationContext, the goal is rather to pass the call through to the underlying BeanFactory along the following lines, similar to the surrounding methods there - which avoids two current cases of code duplication in favor of a delegation arrangement that preserves potentially customized behavior in the underlying BeanFactory:

	public <T> T getBean(String name, ParameterizedTypeReference<T> typeReference) throws BeansException {
		assertBeanFactoryActive();
		return getBeanFactory().getBean(name, typeReference);
	}

Feel free to tweak that in the PR, or otherwise I can also do it myself when merging.

@quaff
Copy link
Copy Markdown
Contributor Author

quaff commented Mar 24, 2026

Updated.

@jhoeller jhoeller merged commit 0eba6f0 into spring-projects:main Mar 24, 2026
2 checks passed
@jhoeller
Copy link
Copy Markdown
Contributor

Perfect - thanks for your timely efforts there, @quaff!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add typesafe method to get generic bean by name with ParameterizedTypeReference

3 participants