-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Support dynamic bean lookup a la CDI's Instance type [SPR-13132] #17723
Comments
Juergen Hoeller commented On a related note, as of 4.2, we support bean lookup by And with respect to CDI's Juergen |
Juergen Hoeller commented On a more recent note, 4.3 ships I guess this primarily leaves narrowing to specific beans by annotation or by subtype, as well as the lookup of all matching beans for the declared type? Do you have any concrete use cases in mind? Juergen |
Thomas Darimont commented Yes, it mostly boils down to narrowing the beans by annotation / subtype - one use case is that I want to dynamically select a "strategy" among multiple candidates, which are represented as beans. One could of course do
but then I'd also need boilerplate code to select the default if it's just one, or match those candidates based on a predicate (e.g. if an annotation is present and has a certain configuration or not) - |
Jan Ortmann commented Will this feature be available in 5.1? |
Juergen Hoeller commented To be clear, this ticket is not about CDI API compatibility, it is about CDI-style retrieval variants within Spring's programming model. So while this might make CDI/Spring bridging more straightforward in some scenarios, I'm afraid it won't cover your use case directly. |
Jan Ortmann commented Thanks for the fast reply. |
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 viaInstance<T>
backed by theBeanRegistry
.I did a quick PoC here.
The basic idea is to provide a custom
AutowireCandidateResolver
that detectsInstance<T>
autowire targets, identifies the actual target bean type and provides some kind ofInstance
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:
3 votes, 7 watchers
The text was updated successfully, but these errors were encountered: