[英]Define guava HashBasedTable/Table in spring xml config
I'm trying to create and populate a guava HashBasedTable
in spring xml config file but I haven't been able to. 我正在尝试在spring xml配置文件中创建并填充番石榴
HashBasedTable
,但我无法做到。
My table looks like this: 我的表看起来像这样:
Table<String, Foo, Bar> myTable;
And I've tried this in my xml but don't know how put new value into the table: 我已经在我的xml中尝试了这个,但不知道如何将新值放入表中:
<property name="myTable">
<bean class="com.google.common.collect.HashBasedTable" factory-method="create">
<!--- how do I insert value in here??? -->
</bean>
</property>
If you want to do this exclusively in xml, it's a bit tricky: I see guava doesn't offer too many options for putting values in that Table. 如果你想在xml中专门做这个,那有点棘手:我看到guava没有提供太多选项来在表中放置值。 There is an approach, but it's weird for more than one insert:
有一种方法,但对于多个插入物来说很奇怪:
<bean id="myTable" class="com.google.common.collect.HashBasedTable" factory-method="create" />
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="myTable" />
</property>
<property name="targetMethod">
<value>put</value>
</property>
<property name="arguments">
<list>
<value>1</value>
<value>1</value>
<value>value</value>
</list>
</property>
</bean>
If you have not to use exclusively xml, you can use some Java to make your configuration a little more readable. 如果您不必专门使用xml,则可以使用一些Java来使您的配置更具可读性。
You can create an Utility method: 您可以创建一个Utility方法:
public class Utils {
public static Table tableFromMap(Map<Object, Map<Object, Object>> map){
Table ret = null;
if(map != null){
ret = HashBasedTable.create();
for(Object k1 : map.keySet()){
if(map.get(k1) != null){
for(Object k2 : map.get(k1).keySet()){
ret.put(k1, k2, map.get(k1).get(k2));
}
}
}
}
return ret;
}
}
And add this to your configuration 并将其添加到您的配置中
<bean id="mytable" class="it.myproject.Utils" factory-method="tableFromMap">
<constructor-arg>
<util:map>
<entry key="A">
<util:map>
<entry key="B" value="C" />
<entry key="D" value="E" />
</util:map>
</entry>
</util:map>
</constructor-arg>
</bean>
Resulting in this table: 导致此表:
A | B | C
A | D | E
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.