spring-projects / spring-projects/spring-framework

Support dynamic bean lookup a la CDI's Instance type [SPR-13132]

Open
#17,723 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

in: core type: enhancement
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

Thomas Darimont opened SPR-13132 and commented

It would be helpful to be able to dynamically query an application context for beans that match a specific type and or are annotated with some specific qualifier annotations.

CDI offers support for this via javax.enterprise.inject.Instance. Currently the application context only offers to query beans by type. It would be great if we could support dynamic bean lookups via Instance<T> backed by the BeanRegistry.

I did a quick PoC here.
The basic idea is to provide a custom AutowireCandidateResolver that detects Instance<T> autowire targets, identifies the actual target bean type and provides some kind of Instance adapter which performs the actual bean lookup.


Reference URL: https://github.com/thomasdarimont/spring-boot-cdi-instance-example/blob/master/src/test/java/demo/SimpleCdiInstanceAdapterTests.java#L73

Issue Links:

  • #16761 Add generics / parameterized type support to ListableBeanFactory getBeanNamesForType/getBeansOfType methods
  • #18515 An ObjectFactory variant with lenient not-unique handling
  • #13532 Convenient programmatic bean retrieval with qualifiers
  • #18529 ObjectFactory lacks method for getting bean with specified constructor arguments
  • #17522 Ability to query an ApplicationContext for annotations on a bean

3 votes, 7 watchers

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the CustomAutowireCandidateResolver.java proof of concept and the SimpleCdiInstanceAdapterTests.java reference test. Review the BeanRegistry and AutowireCandidateResolver entry points described in the issue, along with the linked related issues. Done means a supported Instance-style lookup can query matching and qualified beans with tests covering the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.