问题
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?
回答1:
You 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:
public interface EnumMappedBean {
SomeEnum getSomeEnum();
}
Then every component that you want to be mapped has to implement it.
@Component
public class Bean1 implements EnumMappedBean {
@Override
public SomeEnum getSomeEnum() {
return SomeEnum.ENUM1;
}
}
@Component
public class Bean2 implements EnumMappedBean {
@Override
public SomeEnum getSomeEnum() {
return SomeEnum.ENUM2;
}
}
Then you can map each of this components by it's enumaration.
@Configuration
public class AppConfig {
@Bean
public Map<SomeEnum, EnumMappedBean> getBeansMappedByEnum(@NonNull Collection<EnumMappedBean> enumBeans) {
return enumBeans.stream()
.collect(toMap(EnumMappedBean::getSomeEnum, Function.identity()));
}
}
And inject wherever you want.
@Service
public class SomeOtherBean {
private Map<SomeEnum, EnumMappedBean> beansMappedByEnum;
@Autowired
public SomeOtherBean(@NonNull Map<SomeEnum, EnumMappedBean> beansMappedByEnum) {
this.beansMappedByEnum = beansMappedByEnum;
}
}
In config class you can also validate that every component declare uniqe, non-null enum value.
回答2:
You must add @Qualifier("impl1")
or @Qualifier("impl2")
with @Autowired
to inject your implementation class since the DummyInterface
has multiple implementations.
@Autowired
@Qualifier("impl2") //Injects impl2 class
private EnumMap<Your Enum, DummyInterface> mapping;
Further info on EnumMap
, you can refer here
来源:https://stackoverflow.com/questions/57899018/how-to-injectautowired-beans-to-map-with-enum-as-map-key-in-spring