From 1457a55e41eb8f3add2907b70dfdaee0d08d941e Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 28 May 2015 18:15:35 +0200 Subject: [PATCH] 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 --- .../cache/CacheConfigFileCondition.java | 11 +- .../autoconfigure/cache/CacheProperties.java | 113 +++++++++++++++--- .../cache/EhCacheCacheConfiguration.java | 7 +- .../cache/HazelcastCacheConfiguration.java | 6 +- .../cache/InfinispanCacheConfiguration.java | 5 +- .../cache/JCacheCacheConfiguration.java | 3 +- .../cache/CacheAutoConfigurationTests.java | 18 +-- .../appendix-application-properties.adoc | 7 +- 8 files changed, 130 insertions(+), 40 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheConfigFileCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheConfigFileCondition.java index aa03c8cbbe0..bdeb69a8de8 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheConfigFileCondition.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheConfigFileCondition.java @@ -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); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheProperties.java index 6e1ce5dfa1c..dcccf644f1d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheProperties.java @@ -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 cacheNames = new ArrayList(); + 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 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; + } } /** diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.java index fdc3e475bb5..8194e7f939a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.java @@ -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"); } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java index 573190c908f..65547f26de0 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java @@ -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 diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/InfinispanCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/InfinispanCacheConfiguration.java index 2ffe80d23b2..341dd3bf62b 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/InfinispanCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/InfinispanCacheConfiguration.java @@ -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); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java index 5ecc03e968e..1bb627fcd12 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java @@ -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(), diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index 6c656f24267..d2e03c5bbf9 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -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); diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index f2901fd6172..78210fd2ec3 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -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]