Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject Google Guava Hashmultimap using Spring

Tags:

java

spring

guava

Is it possible to provide an example of creating a Multimap<String, String> using Spring?

I am curious in looking at how it would be done in the application context XML file.

like image 377
kuriouscoder Avatar asked Jul 26 '26 14:07

kuriouscoder


1 Answers

Google Collections is deprecated in favor of Guava, so I will answer regarding Guava.

I will use this Bean class:

public class Bean{
    private Multimap<String, String> map;
    public void setMap(Multimap<String, String> map){
        this.map = map;
    }
}

The only Guava Multimap factory method you can easily wire (with XML only) is the Multimaps.forMap(existingMap) method. Here's the XML:

<bean class="foo.bar.Bean">
    <property name="map">
        <bean class="com.google.common.collect.Multimaps"
            factory-method="forMap">
            <constructor-arg>
                <bean class="java.util.HashMap" />
            </constructor-arg>
        </bean>
    </property>
</bean>

If you want one of the more complicated Supplier-based methods in the Multimaps class, you will have to create the Suppliers as Java Classes. If you just want a primitive instantiating Supplier, you can create that as a Spring FactoryBean:

public class SupplierFactoryBean<T> extends AbstractFactoryBean<Supplier<T>>{

    private final Class<T> type;
    private final Supplier<T> supplier;

    public SupplierFactoryBean(final Class<T> type){
        this.type = type;
        this.supplier = new Supplier<T>(){
            @Override
            public T get(){
                try{
                    return type.newInstance();
                } catch(final Exception e){
                    throw new IllegalStateException(e);
                }
            }
        };
    }

    @Override
    public Class<?> getObjectType(){ return type; }

    @Override
    protected Supplier<T> createInstance() throws Exception{
        return supplier;
    }
}

(You may also model the above as a Supplier class without a FactoryBean, the Spring usage remains the same. Just make sure it returns a new instance for every call.)

Now you can wire the more complicated methods. Example:

<bean class="foo.bar.Bean">
    <property name="map">
        <bean class="com.google.common.collect.Multimaps"
            factory-method="newSetMultimap">
            <constructor-arg>
                <bean class="java.util.HashMap" />
            </constructor-arg>
            <constructor-arg>
                <bean class="foo.bar.SupplierFactoryBean">
                    <constructor-arg value="java.util.TreeSet" />
                </bean>
            </constructor-arg>
        </bean>
    </property>
</bean>

Both examples above use static factory methods. Here's the corresponding chapter in the Spring Reference: 3.3.2.2 Instantiation with a static factory method

like image 131
Sean Patrick Floyd Avatar answered Jul 29 '26 05:07

Sean Patrick Floyd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!