We're updating the issue view to help you get more done. 

Search 6 groundwork - Make bean references easier to use in configuration properties

Description

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:

1 2 3 4 5 6 7 8 9 10 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:

1 2 3 4 5 6 7 8 9 10 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 ); }

Environment

None

Status

Assignee

Yoann Rodière

Reporter

Yoann Rodière

Labels

None

Suitable for new contributors

Yes, likely

Feedback Requested

None

Components

Fix versions

Priority

Major