I've learned that in spring, i can autowire/inject into Map<String, SomeBeanInterface>
by configured name like below:
public interface DummyInterface{
}
@Component("impl1")
public class Impl1 implement DummyInterface{
}
@Component("impl2")
public class Impl2 implement DummyInterface{
}
public class SomeUsage{
@Autowired
private Map<String, DummyInterface> mapping;
// ...
}
and retrieve the Component
by string as key like:
SomeUsage use = new SomeUsage();
DummyInterface dummy = use.getMapping().get("impl1");
// do sth...
However, if the key of bean mapping is not the type of String
, but the type of user defined Enum
, how should i inject the beans into the enumMap?
I've read some post and learned that it can be configured by xml file. But it seems to be that the xml configuration is tightly coupled with the <Enum, Bean>
pair, which means that each time if i add a new <Enum, Bean>
pair, i have to synchronize the configuration file, it seems that there's no difference comparing to my current solution, that is, still using the <String, Bean>
collection and maintain the <Enum, String>
mapping in java code by my own. Are there any better solution to handle this? Or do i miss something?