Affects: 5.1.x


I'd like to define multiple implementations of an interface and expose them as singleton beans and I want to define boolean expressions on the implementing classes as spEL. When the client code calls the methods of the interface the expressions should be evaluated to select which of the implementing beans is to be called.

Something like this:

@ProxiedService(expression = "#{switch.switchValue == 'B2B'}")
public static class B2BServiceImpl implements ProxiedServiceInterface {
  @Override
  public String doCoolStuff() {
    return "Well you know ... ";
  }
}

I'd started an stackoverflow question on this but didn't really get answers that helped me.

So I tried to figure out myself how this could be implemented and ended up with this small project which basically uses a BeanFactoryPostProcessor registering a new AutowireCandiateResolver that inherits from the ContextAnnotationAutowireCandidateResolver (which is used for implementing the @Lazy annotation which basically gave me the idea for this)

I would have implemented this directly into spring but wasn't sure if i found the right/all places to adjust or whether it's really a good idea to do this or if it would be better to just implement this as an extension and keep it outside of spring itself.

Of course an actual implemetation should make sure (if possible) that the implementing classes should only have one method to call or don't have a state. Because in that case calls of different methods on the interface may call the methods within to different objects.

Comment From: snicoll

Thanks for the suggestion. The use of SpEL is quite specific and I wonder if it could be greatly simplified by having a facade that provides the implementation to use, even caching it on startup perhaps? The above looks really specific and not a good candidate for the core framework.