title: Hazelcast and JCache in Spring Boot creates two instances
body: <p>It looks like the default Spring boot auto configuration will create two hazelcast instances when using JCache and caching is enabled (<code>@EnableCaching</code>)</p>
  
  <p>Full example at: <a href="https://github.com/dirkvanrensburg/hazelcast-springboot-jcache" rel="nofollow noreferrer">https://github.com/dirkvanrensburg/hazelcast-springboot-jcache</a></p>
  
  <blockquote>
    <p><strong>TLDR;</strong> 
    Is there a way to get Spring boot's autoconfiguration to only create one Hazelcast instance when enabling caching through JCache?</p>
  </blockquote>
  
  <p>I created a demo Spring boot project by adding the following dependencies:</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-cache&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;javax.cache&lt;/groupId&gt;
          &lt;artifactId&gt;cache-api&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.hazelcast&lt;/groupId&gt;
          &lt;artifactId&gt;hazelcast&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.hazelcast&lt;/groupId&gt;
          &lt;artifactId&gt;hazelcast-spring&lt;/artifactId&gt;
          &lt;version&gt;${hazelcast.version}&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>and adding <code>@EnableCaching</code> to the Application class, Spring will auto configure Hazelcast but start two hazelcast instances, which joins in a cluster as evidenced in the logs:</p>
  
  <pre><code>Members [2] {
      Member [192.168.1.157]:5701 - 3eabbe90-6815-49ff-8d93-9e4b12e67810
      Member [192.168.1.157]:5702 - e9c93366-2408-4726-965a-b21dcf897113 this
  }
  </code></pre>
  
  <p>The caching works but I don't want two instances of Hazelcast.</p>
  
  <p><strong>Hack</strong></p>
  
  <p>I managed to make it work by providing my own cache manager:</p>
  
  <pre><code>@Bean
  public CacheManager springHzProvider(HazelcastInstance instance) {
      return SpringHazelcastCachingProvider.getCacheManager(instance, null, new Properties());
  }
  </code></pre>
  
  <p>and removing the <code>hazelcast</code> and <code>hazelcast-spring</code> dependencies and adding <code>hazelcast-all</code>:</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;com.hazelcast&lt;/groupId&gt;
          &lt;artifactId&gt;hazelcast-all&lt;/artifactId&gt;
          &lt;version&gt;${hazelcast.version}&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>But the question remains whether there is better 'proper' way of achieving this? Ideally without defining a custom cache manager and adding <code>hazelcast-all</code></p>