Like Spring Boot itself (see here), Spring Boot for Apache Geode and Pivotal GemFire (SBDG) supports externalized configuration.
By externalized configuration, we mean configuration meta-data stored in a Spring Boot
application.properties
file,
for instance. Properties can even be delineated by concern, broken out into individual properties files, that are
perhaps only enabled by a specific Profile.
There are many other powerful things you can do, such as use placeholders in properties, encrypt properties, and so on. What we are particularly interested in, in this section, is type-safety.
Like Spring Boot, Spring Boot for Apache Geode/Pivotal GemFire provides a hierarchy of classes used to capture
the configuration of several Apache Geode or Pivotal GemFire features in an associated @ConfigurationProperties
annotated class. Again, the configuration is specified as well-known, documented properties in 1 or more Spring Boot
application.properties
files.
For instance, I may have configured my Spring Boot, ClientCache
application as follows:
Spring Boot application.properties
containing Spring Data properties for Apache Geode / Pivotal GemFire.
# Spring Boot application.properties used to configure Apache Geode spring.data.gemfire.name=MySpringBootApacheGeodeApplication # Configure general cache properties spring.data.gemfire.cache.copy-on-read=true spring.data.gemfire.cache.log-level=debug # Configure ClientCache specific properties spring.data.gemfire.cache.client.durable-client-id=123 spring.data.gemfire.cache.client.keep-alive=true # Configure a log file spring.data.gemfire.logging.log-file=/path/to/geode.log # Configure the client's connection Pool to the servers in the cluster spring.data.gemfire.pool.locators=10.105.120.16[11235],boombox[10334]
There are many other properties a user may use to externalize the configuration of their Spring Boot, Apache Geode application. You may refer to the Spring Data for Apache Geode (SDG) configuration annotations Javadoc for specific configuration properties as needed. Specifically, review the "enabling" annotation attributes.
There may be cases where you require access to the configuration meta-data (specified in properties) in your Spring Boot applications themselves, perhaps to further inspect or act on a particular configuration setting.
Of course, you can access any property using Spring’s Environment
abstraction,
like so:
Using the Spring `Enviornment.
boolean copyOnRead = environment.getProperty("spring.data.gemfire.cache.copy-on-read", Boolean.TYPE, false);
While using the Environment
is a nice approach, you might need access to additional properties or want to access
the property values in a type-safe manner. Therefore, it is now possible, thanks to SBDG’s auto-configured
configuration processor, to access the configuration meta-data using provided @ConfigurationProperties
classes.
Following on to our example above, I can now do the following:
Using GemFireProperties
.
@Component class MyApplicationComponent { @Autowired private GemFireProperties gemfireProperties; public void someMethodUsingGemFireProperties() { boolean copyOnRead = this.gemfireProperties.getCache().isCopyOnRead(); // do something with `copyOnRead` } ... }
Given a handle to GemFireProperties
,
you can access any of the configuration properties used to configure either Apache Geode or Pivotal GemFire in
a Spring context. You simply only need to autowire an instance of GemFireProperties
into your application component.
A complete reference to the SBDG provided @ConfigurationProperties
classes and supporting classes is available
here.
The same capability applies to accessing the externalized configuration of Spring Session when using either Apache Geode or Pivotal GemFire as your (HTTP) Session state caching provider.
In this case, you simply only need to acquire a handle to an instance of the
SpringSessionProperties
class.
As before, you would specify Spring Session for Apache Geode (SSDG) properties as follows:
Spring Boot application.properties
for Spring Session using Apache Geode as the (HTTP) Session state caching provider.
# Spring Boot application.properties used to configure Apache Geode as a Session state caching provider in Spring Session spring.session.data.gemfire.session.expiration.max-inactive-interval-seconds=300 spring.session.data.gemfire.session.region.name=UserSessions
Then, in your application:
Using SpringSessionProperties
.
@Component class MyApplicationComponent { @Autowired private SpringSessionProperties springSessionProperties; public void someMethodUsingSpringSessionProperties() { String sessionRegionName = this.springSessionProperties.getSession().getRegion().getName(); // do something with `sessionRegionName` } ... }