Support for fine-grained by-type references in the bean definition model #23032
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
type: enhancement
A general enhancement
Milestone
After reviewing the core Spring Framework
BeanReference
implementations, I got to thinking if there was aRuntimeBeanTypeReference
like implementation, or something similar?I am aware of the RuntimeBeanReference and RuntimeBeanNameReference implementations, however, both of these only refer to a “single” bean in the Spring context.
This is useful in situations like the following.
Given:
Then in a framework (e.g. Spring Data or Spring Session, etc) provided
ImportBeanDefinitionRegistrar
implementation, I can:class CustomImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
However, what if I want to register a
BeanDefinition
who’s class type has a property that requests all beans of a particular type declared and registered in the Spring container?Reimagining our class definitions from above:
Then, with a
RuntimeBeanTypeReference
, I could:I know we also have things like
Managed[List|Map|Set]
which can take 1 or moreBeanReference
objects, but again, I am constrained by having to know all the bean names possibly registered in the container upfront.Of course, I could iterate the
BeanFactory
,beanDefinitionNames
and inspect eachBeanDefinition
to determine if the class of the bean definition matches my desired type (e.g.B
).But, this does not work so well with
@Configuration
classes as it turns out.It seems that when looking at the
beanDefinitionMap
the@Configuration
classes have not be resolved with any@Bean
definitions they contain, particularly when inspecting theBeanFactory
(e.g.getBeansOfType(..)
) inside aImportBeanDefinitionRegistrar
.Therefore, by iterating (prematurely), I would not know if those
@Configuration
classes (or really, any@Component
class), which is allowed to contain other@Bean
definitions, would potentially match beans of my desired type (i.e.B
).Hence the thinking behind a
RuntimeBeanTypeReference
implementation.I suppose
class A
could be defined as:But, if annotation config (autowiring) were not enabled (which even though maybe uncommon), would not work.
Hence my thinking behind a
RuntimeBeanTypeReference
implementation.Thoughts?
The text was updated successfully, but these errors were encountered: