I'm working in Spring version greater than 4. I've got beans with the same super-class configured using @Configuration
@Configuration
public Class ConfigClass{
@Bean
public Apple apple(){stuff to return apple bean}
@Bean
public Orange orange(){stuff to return orange bean}
}
I've got an bean which maybe composed of either one of those two beans
@Component
public Class FruitEater<ReturnType extends Fruit>{
@Bean
ReturnType fruit;
}
And I get this beautiful ambiguity error message:
No qualifying bean of type [fruit] is defined: expected single matching bean but found 2: appleInjection,orangeInjection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: fruiteater.bean; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [fruit] is defined: expected single matching bean but found 2: apple,orange
My thinking is that since the ReturnType should be resolved at compile-time, spring should be able to Autowire based on the Generic Type. I've heard of the Spring's ResolvableType but I'm not sure how I can leverage that as I'm still pretty new to Spring. Is there a way to resolve this and keep my FruitEater a generic FruitEater?
Thanks, in advance.