| 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 Open )
- an instance of TheExpectedType (requires
HSEARCH-3174 Open )
One solution would be to allow this:
private static final ConfigurationProperty<Optional<BeanReference>> MY_PROPERTY = ConfigurationProperty.forKey( "myKey )
.asBeanReference()
.build();
...
public void someMethod(BeanProvider beanProvider, ConfigurationPropertySource source) {
MyBean myBean = MY_KEY.get(source).orElseThrow( () -> new IllegalStateException() )
.get( beanProvider, MyBean.class );
}
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:
private static final ConfigurationProperty<Optional<BeanReference<MyBean>>> MY_PROPERTY = ConfigurationProperty.forKey( "myKey )
.asBeanReference( MyBean.class )
.build();
...
public void someMethod(BeanProvider beanProvider, ConfigurationPropertySource source) {
MyBean myBean = MY_KEY.get(source).orElseThrow( () -> new IllegalStateException() )
.get( beanProvider );
}
|