This section dives into the Coherence Spring Boot module. It explains how to use Coherence’s dedicated support for Spring Boot, e.g. Autoconfiguration.
1. Getting Started
In order to start using Coherence with Spring Boot you have to add the coherence-spring-boot-starter
dependency as well
as the desired version of Coherence.
<dependencies>
<dependency>
<groupId>com.oracle.coherence.spring</groupId>
<artifactId>coherence-spring-boot-starter</artifactId>
<version>3.0.0-M1</version>
</dependency>
<dependency>
<groupId>com.oracle.coherence.ce</groupId>
<artifactId>coherence</artifactId>
<version>21.06-M2</version>
</dependency>
</dependencies>
dependencies {
compile("com.oracle.coherence.spring:coherence-spring-boot-starter:3.0.0-M1")
compile("com.oracle.coherence.ce:coherence:21.06-M2")
}
As Coherence Spring takes advantage of the new Coherence Bootstrap API, it requires Oracle Coherence CE version
20.12 or higher.
|
2. Using Coherence with Spring Boot
By adding the coherence-spring-boot-starter
dependency, AutoConfiguration will be activated via the
CoherenceAutoConfiguration
class. This will also bind the CoherenceProperties
class for further configuration. The YAML configuration for Spring Boot’s Coherence support may look like the following:
coherence:
logging:
destination: slf4j
logger-name: MyCoherence
sessions:
- name: default
config: "coherence-cache-config.xml"
priority: 1
- name: test
config: "test-coherence-config.xml"
priority: 2
properties:
coherence.log.limit: 400
coherence.log.level: 1
The following configuration properties are available.
Key | Default Value | Description |
---|---|---|
coherence.logging.destination |
|
The type of the logging destination. |
coherence.logging.severity-level |
Specifies which logged messages are emitted to the log destination. The legal values are |
|
coherence.logging.logger-name |
|
Specifies a logger name within chosen logging system |
coherence.logging.message-format |
Specifies how messages that have a logging level specified are formatted before passing them to the log destination. |
|
coherence.logging.character-limit |
Specifies the maximum number of characters that the logger daemon processes from the message queue before discarding all remaining messages in the queue |
|
coherence.sessions.<type> |
N/A |
Indicates the type of the session. One of the following: |
coherence.sessions.<type>[0].name |
Namee of the session. |
|
coherence.sessions.<type>[0].config |
The Coherence cache configuration URI for the session |
|
coherence.sessions.<type>[0].priority |
|
The priority order to be used when starting the session. Sessions will be started with the lowest priority first. |
coherence.sessions.<type>[0].scope-name |
The scope name for the session. |
Session
-related configuration properties are defined based on the session type in:
All but the Session properties are translated into native Coherence properties. If both
Spring Boot property AND a native property coherence.properties.* are configured, the Spring Boot
property is used.
|
If the provided Spring Boot configuration properties are not sufficient for your needs, you can also specify any of the native Coherence properties. For a list of available native properties, please consult the reference guide chapter on System Property Overrides.
3. Coherence Support of the Spring Boot ConfigData API
Since Spring Boot 2.4.x
, you can define your own
custom config locations. This allows you to
import these config locations as property sources. As such, Coherence Spring allows you to use a Coherence cluster as a source of
configuration data for your Spring Boot based applications.
Please also consult the Spring Boot reference guide on Externalized Configuration, especially the chapter on Importing Additional Data. |
Please also see the chapter on Coherence Spring Cloud Config. |
You can trigger the import of remote configuration properties with the Spring Boot property
spring.config.import
using the value coherence:
. You will also need the corresponding Coherence config-client
configured to specify any remote connection settings for your Coherence cluster as well as settings specifying how
properties will be applied, e.g. the name of the application used to fetch remote properties.
spring:
config:
import: coherence:
coherence:
config-client:
application-name: berlin
profile: kona
Many properties have sensible default value. Please see class CoherenceConfigClientProperties for details.
4. Caching with Spring Boot
If you have not already done so, please read the chapter Configuring Coherence Cache for Spring first.
In this chapter, we see how we can use caching with Coherence in Spring Boot.
As long as coherence-spring-boot-starter
is added to your project, and caching is enabled via @EnableCaching
,
Coherence Autoconfiguration
will automatically provide a
CoherenceCacheManager (implementing
Spring’s CacheManager) to the ApplicationContext. However, the
CacheManager
is only added if no CacheManager
was configured explicitly beforehand.
Once that is in place, you can take advantage of
Spring’s Cache abstraction
backed by Coherence and use Spring’s annotations such as @Cacheable
, @CacheEvict
, @CachePut
etc.
When learning about Coherence’s Spring cache abstraction support, please familiarize yourself with the following resources:
|