Chapter 14. Spring Integration

Table of Contents

14.1. Configuration
14.2. Spring Managed Context
14.3. Spring Cache
14.4. Hibernate 2nd Level Cache Config
14.5. Spring Data - JPA
14.6. Spring Data - MongoDB

14.1. Configuration

You can declare Hazelcast beans for Spring context using beans namespace (default spring beans namespace) as well to declare hazelcast maps, queues and others. Hazelcast-Spring integration requires either hazelcast-spring jar or hazelcast-all jar in the classpath.

<bean id="instance" class="com.hazelcast.core.Hazelcast" factory-method="newHazelcastInstance">
        <bean class="com.hazelcast.config.Config">
            <property name="groupConfig">
                <bean class="com.hazelcast.config.GroupConfig">
                    <property name="name" value="dev"/>
                    <property name="password" value="pwd"/>
            <!-- and so on ... -->

<bean id="map" factory-bean="instance" factory-method="getMap">
    <constructor-arg value="map"/>

Hazelcast has Spring integration (requires version 2.5 or greater) since 1.9.1 using hazelcast namespace.

  • Add namespace xmlns:hz="" to beans tag in context file:

    <beans xmlns=""

  • Use hz namespace shortcuts to declare cluster, its items and so on.

After that you can configure Hazelcast instance (node):

<hz:hazelcast id="instance">
        <hz:group name="dev" password="password"/>
        <hz:network port="5701" port-auto-increment="false">
                <hz:multicast enabled="false"
                <hz:tcp-ip enabled="true">
        <hz:map name="map"

You can easily configure map-store and near-cache too. (For map-store you should set either class-name or implementation attribute.)

    <hz:map name="map1">
            <hz:near-cache time-to-live-seconds="0" max-idle-seconds="60"
               eviction-policy="LRU" max-size="5000"  invalidate-on-change="true"/>

            <hz:map-store enabled="true" class-name=""

    <hz:map name="map2">
            <hz:map-store enabled="true" implementation="dummyMapStore"

    <bean id="dummyMapStore" class="" />

It's possible to use placeholders instead of concrete values. For instance, use property file for group configuration:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">

<hz:hazelcast id="instance">
        <!-- ... -->

Similar for client

<hz:client id="client"
    group-name="${}" group-password="${}">

Hazelcast also supports lazy-init, scope and depends-on bean attributes.

<hz:hazelcast id="instance" lazy-init="true" scope="singleton">

<hz:client id="client" scope="prototype" depends-on="instance">

You can declare beans for the following Hazelcast objects:

  • map

  • multiMap

  • queue

  • topic

  • set

  • list

  • executorService

  • idGenerator

  • atomicNumber

  • semaphore

  • countDownLatch

  • lock


<hz:map id="map" instance-ref="client" name="map" lazy-init="true" />
<hz:multiMap id="multiMap" instance-ref="instance" name="multiMap" lazy-init="false" />
<hz:queue id="queue" instance-ref="client" name="queue" lazy-init="true" depends-on="instance"/>
<hz:topic id="topic" instance-ref="instance" name="topic" depends-on="instance, client"/>
<hz:set id="set" instance-ref="instance" name="set" />
<hz:list id="list" instance-ref="instance" name="list"/>
<hz:executorService id="executorService" instance-ref="client" name="executorService"/>
<hz:idGenerator id="idGenerator" instance-ref="instance" name="idGenerator"/>
<hz:atomicNumber id="atomicNumber" instance-ref="instance" name="atomicNumber"/>
<hz:atomicNumber id="semaphore" instance-ref="client" name="semaphore"/>
<hz:atomicNumber id="countDownLatch" instance-ref="instance" name="countDownLatch"/>
<hz:atomicNumber id="lock" instance-ref="client" name="lock"/>

Injecting Typed Collections/Maps

Spring tries to create a new Map/Collection instance and fill the new instance by iterating and converting values of the original Map/Collection (IMap, IQueue etc.) to required types when generic type parameters of the original Map/Collection and the target property/attribute do not match.

Since Hazelcast Maps/Collections are designed to hold very large data which a single machine can not carry, iterating through whole values can cause out of memory errors.

To avoid this issue either target property/attribute can be declared as un-typed Map/Collection

public class SomeBean {
    IMap map; // instead of IMap<K, V> map

    IQueue queue; // instead of IQueue<E> queue


or parameters of injection methods (constructor, setter) can be un-typed.

public class SomeBean {

    IMap<K, V> map;

    IQueue<E> queue;

    public SomeBean(IMap map) { // instead of IMap<K, V> map = map;


    public void setQueue(IQueue queue) { // instead of IQueue<E> queue
        this.queue = queue;

For more info see Spring issue-3407 .