Favor JSR-107 provider if present

Previously, native cache libraries were favored over a standard JSR-107
implementation. If a user has a working setup using JCache with one
provider and switch to another provider, his setup may be broken if we
happen to provide a native support for the new provider.

We now consistently favor JSR-107 if it is present. Native support can
still be enabled via the `spring.cache.type` property.

Closes gh-3822
This commit is contained in:
Stephane Nicoll
2015-08-25 15:07:24 +02:00
parent 79b9bdec62
commit 63d157bb7c
5 changed files with 67 additions and 53 deletions

View File

@@ -10,6 +10,7 @@ abstraction is supported by many caching libraries, including:
* `Redis`
* `Guava`
* Simple provider based on `ConcurrentHashMap`
* Generic provider based on `org.springframework.Cache` bean definition(s)
The sample defines a simple `CountryService` that caches countries by ISO code. When
the application starts a client invokes the service with a random code every 500ms. You
@@ -35,6 +36,25 @@ as explained below.
=== JCache (JSR-107)
If you want to configure your cache infrastructure via the standard, you need a compliant
implementation and the JSR-107 api. You first need to add `javax.cache:cache-api` to your
project. Then you could try the following:
* `Hazelcast`: add `com.hazelcast:hazelcast`
* `Infinispan`: add `org.infinispan:infinispan-jcache`
TIP: Certain cache providers do not create a default cache on-the-fly if it does not exist
so you might need to update the sample to create the caches on startup or specify the
location to the provider-specific file via the `spring.cache.jcache.config` property.
NOTE: Any other JSR-107 compliant provider is also supported but Spring Boot may not
offer a dependency management entry for it. You will have to add it with the version
of the library that you want to use.
=== EhCache 2.x
Simply add the `net.sf.ehcache:ehcache` dependency to the project. Since there is a
default `ehcache.xml` configuration file at the root of the classpath, it is automatically
@@ -59,20 +79,6 @@ can set the `spring.cache.infinispan.config` property to use the provided
=== JCache (JSR-107)
If you want to configure your cache infrastructure via the standard, you need a compliant
implementation. You could try the following:
* `Hazelcast`: add `com.hazelcast:hazelcast`
* `Infinispan`: add `org.infinispan:infinispan-jcache`
Since Spring Boot supports the native cache library and the JCache wrapper, you
should set the `spring.cache.type` property to `jcache` to specify that you want the
cache manager to be auto-configured that way.
=== Redis
Add the `spring-boot-starter-redis` and make sure it is configured properly (by default,
a redis instance with the default settings is expected on your local box).

View File

@@ -33,6 +33,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- JSR-107 API (uncomment to try the JCache support) -->
<!--
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
-->
<!-- Additional cache providers (uncomment to try them) -->
<!--
<dependency>

View File

@@ -7,7 +7,6 @@
#
# JCache configuration (example with hazelcast).
#
#spring.cache.type=jcache
#spring.cache.jcache.config=hazelcast.xml