Commit 1457a55e authored by Stephane Nicoll's avatar Stephane Nicoll

Remove spring.cache.config property

Remove `spring.cache.config`  as it is too generic and does not express
enough what is configured. This property is replaced by cache library
specific properties, that is `spring.cache.ehcache.config`,
`spring.cache.hazelcast.config`, `spring.cache.infinispan.config` and
`spring.cache.jcache.config`.

See gh-2633
parent 6575b31f
......@@ -34,10 +34,14 @@ abstract class CacheConfigFileCondition extends SpringBootCondition {
private final String name;
private final String configPrefix;
private final String[] resourceLocations;
public CacheConfigFileCondition(String name, String... resourceLocations) {
public CacheConfigFileCondition(String name, String configPrefix,
String... resourceLocations) {
this.name = name;
this.configPrefix = configPrefix;
this.resourceLocations = resourceLocations;
}
......@@ -45,9 +49,10 @@ abstract class CacheConfigFileCondition extends SpringBootCondition {
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
context.getEnvironment(), "spring.cache.");
context.getEnvironment(), this.configPrefix);
if (resolver.containsProperty("config")) {
return ConditionOutcome.match("A spring.cache.config property is specified");
return ConditionOutcome.match("A '" + this.configPrefix + ".config' " +
"property is specified");
}
return getResourceOutcome(context, metadata);
}
......
......@@ -38,17 +38,18 @@ public class CacheProperties {
*/
private CacheType type;
/**
* The location of the configuration file to use to initialize the cache library.
*/
private Resource config;
/**
* Comma-separated list of cache names to create if supported by the underlying cache
* manager. Usually, this disables the ability to create additional caches on-the-fly.
*/
private List<String> cacheNames = new ArrayList<String>();
private final EhCache ehcache = new EhCache();
private final Hazelcast hazelcast = new Hazelcast();
private final Infinispan infinispan = new Infinispan();
private final JCache jcache = new JCache();
private final Guava guava = new Guava();
......@@ -61,14 +62,6 @@ public class CacheProperties {
this.type = mode;
}
public Resource getConfig() {
return this.config;
}
public void setConfig(Resource config) {
this.config = config;
}
public List<String> getCacheNames() {
return this.cacheNames;
}
......@@ -77,6 +70,18 @@ public class CacheProperties {
this.cacheNames = cacheNames;
}
public EhCache getEhcache() {
return this.ehcache;
}
public Hazelcast getHazelcast() {
return this.hazelcast;
}
public Infinispan getInfinispan() {
return this.infinispan;
}
public JCache getJcache() {
return this.jcache;
}
......@@ -91,20 +96,85 @@ public class CacheProperties {
* @throws IllegalArgumentException if the config attribute is set to a unknown
* location
*/
public Resource resolveConfigLocation() {
if (this.config != null) {
Assert.isTrue(this.config.exists(), "Cache configuration field defined by "
+ "'spring.cache.config' does not exist " + this.config);
return this.config;
public Resource resolveConfigLocation(Resource config) {
if (config != null) {
Assert.isTrue(config.exists(), "Cache configuration does not " +
"exist '" + config.getDescription() + "'");
return config;
}
return null;
}
/**
* EhCache specific cache properties.
*/
public static class EhCache {
/**
* The location of the configuration file to use to initialize EhCache.
*/
private Resource config;
public Resource getConfig() {
return config;
}
public void setConfig(Resource config) {
this.config = config;
}
}
/**
* Hazelcast specific cache properties.
*/
public static class Hazelcast {
/**
* The location of the configuration file to use to initialize Hazelcast.
*/
private Resource config;
public Resource getConfig() {
return config;
}
public void setConfig(Resource config) {
this.config = config;
}
}
/**
* Infinispan specific cache properties.
*/
public static class Infinispan {
/**
* The location of the configuration file to use to initialize Infinispan.
*/
private Resource config;
public Resource getConfig() {
return config;
}
public void setConfig(Resource config) {
this.config = config;
}
}
/**
* JCache (JSR-107) specific cache properties.
*/
public static class JCache {
/**
* The location of the configuration file to use to initialize the cache manager. The
* configuration file is dependent of the underlying cache implementation.
*/
private Resource config;
/**
* Fully qualified name of the CachingProvider implementation to use to retrieve
* the JSR-107 compliant cache manager. Only needed if more than one JSR-107
......@@ -120,6 +190,13 @@ public class CacheProperties {
this.provider = provider;
}
public Resource getConfig() {
return config;
}
public void setConfig(Resource config) {
this.config = config;
}
}
/**
......
......@@ -45,7 +45,7 @@ import org.springframework.core.io.Resource;
class EhCacheCacheConfiguration {
@Autowired
private CacheProperties properties;
private CacheProperties cacheProperties;
@Bean
public EhCacheCacheManager cacheManager(CacheManager ehCacheCacheManager) {
......@@ -55,7 +55,8 @@ class EhCacheCacheConfiguration {
@Bean
@ConditionalOnMissingBean
public CacheManager ehCacheCacheManager() {
Resource location = this.properties.resolveConfigLocation();
Resource location = this.cacheProperties.resolveConfigLocation(
this.cacheProperties.getEhcache().getConfig());
if (location != null) {
return EhCacheManagerUtils.buildCacheManager(location);
}
......@@ -70,7 +71,7 @@ class EhCacheCacheConfiguration {
static class ConfigAvailableCondition extends CacheConfigFileCondition {
public ConfigAvailableCondition() {
super("EhCache", "classpath:/ehcache.xml");
super("EhCache", "spring.config.ehcache", "classpath:/ehcache.xml");
}
}
......
......@@ -64,7 +64,8 @@ class HazelcastCacheConfiguration {
@Bean
@ConditionalOnMissingBean
public HazelcastInstance hazelcastInstance() throws IOException {
Resource location = this.cacheProperties.resolveConfigLocation();
Resource location = this.cacheProperties.resolveConfigLocation(
this.cacheProperties.getHazelcast().getConfig());
if (location != null) {
Config cfg = new XmlConfigBuilder(location.getURL()).build();
return Hazelcast.newHazelcastInstance(cfg);
......@@ -80,7 +81,8 @@ class HazelcastCacheConfiguration {
static class ConfigAvailableCondition extends CacheConfigFileCondition {
public ConfigAvailableCondition() {
super("Hazelcast", "file:./hazelcast.xml", "classpath:/hazelcast.xml");
super("Hazelcast", "spring.config.hazelcast",
"file:./hazelcast.xml", "classpath:/hazelcast.xml");
}
@Override
......
......@@ -73,9 +73,10 @@ public class InfinispanCacheConfiguration {
}
private EmbeddedCacheManager createEmbeddedCacheManager() throws IOException {
Resource location = this.cacheProperties.resolveConfigLocation();
Resource location = this.cacheProperties.resolveConfigLocation(
this.cacheProperties.getInfinispan().getConfig());
if (location != null) {
InputStream in = this.cacheProperties.getConfig().getInputStream();
InputStream in = location.getInputStream();
try {
return new DefaultCacheManager(in);
}
......
......@@ -91,7 +91,8 @@ class JCacheCacheConfiguration {
private CacheManager createCacheManager() throws IOException {
CachingProvider cachingProvider = getCachingProvider(this.cacheProperties
.getJcache().getProvider());
Resource configLocation = this.cacheProperties.resolveConfigLocation();
Resource configLocation = this.cacheProperties.resolveConfigLocation(
this.cacheProperties.getJcache().getConfig());
if (configLocation != null) {
return cachingProvider.getCacheManager(configLocation.getURI(),
cachingProvider.getDefaultClassLoader(),
......
......@@ -275,7 +275,7 @@ public class CacheAutoConfigurationTests {
String configLocation = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml";
load(JCacheCustomConfiguration.class, "spring.cache.type=jcache",
"spring.cache.jcache.provider=" + cachingProviderFqn,
"spring.cache.config=" + configLocation);
"spring.cache.jcache.config=" + configLocation);
JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class);
Resource configResource = new ClassPathResource(configLocation);
assertThat(cacheManager.getCacheManager().getURI(), is(configResource.getURI()));
......@@ -286,11 +286,11 @@ public class CacheAutoConfigurationTests {
String cachingProviderFqn = MockCachingProvider.class.getName();
String configLocation = "org/springframework/boot/autoconfigure/cache/does-not-exist.xml";
this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("spring.cache.config");
this.thrown.expectMessage("does not exist");
this.thrown.expectMessage(configLocation);
load(JCacheCustomConfiguration.class, "spring.cache.type=jcache",
"spring.cache.jcache.provider=" + cachingProviderFqn,
"spring.cache.config=" + configLocation);
"spring.cache.jcache.config=" + configLocation);
}
@Test
......@@ -307,7 +307,7 @@ public class CacheAutoConfigurationTests {
@Test
public void ehCacheCacheWithConfig() {
load(DefaultCacheConfiguration.class, "spring.cache.type=ehcache",
"spring.cache.config=cache/ehcache-override.xml");
"spring.cache.ehcache.config=cache/ehcache-override.xml");
EhCacheCacheManager cacheManager = validateCacheManager(EhCacheCacheManager.class);
assertThat(cacheManager.getCacheNames(),
containsInAnyOrder("cacheOverrideTest1", "cacheOverrideTest2"));
......@@ -336,7 +336,7 @@ public class CacheAutoConfigurationTests {
@Test
public void hazelcastCacheWithConfig() {
load(DefaultCacheConfiguration.class, "spring.cache.type=hazelcast",
"spring.cache.config=org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml");
"spring.cache.hazelcast.config=org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml");
HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class);
cacheManager.getCache("foobar");
assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foobar"));
......@@ -348,7 +348,7 @@ public class CacheAutoConfigurationTests {
this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("foo/bar/unknown.xml");
load(DefaultCacheConfiguration.class, "spring.cache.type=hazelcast",
"spring.cache.config=foo/bar/unknown.xml");
"spring.cache.hazelcast.config=foo/bar/unknown.xml");
}
@Test
......@@ -376,7 +376,7 @@ public class CacheAutoConfigurationTests {
String configLocation = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml";
load(DefaultCacheConfiguration.class, "spring.cache.type=jcache",
"spring.cache.jcache.provider=" + cachingProviderFqn,
"spring.cache.config=" + configLocation);
"spring.cache.jcache.config=" + configLocation);
JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class);
Resource configResource = new ClassPathResource(configLocation);
......@@ -387,7 +387,7 @@ public class CacheAutoConfigurationTests {
@Test
public void infinispanCacheWithConfig() {
load(DefaultCacheConfiguration.class, "spring.cache.type=infinispan",
"spring.cache.config=infinispan.xml");
"spring.cache.infinispan.config=infinispan.xml");
SpringEmbeddedCacheManager cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class);
assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar"));
}
......@@ -431,7 +431,7 @@ public class CacheAutoConfigurationTests {
String configLocation = "infinispan.xml";
load(DefaultCacheConfiguration.class, "spring.cache.type=jcache",
"spring.cache.jcache.provider=" + cachingProviderFqn,
"spring.cache.config=" + configLocation);
"spring.cache.jcache.config=" + configLocation);
JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class);
Resource configResource = new ClassPathResource(configLocation);
......
......@@ -495,9 +495,12 @@ content into your application; rather pick only the properties that you need.
spring.batch.table-prefix= # table prefix for all the batch meta-data tables
# SPRING CACHE ({sc-spring-boot-autoconfigure}/cache/CacheProperties.{sc-ext}[CacheProperties])
spring.cache.type= # generic, ehcache, hazelcast, jcache, redis, guava, simple, none
spring.cache.config= #
spring.cache.type= # generic, ehcache, hazelcast, infinispan, jcache, redis, guava, simple, none
spring.cache.cache-names= # cache names to create on startup
spring.cache.ehcache.config= # location of the ehcache configuration
spring.cache.hazelcast.config= # location of the hazelcast configuration
spring.cache.infinispan.config= # location of the infinispan configuration
spring.cache.jcache.config= # location of jcache configuration
spring.cache.jcache.provider= # fully qualified name of the CachingProvider implementation to use
spring.cache.guava.spec= # link:http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/CacheBuilderSpec.html[guava specs]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment