[[geode-configuration-externalized]]
== Externalized Configuration
:geode-name: {apache-geode-name}


Like Spring Boot itself (see {spring-boot-docs-html}/boot-features-external-config.html[here]), Spring Boot
for {geode-name} (SBDG) supports externalized configuration.

By externalized configuration, we mean configuration metadata stored in a Spring Boot
{spring-boot-docs-html}/boot-features-external-config.html#boot-features-external-config-application-property-files[`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 {spring-boot-docs-html}/boot-features-external-config.html#boot-features-external-config-profile-specific-properties[Profile].

There are many other powerful things you can do, such as, but not limited to, using
{spring-boot-docs-html}/boot-features-external-config.html#boot-features-external-config-placeholders-in-properties[placeholders]
in properties, {spring-boot-docs-html}/boot-features-external-config.html#boot-features-encrypting-properties[encrypting]
properties, and so on.  What we are particularly interested in, in this section, is
{spring-boot-docs-html}/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties[type-safety].

Like Spring Boot, Spring Boot for {geode-name} provides a hierarchy of classes used to capture configuration for several
{geode-name} features in an associated `@ConfigurationProperties` annotated class. Again, the configuration metadata 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 {geode-name}
[source,properties]
----
# Spring Boot application.properties used to configure {geode-name}

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,
{geode-name} applications.  You may refer to the Spring Data for {geode-name} (SDG) configuration annotations
{spring-data-geode-javadoc}/org/springframework/data/gemfire/config/annotation/package-frame.html[Javadoc]
for specific configuration properties as needed.  Specifically, review the "_enabling_" annotation attributes.

There may be cases where you require access to the configuration metadata (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 {spring-framework-javadoc}/org/springframework/core/env/Environment.html[`Environment`] abstraction,
like so:

.Using the Spring `Environment`
[source,java]
----
@Configuration
class GeodeConfiguration {
  void readConfigurationFromEnvironment(Environment environment) {
    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 metadata using provided `@ConfigurationProperties` classes.

Following on to our example above, I can now do the following:

.Using `GemFireProperties`
[source,java]
----

@Component
class MyApplicationComponent {

  @Autowired
  private GemFireProperties gemfireProperties;

  public void someMethodUsingGemFireProperties() {

    boolean copyOnRead = this.gemfireProperties.getCache().isCopyOnRead();

    // do something with `copyOnRead`
  }
}
----

Given a handle to {spring-boot-data-geode-javadoc}/org/springframework/geode/boot/autoconfigure/configuration/GemFireProperties.html[`GemFireProperties`],
you can access any of the configuration properties used to configure {geode-name} 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
{spring-boot-data-geode-javadoc}/org/springframework/geode/boot/autoconfigure/configuration/package-frame.html[here].

[[geode-configuration-externalized-session]]
=== Externalized Configuration of Spring Session

The same capability applies to accessing the externalized configuration of Spring Session when using {geode-name} as
your (HTTP) Session state caching provider.

In this case, you simply only need to acquire a reference to an instance of the
{spring-boot-data-geode-javadoc}/org/springframework/geode/boot/autoconfigure/configuration/SpringSessionProperties.html[`SpringSessionProperties`]
class.

As before, you would specify Spring Session for {geode-name} (SSDG) properties as follows:

.Spring Boot `application.properties` for Spring Session using {geode-name} as the (HTTP) Session state caching provider
[source,properties]
----
# Spring Boot application.properties used to configure {geode-name} 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`
[source,java]
----
@Component
class MyApplicationComponent {

  @Autowired
  private SpringSessionProperties springSessionProperties;

  public void someMethodUsingSpringSessionProperties() {

    String sessionRegionName = this.springSessionProperties.getSession().getRegion().getName();

    // do something with `sessionRegionName`
  }
}
----
