How to start Embedded Gemfire Application with SpringBoot

Gemfire is a memory caching solution for scaling software system, JavaSampleApproach will guide you how to create an Embedded Gemfire Application with SpringBoot.

Related article:
Infinispan Cache Solution | Spring Cache | Spring Boot
How to create a SpringBoot Gemfire RestfulApi

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE

II. Overview
1. Project Structure

embedded gemfire application

2. Step to do

– Create Spring Boot project
– Add needed dependencies
– Create a Gemfire Region Model
– Config Gemfire in Local model
– Implement Gemfire Repository
– Implement client for check Gemfire’s working
– Run SpringBoot project and result

III. Practices
1. Create Spring Boot project

Open Spring Tool Suite, on Menu bar, choose: File->New->Spring Starter Project, input needed project’s info:
embedded gemfire application project info

Press: Next then Finish, Spring boot project will be created

2. Add needed dependencies

– Add gemfire & spring-shell dependencies:



	SpringBoot Gemfire Config








3. Create a Gemfire Region Model

@Region: Annotation to define the region an entity will be stored in.

package com.javasampleapproach.gemfire.model;


public class Customer {
    private String firstname;
    private String lastname;
    private int age;
    public Customer(String firstname, String lastname, int age) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.age = age;

	public String getFirstname() {
		return firstname;

	public void setFirstname(String firstname) {
		this.firstname = firstname;

	public String getLastname() {
		return lastname;

	public void setLastname(String lastname) {
		this.lastname = lastname;
	public String toString(){
		return "firstname: " + firstname + " ,lastname: " + lastname + " ,age: " + age; 

	public int getAge() {
		return age;

	public void setAge(int age) {
		this.age = age;

4. Config Gemfire in Local model

Config Gemfire cache via @Bean: CacheFactoryBean gemfireCache()
Config Gemfire Region via @Bean: LocalRegionFactoryBean customerRegion(final GemFireCache cache)

package com.javasampleapproach.gemfire.config;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.gemstone.gemfire.cache.GemFireCache;
import com.javasampleapproach.gemfire.model.Customer;

public class GemfireConfig {
    Properties gemfireProperties() {
        Properties gemfireProperties = new Properties();
        gemfireProperties.setProperty("name", "EmbeddedGemfireApplication");
        gemfireProperties.setProperty("mcast-port", "0");
        return gemfireProperties;
    CacheFactoryBean gemfireCache() {
        CacheFactoryBean gemfireCache = new CacheFactoryBean();
        return gemfireCache;
    LocalRegionFactoryBean customerRegion(final GemFireCache cache) {
        LocalRegionFactoryBean customerRegion = new LocalRegionFactoryBean<>();
        return customerRegion;

In a main class of Spring Boot, use @EnableGemfireRepositories for enable Gemfire Caching.

5. Implement Gemfire Repository

Spring Data provides an interface: CrudRepository extends Repository for implementation a repository to retrieve entities from Gemfire caching:

package com.javasampleapproach.gemfire.repository;


import com.javasampleapproach.gemfire.model.Customer;

public interface CustomerRepository extends CrudRepository {
	Customer findByFirstname(String firstname);
	Customer findByLastname(String lastname);
	Iterable findByAgeGreaterThan(int age);
	Iterable findByAgeLessThan(int age);

Above sourcecode, we defines 4 methods to search gemfire entities base on: firstname, lastname and age.

6. Implement client for check Gemfire’s working

For a sample client, in main class, we implements CommandLineRunner for saving some customer entities then retrieving them.

package com.javasampleapproach.gemfire;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.javasampleapproach.gemfire.model.Customer;
import com.javasampleapproach.gemfire.repository.CustomerRepository;

public class SpringBootGemfireApplication implements CommandLineRunner{

    CustomerRepository customerRepository;
	public static void main(String[] args) {, args);

	public void run(String... arg0) throws Exception {
		Customer peter = new Customer("Peter", "Williams", 20);
		Customer mary = new Customer("Mary", "Diaz", 25);
		// SAVE customer to Gemfire;;
		// FindAll Customers
		System.out.println("-------Find All Customers-------");
		Iterable custList = customerRepository.findAll();
		for(Customer c: custList){
		// Find a Customer by firstname
		System.out.println("-------Find Customer by FirstName-------");
		Customer c1 = customerRepository.findByFirstname("Peter");
		// Find a Customer by lastname
		System.out.println("-------Find Customer by LastName-------");
		Customer c2 = customerRepository.findByLastname("Diaz");
		// find customer by age
		System.out.println("-------Find Customers have age greater than 22-------");
		Iterable custLstByAgeGreaterThan22 = customerRepository.findByAgeGreaterThan(22);
		for(Customer c: custLstByAgeGreaterThan22){
		System.out.println("-------Find Customers have age less than 30-------");
		Iterable custLstByAgeLessThan30 = customerRepository.findByAgeLessThan(30);
		for(Customer c: custLstByAgeLessThan30){

7. Run SpringBoot project and result

Gemfire Logs:

System Properties:
    PID = 3276
    awt.toolkit =
    file.encoding = UTF-8
    file.encoding.pkg =
    file.separator = \
    java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment
    java.awt.headless = true
    java.awt.printerjob =
    java.class.version = 52.0
    java.endorsed.dirs = C:\Program Files\Java\jdk1.8.0_101\jre\lib\endorsed
    java.ext.dirs = C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
    java.home = C:\Program Files\Java\jdk1.8.0_101\jre = C:\Users\User\AppData\Local\Temp\ = Java(TM) SE Runtime Environment
    java.runtime.version = 1.8.0_101-b13 = Java Platform API Specification
    java.specification.vendor = Oracle Corporation
    java.specification.version = 1.8
    java.vendor = Oracle Corporation
    java.vendor.url =
    java.vendor.url.bug =
    java.version = 1.8.0_101 = mixed mode = Java HotSpot(TM) 64-Bit Server VM = Java Virtual Machine Specification
    java.vm.specification.vendor = Oracle Corporation
    java.vm.specification.version = 1.8
    java.vm.vendor = Oracle Corporation
    java.vm.version = 25.101-b13
    line.separator = 

    log4j.configurationFile = jar:file:/C:/Users/User/.m2/repository/com/gemstone/gemfire/gemfire/8.2.0/gemfire-8.2.0.jar!/com/gemstone/gemfire/internal/logging/log4j/log4j2-default.xml
    org.jboss.logging.provider = slf4j
    os.version = 10.0
    p2p.useSSL = false
    path.separator = ;
    spring.beaninfo.ignore = true = 64
    sun.boot.class.path = C:\Program Files\Java\jdk1.8.0_101\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_101\jre\classes
    sun.boot.library.path = C:\Program Files\Java\jdk1.8.0_101\jre\bin
    sun.cpu.endian = little
    sun.cpu.isalist = amd64
    sun.desktop = windows = UnicodeLittle = com.javasampleapproach.gemfire.SpringBootGemfireApplication = SUN_STANDARD
    sun.jnu.encoding = Cp1252 = HotSpot 64-Bit Tiered Compilers = 
    sun.os.patch.level = = US
    user.language = en
    user.script = 
    user.timezone = Asia/Bangkok
    user.variant = 


-------Find All Customers-------
firstname: Mary ,lastname: Diaz ,age: 25
firstname: Peter ,lastname: Williams ,age: 20
-------Find Customer by FirstName-------
firstname: Peter ,lastname: Williams ,age: 20
-------Find Customer by LastName-------
firstname: Mary ,lastname: Diaz ,age: 25
-------Find Customers have age greater than 22-------
firstname: Mary ,lastname: Diaz ,age: 25
-------Find Customers have age less than 30-------
firstname: Mary ,lastname: Diaz ,age: 25
firstname: Peter ,lastname: Williams ,age: 20

IV. Sourcecode


By grokonez | December 25, 2016.

Last updated on June 4, 2017.

Related Posts

1 thought on “How to start Embedded Gemfire Application with SpringBoot”

  1. No qualifying bean of type ‘CustomerRepository’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)

Got Something To Say:

Your email address will not be published. Required fields are marked *