Spring Boot for Tanzu GemFire 1.0

Data Access with GemfireTemplate

Last Updated February 19, 2025

This topic explains how to access data stored in GemFire when using Spring Boot for VMware Tanzu GemFire.


There are several ways to access data stored in VMware GemFire.

For example, you can use the Region API (see VMware GemFire Java API Reference) directly. If you are driven by the application’s domain context, you can use the power of Spring Data Repositories instead.

While the Region API offers flexibility, it couples your application to VMware GemFire, which is usually undesirable and unnecessary. While using Spring Data Repositories provides a very powerful and convenient abstraction, you give up the flexibility provided by a lower-level Region API.

A good compromise is to use the Template software design pattern. This pattern is consistently and widely used throughout the entire Spring portfolio.

For example, the Spring Framework provides JdbcTemplate and JmsTemplate.

Spring Data for VMware GemFire offers the GemfireTemplate.

The GemfireTemplate provides a highly consistent and familiar API to perform data access operations on VMware GemFire cache Regions.

GemfireTemplate offers:

  • A simple and convenient data access API to perform basic CRUD and simple query operations on cache Regions.

  • Use of Spring Framework’s consistent data access Exception hierarchy.

  • Automatic enlistment in the presence of local cache transactions.

  • Consistency and protection from Region API (see VMware GemFire Java API Reference) breaking changes.

Given these advantages, Spring Boot for Tanzu GemFire auto-configures GemfireTemplate beans for each Region present in the VMware GemFire cache.

Additionally, Spring Boot for Tanzu GemFire is careful not to create a GemfireTemplate if you have already declared a GemfireTemplate bean in the Spring ApplicationContext for a given Region.

Explicitly Declared Regions

Consider an explicitly declared Region bean definition: 1. Explicitly Declared Region Bean Definition

@Configuration
class GemFireConfiguration {

    @Bean("Example")
    ClientRegionFactoryBean<?, ?> exampleRegion(GemFireCache gemfireCache) {
        // ...
    }
}

Spring Boot for Tanzu GemFire automatically creates a GemfireTemplate bean for the Example Region by using the bean name exampleTemplate. Spring Boot for Tanzu GemFire names the GemfireTemplate bean after the Region by converting the first letter in the Region’s name to lower case and appending Template to the bean name.

In a managed Data Access Object (DAO), you can inject the Template:

@Repository
class ExampleDataAccessObject {

    @Autowired
    @Qualifier("exampleTemplate")
    private GemfireTemplate exampleTemplate;

}

You should use the @Qualifier annotation to qualify which GemfireTemplate bean you are specifically referring, especially if you have more than one Region bean definition.

Entity-defined Regions

Spring Boot for Tanzu GemFire auto-configures GemfireTemplate beans for entity-defined Regions.

Consider the following entity class:

Example 1. Customer class

@Region("Customers")
class Customer {
    // ...
}

Further, consider the following configuration:

Example 2. VMware GemFire Configuration

@Configuration
@EnableEntityDefinedRegions(basePackageClasses = Customer.class)
class GemFireConfiguration {
    // ...
}

Spring Boot for Tanzu GemFire auto-configures a GemfireTemplate bean for the Customers Region named customersTemplate, which you can then inject into an application component:

Example 3. CustomerService application component

@Service
class CustomerService {

    @Autowired
    @Qualifier("customersTemplate")
    private GemfireTemplate customersTemplate;

}

Again, be careful to qualify the GemfireTemplate bean injection if you have multiple Regions, whether declared explicitly or implicitly, such as when you use the @EnableEntityDefineRegions annotation.

Caching-defined Regions

Spring Boot for Tanzu GemFire auto-configures GemfireTemplate beans for caching-defined Regions.

When you use Spring Framework’s Cache Abstraction backed by VMware GemFire, one requirement is to configure Regions for each of the caches specified in the caching annotations of your application service components.

Fortunately, Spring Boot for Tanzu GemFire makes enabling and configuring caching easy and automatic.

Consider the following cacheable application service component:

Example 4. Cacheable CustomerService class

@Service
class CacheableCustomerService {

    @Autowired
    @Qualifier("customersByNameTemplate")
    private GemfireTemplate customersByNameTemplate;

    @Cacheable("CustomersByName")
    public Customer findBy(String name) {
        return toCustomer(customersByNameTemplate.query("name = " + name));
    }
}

