Skip to content

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

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

Open
quaff opened this issue Mar 31, 2025 · 1 comment
Open

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

quaff opened this issue Mar 31, 2025 · 1 comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: waiting-for-triage An issue we've not yet triaged or decided on

Comments

@quaff
Copy link
Contributor

quaff commented Mar 31, 2025

Related to GH-34669, but it requires the bean name matched first, and throw BeanNotOfRequiredTypeException if the bean type doesn't match required type from type reference.

Take codes in Spring Batch for example:

		@SuppressWarnings("unchecked")
		ItemReader<CustomerCredit> outputReader = (ItemReader<CustomerCredit>) applicationContext
			.getBean("outputTestReader");

I'd like it could be rewrite to:

		ItemReader<CustomerCredit> outputReader = applicationContext.getBean("outputTestReader", new ParameterizedTypeReference<>() {});
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 31, 2025
@quaff
Copy link
Contributor Author

quaff commented Mar 31, 2025

Here is prototype: https://github.com/spring-projects/spring-framework/compare/main...quaff:spring-framework:patch-112?expand=1

I'd like to create PR if the proposal is accepted.

@quaff quaff changed the title Provider a method similar to getBean(String, Class<T>) to get generic beans Add typesafe method to get generic bean by name with type reference Apr 1, 2025
@jhoeller jhoeller added the in: core Issues in core modules (aop, beans, core, context, expression) label Apr 1, 2025
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) status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

No branches or pull requests

3 participants