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?
You must add
@Qualifier("impl1")
or@Qualifier("impl2")
with@Autowired
to inject your implementation class since theDummyInterface
has multiple implementations.Further info on
EnumMap
, you can refer hereYou always have to define mapping between Enum and Spring Bean but you can enforce that components have to declare to which enumeration they are mapped to. You can acheive that creating interface like:
Then every component that you want to be mapped has to implement it.
Then you can map each of this components by it's enumaration.
And inject wherever you want.
In config class you can also validate that every component declare uniqe, non-null enum value.