Add generic bean support using @ConditionalOnBean and @ConditionalOnMissingBean annotations as described in #28845. Added two method to annotations: typeArguments and typeArgumentNames for classes and class names respectively.
Usage example:
// loaded via spring factories
public class SomeAutoConfiguration {
@ConditionalOnMissingBean(typeArguments = { String.class, Integer.class })
@Bean
public Converter<String, Integer> converter() {
// ...
}
}
Comment From: philwebb
Thanks very much for the pull-request @VladislavSevruk. We discussed this today on our team call and we'd like to try and remove the typeArguments and typeArgumentNames attributes and instead detect the generics from the return type.
If you're interested in updating your pull-request in that direction, that would be great but please be aware that we're currently focusing on some Spring Boot 3.0 issues and this one is in the "general backlog" so we might not be able to review and merge things all that quickly.
No worries if you don't want to spend any additional time on this. We'll leave this issue open and mark it for merge-with-amendments.
Comment From: VladislavSevruk
Thank you for feedback @philwebb. I've updated code to auto-detect type arguments for generics from method return type.
But I also left typeArguments and typeArgumentNames attributes at annotations for cases when user uses type or value attributes.
Please let me know if I need to remove annotation attributes anyway.
Comment From: snicoll
@VladislavSevruk yes please. We really want to support that mode only for the return type.
Comment From: VladislavSevruk
@philwebb @snicoll the typeArguments and typeArgumentNames attributes were removed
Comment From: snicoll
Thanks a lot for the follow-up @VladislavSevruk. When merging this, we probably want to restore the existing tests using ExampleBean and create a separate test class with a generic as moving everything to generic doesn't seem right IMO.
Comment From: VladislavSevruk
@snicoll tests with ExampleBean are reverted to initial state, tests for generic return type are extracted to separate classes
Comment From: mitasov-ra
Any updates on when this PR will be merged?
Will it affect older versions of Spring Boot (pre 3.x) or is it just for 3.x?
Comment From: wilkinsona
@mitasov-ra The issue remains in the general backlog so things haven't changed since this comment. We consider the change to be an enhancement so it will only be made in a new minor (or major) release and won't affect older versions of Boot.
Comment From: Bennett-Lynch
Is this possible at all today? E.g., taking the example:
@ConditionalOnMissingBean(typeArguments = { String.class, Integer.class })
@Bean
public Converter<String, Integer> converter() {
// ...
}
Is it possible to achieve this with the current parameterizedContainer behavior? E.g.,
@ConditionalOnMissingBean(value = { String.class, Integer.class }, parameterizedContainer = { Converter.class })
@Bean
public Converter<String, Integer> converter() {
// ...
}
Comment From: sheiy
How is this going?
Comment From: wilkinsona
@sheiy No change since the last time someone asked. Any updates will appear here when we have them.