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 { ...@@ -34,10 +34,14 @@ abstract class CacheConfigFileCondition extends SpringBootCondition {
private final String name; private final String name;
private final String configPrefix;
private final String[] resourceLocations; private final String[] resourceLocations;
public CacheConfigFileCondition(String name, String... resourceLocations) { public CacheConfigFileCondition(String name, String configPrefix,
String... resourceLocations) {
this.name = name; this.name = name;
this.configPrefix = configPrefix;
this.resourceLocations = resourceLocations; this.resourceLocations = resourceLocations;
} }
...@@ -45,9 +49,10 @@ abstract class CacheConfigFileCondition extends SpringBootCondition { ...@@ -45,9 +49,10 @@ abstract class CacheConfigFileCondition extends SpringBootCondition {
public ConditionOutcome getMatchOutcome(ConditionContext context, public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) { AnnotatedTypeMetadata metadata) {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
context.getEnvironment(), "spring.cache."); context.getEnvironment(), this.configPrefix);
if (resolver.containsProperty("config")) { 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); return getResourceOutcome(context, metadata);
} }
......
...@@ -38,17 +38,18 @@ public class CacheProperties { ...@@ -38,17 +38,18 @@ public class CacheProperties {
*/ */
private CacheType type; 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 * 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. * manager. Usually, this disables the ability to create additional caches on-the-fly.
*/ */
private List<String> cacheNames = new ArrayList<String>(); 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 JCache jcache = new JCache();
private final Guava guava = new Guava(); private final Guava guava = new Guava();
...@@ -61,14 +62,6 @@ public class CacheProperties { ...@@ -61,14 +62,6 @@ public class CacheProperties {
this.type = mode; this.type = mode;
} }
public Resource getConfig() {
return this.config;
}
public void setConfig(Resource config) {
this.config = config;
}
public List<String> getCacheNames() { public List<String> getCacheNames() {
return this.cacheNames; return this.cacheNames;
} }
...@@ -77,6 +70,18 @@ public class CacheProperties { ...@@ -77,6 +70,18 @@ public class CacheProperties {
this.cacheNames = cacheNames; this.cacheNames = cacheNames;
} }
public EhCache getEhcache() {
return this.ehcache;
}
public Hazelcast getHazelcast() {
return this.hazelcast;
}
public Infinispan getInfinispan() {
return this.infinispan;
}
public JCache getJcache() { public JCache getJcache() {
return this.jcache; return this.jcache;
} }
...@@ -91,20 +96,85 @@ public class CacheProperties { ...@@ -91,20 +96,85 @@ public class CacheProperties {
* @throws IllegalArgumentException if the config attribute is set to a unknown * @throws IllegalArgumentException if the config attribute is set to a unknown
* location * location
*/ */
public Resource resolveConfigLocation() { public Resource resolveConfigLocation(Resource config) {
if (this.config != null) { if (config != null) {
Assert.isTrue(this.config.exists(), "Cache configuration field defined by " Assert.isTrue(config.exists(), "Cache configuration does not " +
+ "'spring.cache.config' does not exist " + this.config); "exist '" + config.getDescription() + "'");
return this.config; return config;
} }
return null; 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. * JCache (JSR-107) specific cache properties.
*/ */
public static class JCache { 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 * 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 * the JSR-107 compliant cache manager. Only needed if more than one JSR-107
...@@ -120,6 +190,13 @@ public class CacheProperties { ...@@ -120,6 +190,13 @@ public class CacheProperties {
this.provider = provider; 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; ...@@ -45,7 +45,7 @@ import org.springframework.core.io.Resource;
class EhCacheCacheConfiguration { class EhCacheCacheConfiguration {
@Autowired @Autowired
private CacheProperties properties; private CacheProperties cacheProperties;
@Bean @Bean
public EhCacheCacheManager cacheManager(CacheManager ehCacheCacheManager) { public EhCacheCacheManager cacheManager(CacheManager ehCacheCacheManager) {
...@@ -55,7 +55,8 @@ class EhCacheCacheConfiguration { ...@@ -55,7 +55,8 @@ class EhCacheCacheConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public CacheManager ehCacheCacheManager() { public CacheManager ehCacheCacheManager() {
Resource location = this.properties.resolveConfigLocation(); Resource location = this.cacheProperties.resolveConfigLocation(
this.cacheProperties.getEhcache().getConfig());
if (location != null) { if (location != null) {
return EhCacheManagerUtils.buildCacheManager(location); return EhCacheManagerUtils.buildCacheManager(location);
} }
...@@ -70,7 +71,7 @@ class EhCacheCacheConfiguration { ...@@ -70,7 +71,7 @@ class EhCacheCacheConfiguration {
static class ConfigAvailableCondition extends CacheConfigFileCondition { static class ConfigAvailableCondition extends CacheConfigFileCondition {
public ConfigAvailableCondition() { public ConfigAvailableCondition() {
super("EhCache", "classpath:/ehcache.xml"); super("EhCache", "spring.config.ehcache", "classpath:/ehcache.xml");
} }
} }
......
...@@ -64,7 +64,8 @@ class HazelcastCacheConfiguration { ...@@ -64,7 +64,8 @@ class HazelcastCacheConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public HazelcastInstance hazelcastInstance() throws IOException { public HazelcastInstance hazelcastInstance() throws IOException {
Resource location = this.cacheProperties.resolveConfigLocation(); Resource location = this.cacheProperties.resolveConfigLocation(
this.cacheProperties.getHazelcast().getConfig());
if (location != null) { if (location != null) {
Config cfg = new XmlConfigBuilder(location.getURL()).build(); Config cfg = new XmlConfigBuilder(location.getURL()).build();
return Hazelcast.newHazelcastInstance(cfg); return Hazelcast.newHazelcastInstance(cfg);
...@@ -80,7 +81,8 @@ class HazelcastCacheConfiguration { ...@@ -80,7 +81,8 @@ class HazelcastCacheConfiguration {
static class ConfigAvailableCondition extends CacheConfigFileCondition { static class ConfigAvailableCondition extends CacheConfigFileCondition {
public ConfigAvailableCondition() { public ConfigAvailableCondition() {
super("Hazelcast", "file:./hazelcast.xml", "classpath:/hazelcast.xml"); super("Hazelcast", "spring.config.hazelcast",
"file:./hazelcast.xml", "classpath:/hazelcast.xml");
} }
@Override @Override
......
...@@ -73,9 +73,10 @@ public class InfinispanCacheConfiguration { ...@@ -73,9 +73,10 @@ public class InfinispanCacheConfiguration {
} }
private EmbeddedCacheManager createEmbeddedCacheManager() throws IOException { private EmbeddedCacheManager createEmbeddedCacheManager() throws IOException {
Resource location = this.cacheProperties.resolveConfigLocation(); Resource location = this.cacheProperties.resolveConfigLocation(
this.cacheProperties.getInfinispan().getConfig());
if (location != null) { if (location != null) {
InputStream in = this.cacheProperties.getConfig().getInputStream(); InputStream in = location.getInputStream();
try { try {
return new DefaultCacheManager(in); return new DefaultCacheManager(in);
} }
......
...@@ -91,7 +91,8 @@ class JCacheCacheConfiguration { ...@@ -91,7 +91,8 @@ class JCacheCacheConfiguration {
private CacheManager createCacheManager() throws IOException { private CacheManager createCacheManager() throws IOException {
CachingProvider cachingProvider = getCachingProvider(this.cacheProperties CachingProvider cachingProvider = getCachingProvider(this.cacheProperties
.getJcache().getProvider()); .getJcache().getProvider());
Resource configLocation = this.cacheProperties.resolveConfigLocation(); Resource configLocation = this.cacheProperties.resolveConfigLocation(
this.cacheProperties.getJcache().getConfig());
if (configLocation != null) { if (configLocation != null) {
return cachingProvider.getCacheManager(configLocation.getURI(), return cachingProvider.getCacheManager(configLocation.getURI(),
cachingProvider.getDefaultClassLoader(), cachingProvider.getDefaultClassLoader(),
......
...@@ -275,7 +275,7 @@ public class CacheAutoConfigurationTests { ...@@ -275,7 +275,7 @@ public class CacheAutoConfigurationTests {
String configLocation = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"; String configLocation = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml";
load(JCacheCustomConfiguration.class, "spring.cache.type=jcache", load(JCacheCustomConfiguration.class, "spring.cache.type=jcache",
"spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.jcache.provider=" + cachingProviderFqn,
"spring.cache.config=" + configLocation); "spring.cache.jcache.config=" + configLocation);
JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class); JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class);
Resource configResource = new ClassPathResource(configLocation); Resource configResource = new ClassPathResource(configLocation);
assertThat(cacheManager.getCacheManager().getURI(), is(configResource.getURI())); assertThat(cacheManager.getCacheManager().getURI(), is(configResource.getURI()));
...@@ -286,11 +286,11 @@ public class CacheAutoConfigurationTests { ...@@ -286,11 +286,11 @@ public class CacheAutoConfigurationTests {
String cachingProviderFqn = MockCachingProvider.class.getName(); String cachingProviderFqn = MockCachingProvider.class.getName();
String configLocation = "org/springframework/boot/autoconfigure/cache/does-not-exist.xml"; String configLocation = "org/springframework/boot/autoconfigure/cache/does-not-exist.xml";
this.thrown.expect(BeanCreationException.class); this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("spring.cache.config"); this.thrown.expectMessage("does not exist");
this.thrown.expectMessage(configLocation); this.thrown.expectMessage(configLocation);
load(JCacheCustomConfiguration.class, "spring.cache.type=jcache", load(JCacheCustomConfiguration.class, "spring.cache.type=jcache",
"spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.jcache.provider=" + cachingProviderFqn,
"spring.cache.config=" + configLocation); "spring.cache.jcache.config=" + configLocation);
} }
@Test @Test
...@@ -307,7 +307,7 @@ public class CacheAutoConfigurationTests { ...@@ -307,7 +307,7 @@ public class CacheAutoConfigurationTests {
@Test @Test
public void ehCacheCacheWithConfig() { public void ehCacheCacheWithConfig() {
load(DefaultCacheConfiguration.class, "spring.cache.type=ehcache", 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); EhCacheCacheManager cacheManager = validateCacheManager(EhCacheCacheManager.class);
assertThat(cacheManager.getCacheNames(), assertThat(cacheManager.getCacheNames(),
containsInAnyOrder("cacheOverrideTest1", "cacheOverrideTest2")); containsInAnyOrder("cacheOverrideTest1", "cacheOverrideTest2"));
...@@ -336,7 +336,7 @@ public class CacheAutoConfigurationTests { ...@@ -336,7 +336,7 @@ public class CacheAutoConfigurationTests {
@Test @Test
public void hazelcastCacheWithConfig() { public void hazelcastCacheWithConfig() {
load(DefaultCacheConfiguration.class, "spring.cache.type=hazelcast", 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); HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class);
cacheManager.getCache("foobar"); cacheManager.getCache("foobar");
assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foobar")); assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foobar"));
...@@ -348,7 +348,7 @@ public class CacheAutoConfigurationTests { ...@@ -348,7 +348,7 @@ public class CacheAutoConfigurationTests {
this.thrown.expect(BeanCreationException.class); this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("foo/bar/unknown.xml"); this.thrown.expectMessage("foo/bar/unknown.xml");
load(DefaultCacheConfiguration.class, "spring.cache.type=hazelcast", load(DefaultCacheConfiguration.class, "spring.cache.type=hazelcast",
"spring.cache.config=foo/bar/unknown.xml"); "spring.cache.hazelcast.config=foo/bar/unknown.xml");
} }
@Test @Test
...@@ -376,7 +376,7 @@ public class CacheAutoConfigurationTests { ...@@ -376,7 +376,7 @@ public class CacheAutoConfigurationTests {
String configLocation = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"; String configLocation = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml";
load(DefaultCacheConfiguration.class, "spring.cache.type=jcache", load(DefaultCacheConfiguration.class, "spring.cache.type=jcache",
"spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.jcache.provider=" + cachingProviderFqn,
"spring.cache.config=" + configLocation); "spring.cache.jcache.config=" + configLocation);
JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class); JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class);
Resource configResource = new ClassPathResource(configLocation); Resource configResource = new ClassPathResource(configLocation);
...@@ -387,7 +387,7 @@ public class CacheAutoConfigurationTests { ...@@ -387,7 +387,7 @@ public class CacheAutoConfigurationTests {
@Test @Test
public void infinispanCacheWithConfig() { public void infinispanCacheWithConfig() {
load(DefaultCacheConfiguration.class, "spring.cache.type=infinispan", load(DefaultCacheConfiguration.class, "spring.cache.type=infinispan",
"spring.cache.config=infinispan.xml"); "spring.cache.infinispan.config=infinispan.xml");
SpringEmbeddedCacheManager cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class); SpringEmbeddedCacheManager cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class);
assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar")); assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar"));
} }
...@@ -431,7 +431,7 @@ public class CacheAutoConfigurationTests { ...@@ -431,7 +431,7 @@ public class CacheAutoConfigurationTests {
String configLocation = "infinispan.xml"; String configLocation = "infinispan.xml";
load(DefaultCacheConfiguration.class, "spring.cache.type=jcache", load(DefaultCacheConfiguration.class, "spring.cache.type=jcache",
"spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.jcache.provider=" + cachingProviderFqn,
"spring.cache.config=" + configLocation); "spring.cache.jcache.config=" + configLocation);
JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class); JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class);
Resource configResource = new ClassPathResource(configLocation); Resource configResource = new ClassPathResource(configLocation);
......
...@@ -495,9 +495,12 @@ content into your application; rather pick only the properties that you need. ...@@ -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.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 ({sc-spring-boot-autoconfigure}/cache/CacheProperties.{sc-ext}[CacheProperties])
spring.cache.type= # generic, ehcache, hazelcast, jcache, redis, guava, simple, none spring.cache.type= # generic, ehcache, hazelcast, infinispan, jcache, redis, guava, simple, none
spring.cache.config= #
spring.cache.cache-names= # cache names to create on startup 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.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] 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