Given spring java-config with method that returns list of beans:
@Configuration
public class Config {
@Bean(name = "resultConsumers")
public List<Consumer> getConsumers() {
return Arrays.asList(...);
}
}
How can I inject it in another bean?
class Bean {
@Inject
what?
}
P.S. It is not list with different implementations of consumers, they are all instances of the same class.
When you annotate a
Collectiontype with@Autowired, Spring doesn't look for a corresponding bean of that type. Instead it looks for the component type that theCollectionis meant to store.Instead, use
@Resourcewith the bean name.