Further, consider the following configuration:

Example 5. VMware GemFire Configuration

@Configuration
@EnableCachingDefinedRegions
class GemFireConfiguration {

    @Bean
    public CustomerService customerService() {
        return new CustomerService();
    }
}

Spring Boot for Tanzu GemFire auto-configures a GemfireTemplate bean named customersByNameTemplate to perform data access operations on the CustomersByName (@Cacheable) Region. You can then inject the bean into any managed application component, as shown in the preceding application service component example.

Again, be careful to qualify the GemfireTemplate bean injection if you have multiple Regions, whether declared explicitly or implicitly, such as when you use the @EnableCachingDefineRegions annotation.

Warning: Autowiring, or injecting GemfireTemplate beans auto-configured by Spring Boot for Tanzu GemFire for caching-defined Regions into your application components does not always work. This has to do with the Spring container bean creation process. In those cases, you may need to look up the GemfireTemplate by using applicationContext.getBean("customersByNameTemplate", GemfireTemplate.class). This works when autowiring does not.

Native-defined Regions

Spring Boot for Tanzu GemFire even auto-configures GemfireTemplate beans for Regions that have been defined with VMware GemFire native configuration metadata, such as cache.xml.

Consider the following VMware GemFire native cache.xml:

Example 6. Client cache.xml

<?xml version="1.0" encoding="UTF-8"?>
<client-cache xmlns="http://geode.apache.org/schema/cache"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://geode.apache.org/schema/cache http://geode.apache.org/schema/cache/cache-1.0.xsd"
              version="1.0">

    <region name="Example" refid="LOCAL"/>

</client-cache>

Further, consider the following Spring configuration:

Example 7. VMware GemFire Configuration

@Configuration
@EnableGemFireProperties(cacheXmlFile = "cache.xml")
class GemFireConfiguration {
    // ...
}

Spring Boot for Tanzu GemFire auto-configures a GemfireTemplate bean named exampleTemplate after the Example Region defined in cache.xml. You can inject this template as you would any other Spring-managed bean:

Example 8. Injecting the GemfireTemplate

@Service
class ExampleService {

    @Autowired
    @Qualifier("exampleTemplate")
    private GemfireTemplate exampleTemplate;

}

The rules described earlier apply when multiple Regions are present.

Template Creation Rules

Fortunately, Spring Boot for Tanzu GemFire is careful not to create a GemfireTemplate bean for a Region if a template by the same name already exists.

For example, consider the following configuration:

Example 9. VMware GemFire Configuration

@Configuration
@EnableEntityDefinedRegions(basePackageClasses = Customer.class)
class GemFireConfiguration {

    @Bean
    public GemfireTemplate customersTemplate(GemFireCache cache) {
        return new GemfireTemplate(cache.getRegion("/Customers"));
    }
}

Further, consider the following example:

Example 10. Customer class

@Region("Customers")
class Customer {
    // ...
}

Because you explicitly defined and declared the customersTemplate bean, Spring Boot for Tanzu GemFire does not automatically create a template for the Customers Region. This applies regardless of how the Region was created, whether by using @EnableEntityDefinedRegions, @EnableCachingDefinedRegions, explicitly declaring Regions, or natively defining Regions.

Even if you name the template differently from the Region for which the template was configured, Spring Boot for Tanzu GemFire conserves resources and does not create the template.

For example, suppose you named the GemfireTemplate bean vipCustomersTemplate, even though the Region name is Customers, based on the @Region annotated Customer class, which specified the Customers Region.

With the following configuration, Spring Boot for Tanzu GemFire is still careful not to create the template:

Example 11. VMware GemFire Configuration

@Configuration
@EnableEntityDefinedRegions(basePackageClasses = Customer.class)
class GemFireConfiguration {

    @Bean
    public GemfireTemplate vipCustomersTemplate(GemFireCache cache) {
        return new GemfireTemplate(cache.getRegion("/Customers"));
    }
}

Spring Boot for Tanzu GemFire identifies that your vipCustomersTemplate is the template used with the Customers Region, and Spring Boot for Tanzu GemFire does not create the customersTemplate bean, which would result in two GemfireTemplate beans for the same Region.

Note: The name of your Spring bean defined in Java configuration is the name of the method if the Spring bean is not explicitly named by using the name attribute or the value attribute of the @Bean annotation.