我有三个类 a、b 和 c。都继承了同一个类,我必须根据一个键来选择在运行时使用哪个类。
这是我写的Spring代码:
<bean id="ConfigurationAdapterMap" class="abc">
<property name="adapterMap">
<map>
<entry key="key1">
<bean class="class1"/>
</entry>
<entry key="Other">
<bean class="class2"/>
</entry>
</map>
</property>
</bean>
我不想显式传递“其他”,如果给定键与键集中的任何键都不匹配,我想返回 class2。我可以得到 spring 来做这个吗?
最佳答案
来自文档:
You can also explicitly control the exact type of Map that will be instantiated and populated via the use of the 'map-class' attribute on the element.
因此,例如,您可以从 commons-collections
中获取 DefaultedMap
并尝试将其用作 map-class
。或者您可以实现自己的 Map
并使用它。
关于java - 如何在 Spring 为 map 中未找到的键设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11733817/