Spring Autowire Annotation with Several Interface Implementations

java, spring

Solution

Assuming you already have hundreds of interfaces and implementations (as you said in a comment), and you do not want to refactor all the code... then is a tricky problem... and this is a tricky solution:

You could create a custom `BeanDefinitionRegistryPostProcessor` and implement either the method `postProcessBeanDefinitionRegistry` or `postProcessBeanFactory`.

This way you have access to all bean definitions before they are instantiated and injected. Do your logic to find which is the preferred implementation for each one of your interfaces, and then, set that one as primary.

@Component
public class CustomBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(
            BeanDefinitionRegistry registry) throws BeansException {

          // this method can be used to set a primary bean, although 
          // beans defined in a @Configuration class will not be avalable here.

    }

    @Override
    public void postProcessBeanFactory(
            ConfigurableListableBeanFactory beanFactory) throws BeansException {     

        // here, all beans are available including those defined by @configuration, @component, xml, etc.

        // do some magic to somehow find which is the preferred bean name for each interface 
        // you have access to all bean-definition names with: beanFactory.getBeanDefinitionNames()
        String beanName = "aImpl2"; // let's say is this one

        // get the definition for that bean and set it as primary
        beanFactory.getBeanDefinition(beanName).setPrimary(true)

    }



}

The hard part is to find the bean name, it depends of the specifics of your application. I guess that having a consistent naming convention will help.

Update:

It seems that both methods in the interface `BeanDefinitionRegistryPostProcessor` can be used for this purpose. Having in mind that in the `postProcessBeanDefinitionRegistry` phase, beans configured through @configuration classes are not yet available, as noted in the comments below.

On the other hand they are indeed available in `postProcessBeanFactory`.

Problem

Suppose you have one interface ``` public interface A { public void doSomething(); } ``` and two implementation classes ``` @Component(value="aImpl1") public class AImpl1 implements A { } @Component(value="aImpl2") public class AImpl2 implements A{ } ``` And finally a class that will use an "A" implementation: ``` @Component public class MyClass { @Autowire A a; } ``` Now if I want to inject AImpl1 I add the @Qualifier("aImpl1") while if I want to inject AImpl2 I add @Qualifier("aImpl2") The question is: Is it possible to instruct spring somehow to look up all implementations of "A" in this case AImpl1 and AImpl2 and use some application specific conventions to choose the most appropriate implementation? for example in this case my convention could be use the implementation with the greatest suffix (i.e. AImpl2)? EDIT: the class MyClass should not be aware at all about the implementation lookup logic, it should just find its property "a" set with an object of AImpl2.

Original source