The idea would be to allow to define a configuration property of type "BeanReference", so that we can get rid of some of the complexity we currently face each time a bean can be passed to a configuration property. Ideally, the property should accept these values:
a String representing the bean name
a Class<? extends TheExpectedType> (requires HSEARCH-3174)
an instance of TheExpectedType (requires HSEARCH-3174)
One solution would be to allow this:
This would require to:
Move the reponsibility of passing the beanreference data (name, class, ...) to the beanreference itself: instead of writing beanProvider.get(beanRef.getName(), beanRef.getType()), we would write beanRef.get(beanProvider) and the beanreference would pass whatever information is necessary. That encapsulation of the bean retrieval would allow to seamlessly introduce bean references that already contain the bean, so that we can more easily support the case where the bean was provided as a property value.
As a consequence, introduce one factory method per "reference type", which could return different implementations: BeanReference.of(Class<?>), BeanReference.of(String), BeanReference.of(Class<?>, String), BeanReference.ofInstance(Object)
Add a method PropertyKeyContext.asBeanReference() to the property key definition DSL, which would define a converter that calls the appropriate BeanReference.of method depending on the property value type.
As a second step, we could try to introduce a generic parameter in BeanReference, so that we can write this kind of code: