Support customization of CacheManager

This commit allows to customize the auto-configured `CacheManager` by
exposing a bean of type `CacheManagerCustomizer`. The implementation may
reference the type of a `CacheManager` to determine in which case it has
to be invoked.

Several implementations can be provided and ordered using the regular
`Ordered` interface and `@Order` annotation.

Closes gh-5039
This commit is contained in:
Stephane Nicoll
2016-02-24 11:38:05 +01:00
parent ce71bd997c
commit 5febd497da
14 changed files with 445 additions and 17 deletions

View File

@@ -3184,6 +3184,29 @@ Spring Boot tries to detect the following providers (in this order):
It is also possible to _force_ the cache provider to use via the `spring.cache.type`
property.
If the `CacheManager` is auto-configured by Spring Boot, you can further tune its
configuration before it is fully initialized by exposing a bean implementing the
`CacheManagerCustomizer` interface. The following set the cache names to use.
[source,java,indent=0]
----
@Bean
public CacheManagerCustomizer<ConcurrentMapCacheManager> cacheManagerCustomizer() {
return new CacheManagerCustomizer<ConcurrentMapCacheManager>() {
@Override
public void customize(ConcurrentMapCacheManager cacheManager) {
cacheManager.setCacheNames(Arrays.asList("one", "two"));
}
};
}
----
[NOTE]
===
In the example above, a `ConcurrentMapCacheManager` is expected to be configured. If that
is not the case, the customizer won't be invoked at all. You can have as many customizers
as you want and you can also order them as usual using `@Order` or `Ordered`.
===
[[boot-features-caching-provider-generic]]