Igor Dmitriev opened SPR-14555 and commented
Hi there, I am wondering about a such kind of functionality: I would like to have an opportunity to autowire map with key - enum, values - beans. I know it's possible to do it via xml configuration (for instance using MapFactoryBean). And by default we can autowire map with key - String (bean class name) and value - Bean. But it will be cool to do it via annotation configuration, like this:
@Component
public class WoodlandParkZoo implements Zoo {
@AutowiredMap // or @Autowired
private Map<LifeArea, Animal> animals;
@Override
public String say(LifeArea lifeArea) {
return animals.get(lifeArea).say();
}
}
@Component
@MapKeyEnumerated(key = LifeArea.DESERT)
public class Lion implements Animal {
@Override
public String say() {
return "r-r-r!";
}
}
but now I just can do like this, as I know:
private final Map<LifeArea, Animal> animals = new HashMap<>();
@Autowired
public WoodlandParkZoo(@Qualifier("lion") Animal lion, @Qualifier("seal") Animal seal) {
animals.put(LifeArea.DESERT, lion);
animals.put(LifeArea.SEA, seal);
}
@Override
public String say(LifeArea lifeArea) {
String say = animals.get(lifeArea).say();
if (say == null) {
throw new IllegalArgumentException("There is no animal in the zoo");
}
return say;
}
But I think it will be easier and more comfortable to have the previous feature, using @AutowiredMap
annotation.
I have a solution, workaround here - https://github.com/Playmate/SpringAutowiredMap.git.
Affects: 4.3.2
Referenced from: pull request https://github.com/spring-projects/spring-framework/pull/1496
1 votes, 3 watchers
Comment From: snicoll
Thanks for the suggestion and for the PR but this request has not gained much traction from the community. Considering you can easily wrap your own service that does this and I feel it's a little bit too specific for the core framework, I am going to close this now.