Search code examples
javaxmlspringguavaxmltable

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.

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:

<property name="myTable">
        <bean class="com.google.common.collect.HashBasedTable" factory-method="create">
            <!--- how do I insert value in here??? -->
        </bean>
</property>

Solution

  • 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. 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>