Fixed
Details
Assignee
Yoann RodièreYoann RodièreReporter
Yoann RodièreYoann RodièreComponents
Sprint
NoneFix versions
Priority
Major
Details
Details
Assignee
Yoann Rodière
Yoann RodièreReporter
Yoann Rodière
Yoann RodièreComponents
Sprint
None
Fix versions
Priority
Created October 3, 2018 at 7:33 AM
Updated January 31, 2019 at 3:08 PM
Resolved December 4, 2018 at 3:55 PM
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 namea
Class<? extends TheExpectedType>
(requires HSEARCH-3174)an instance of
TheExpectedType
(requires HSEARCH-3174)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 writebeanRef.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 appropriateBeanReference.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 ); }