From d2f11c465e50f90a87678476e14bdec04a00afb2 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 4 Jun 2015 00:51:20 -0700 Subject: [PATCH] Polish cache code --- .../CacheStatisticsAutoConfiguration.java | 9 ++-- .../AbstractJmxCacheStatisticsProvider.java | 42 +++++++++---------- .../actuate/cache/DefaultCacheStatistics.java | 1 + .../InfinispanCacheStatisticsProvider.java | 39 ++++++++++------- .../cache/JCacheCacheStatisticsProvider.java | 22 +++++----- ...CacheStatisticsAutoConfigurationTests.java | 17 ++++---- .../cache/CacheConfigFileCondition.java | 4 +- .../autoconfigure/cache/CacheProperties.java | 19 +++++---- .../cache/EhCacheCacheConfiguration.java | 4 +- .../cache/GuavaCacheConfiguration.java | 2 +- .../cache/HazelcastCacheConfiguration.java | 8 ++-- .../cache/InfinispanCacheConfiguration.java | 14 +++---- .../cache/JCacheCacheConfiguration.java | 14 ++++--- .../cache/CacheAutoConfigurationTests.java | 37 +++++++++------- .../spring-boot-sample-cache/README.adoc | 26 ++++++++---- .../spring-boot-sample-cache/pom.xml | 16 ++++--- .../main/java/sample/CacheManagerCheck.java | 42 +++++++++++++++++++ .../src/main/java/sample/Country.java | 14 ++++--- .../java/sample/SampleCacheApplication.java | 31 ++------------ .../src/main/java/sample/SampleClient.java | 40 ++++++++++-------- .../src/main/resources/application.properties | 4 +- .../sample/SampleCacheApplicationTests.java | 4 +- 22 files changed, 234 insertions(+), 175 deletions(-) create mode 100644 spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/CacheManagerCheck.java diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfiguration.java index b985f6ff582..6bd5d584916 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfiguration.java @@ -18,11 +18,9 @@ package org.springframework.boot.actuate.autoconfigure; import javax.cache.Caching; -import com.hazelcast.core.IMap; -import com.hazelcast.spring.cache.HazelcastCache; import net.sf.ehcache.Ehcache; -import org.infinispan.spring.provider.SpringCache; +import org.infinispan.spring.provider.SpringCache; import org.springframework.boot.actuate.cache.CacheStatistics; import org.springframework.boot.actuate.cache.CacheStatisticsProvider; import org.springframework.boot.actuate.cache.ConcurrentMapCacheStatisticsProvider; @@ -45,6 +43,9 @@ import org.springframework.cache.support.NoOpCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import com.hazelcast.core.IMap; +import com.hazelcast.spring.cache.HazelcastCache; + /** * {@link EnableAutoConfiguration Auto-configuration} for {@link CacheStatisticsProvider} * beans. @@ -81,7 +82,7 @@ public class CacheStatisticsAutoConfiguration { } @Configuration - @ConditionalOnClass({ IMap.class, HazelcastCache.class} ) + @ConditionalOnClass({ IMap.class, HazelcastCache.class }) static class HazelcastCacheStatisticsConfiguration { @Bean diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/AbstractJmxCacheStatisticsProvider.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/AbstractJmxCacheStatisticsProvider.java index d7f6668194e..3ba8ec208ea 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/AbstractJmxCacheStatisticsProvider.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/AbstractJmxCacheStatisticsProvider.java @@ -19,6 +19,7 @@ package org.springframework.boot.actuate.cache; import java.lang.management.ManagementFactory; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; + import javax.management.AttributeNotFoundException; import javax.management.InstanceNotFoundException; import javax.management.MBeanException; @@ -29,37 +30,32 @@ import javax.management.ReflectionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; /** - * Base {@link CacheStatisticsProvider} implementation that uses JMX to - * retrieve the cache statistics. + * Base {@link CacheStatisticsProvider} implementation that uses JMX to retrieve the cache + * statistics. * * @author Stephane Nicoll * @since 1.3.0 + * @param The cache type */ -public abstract class AbstractJmxCacheStatisticsProvider - implements CacheStatisticsProvider { +public abstract class AbstractJmxCacheStatisticsProvider implements + CacheStatisticsProvider { private static final Logger logger = LoggerFactory .getLogger(AbstractJmxCacheStatisticsProvider.class); private MBeanServer mBeanServer; - private Map caches = - new ConcurrentHashMap(); - + private Map caches = new ConcurrentHashMap(); @Override public CacheStatistics getCacheStatistics(CacheManager cacheManager, C cache) { try { ObjectName objectName = internalGetObjectName(cache); - if (objectName != null) { - return getCacheStatistics(objectName); - } - return null; + return (objectName == null ? null : getCacheStatistics(objectName)); } catch (MalformedObjectNameException ex) { throw new IllegalStateException(ex); @@ -71,20 +67,20 @@ public abstract class AbstractJmxCacheStatisticsProvider * {@code null} if none is found. * @param cache the cache to handle * @return the object name of the cache statistics MBean + * @throws MalformedObjectNameException */ - protected abstract ObjectName getObjectName(C cache) throws MalformedObjectNameException; + protected abstract ObjectName getObjectName(C cache) + throws MalformedObjectNameException; /** - * Return the current {@link CacheStatistics} snapshot from the MBean identified by the - * specified {@link ObjectName}. + * Return the current {@link CacheStatistics} snapshot from the MBean identified by + * the specified {@link ObjectName}. * @param objectName the object name of the cache statistics MBean * @return the current cache statistics */ protected abstract CacheStatistics getCacheStatistics(ObjectName objectName); - - private ObjectName internalGetObjectName(C cache) - throws MalformedObjectNameException { + private ObjectName internalGetObjectName(C cache) throws MalformedObjectNameException { String cacheName = cache.getName(); ObjectNameWrapper value = this.caches.get(cacheName); if (value != null) { @@ -102,7 +98,8 @@ public abstract class AbstractJmxCacheStatisticsProvider return this.mBeanServer; } - protected T getAttribute(ObjectName objectName, String attributeName, Class type) { + protected T getAttribute(ObjectName objectName, String attributeName, + Class type) { try { Object attribute = getMBeanServer().getAttribute(objectName, attributeName); return type.cast(attribute); @@ -111,8 +108,8 @@ public abstract class AbstractJmxCacheStatisticsProvider throw new IllegalStateException(ex); } catch (AttributeNotFoundException ex) { - throw new IllegalStateException("Unexpected: MBean with name '" + objectName + "' " + - "does not expose attribute with name " + attributeName, ex); + throw new IllegalStateException("Unexpected: MBean with name '" + objectName + + "' " + "does not expose attribute with name " + attributeName, ex); } catch (ReflectionException ex) { throw new IllegalStateException(ex); @@ -123,13 +120,14 @@ public abstract class AbstractJmxCacheStatisticsProvider } } - private static class ObjectNameWrapper { + private final ObjectName objectName; public ObjectNameWrapper(ObjectName objectName) { this.objectName = objectName; } + } } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/DefaultCacheStatistics.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/DefaultCacheStatistics.java index d080a0cac97..d676e3690be 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/DefaultCacheStatistics.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/DefaultCacheStatistics.java @@ -86,4 +86,5 @@ public class DefaultCacheStatistics implements CacheStatistics { metrics.add(new Metric(name, value)); } } + } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/InfinispanCacheStatisticsProvider.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/InfinispanCacheStatisticsProvider.java index 9f4e60cef35..ba4f795bc5c 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/InfinispanCacheStatisticsProvider.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/InfinispanCacheStatisticsProvider.java @@ -17,6 +17,7 @@ package org.springframework.boot.actuate.cache; import java.util.Set; + import javax.management.MalformedObjectNameException; import javax.management.ObjectInstance; import javax.management.ObjectName; @@ -29,35 +30,43 @@ import org.infinispan.spring.provider.SpringCache; * @author Stephane Nicoll * @since 1.3.0 */ -public class InfinispanCacheStatisticsProvider extends AbstractJmxCacheStatisticsProvider { +public class InfinispanCacheStatisticsProvider extends + AbstractJmxCacheStatisticsProvider { @Override - protected ObjectName getObjectName(SpringCache cache) throws MalformedObjectNameException { - Set instances = getMBeanServer().queryMBeans( - new ObjectName("org.infinispan:component=Statistics,type=Cache," + - "name=\"" + cache.getName() + "(local)\",*"), null); + protected ObjectName getObjectName(SpringCache cache) + throws MalformedObjectNameException { + ObjectName name = new ObjectName( + "org.infinispan:component=Statistics,type=Cache,name=\"" + + cache.getName() + "(local)\",*"); + Set instances = getMBeanServer().queryMBeans(name, null); if (instances.size() == 1) { return instances.iterator().next().getObjectName(); } - return null; // None or more than one + // None or more than one + return null; } + @Override protected CacheStatistics getCacheStatistics(ObjectName objectName) { DefaultCacheStatistics statistics = new DefaultCacheStatistics(); - Integer size = getAttribute(objectName, "numberOfEntries", - Integer.class); + Integer size = getAttribute(objectName, "numberOfEntries", Integer.class); if (size != null) { statistics.setSize((long) size); - if (size > 0) { // Let's initialize the stats if we have some data - Double hitRatio = getAttribute(objectName, "hitRatio", - Double.class); - if ((hitRatio != null)) { - statistics.setHitRatio(hitRatio); - statistics.setMissRatio(1 - hitRatio); - } + if (size > 0) { + // Let's initialize the stats if we have some data + initalizeStats(objectName, statistics); } } return statistics; } + private void initalizeStats(ObjectName objectName, DefaultCacheStatistics statistics) { + Double hitRatio = getAttribute(objectName, "hitRatio", Double.class); + if ((hitRatio != null)) { + statistics.setHitRatio(hitRatio); + statistics.setMissRatio(1 - hitRatio); + } + } + } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/JCacheCacheStatisticsProvider.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/JCacheCacheStatisticsProvider.java index dce29a125ea..a9e60a5df9d 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/JCacheCacheStatisticsProvider.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/JCacheCacheStatisticsProvider.java @@ -17,6 +17,7 @@ package org.springframework.boot.actuate.cache; import java.util.Set; + import javax.management.MalformedObjectNameException; import javax.management.ObjectInstance; import javax.management.ObjectName; @@ -29,27 +30,28 @@ import org.springframework.cache.jcache.JCacheCache; * @author Stephane Nicoll * @since 1.3.0 */ -public class JCacheCacheStatisticsProvider extends AbstractJmxCacheStatisticsProvider { - +public class JCacheCacheStatisticsProvider extends + AbstractJmxCacheStatisticsProvider { + @Override protected ObjectName getObjectName(JCacheCache cache) throws MalformedObjectNameException { - - Set instances = getMBeanServer().queryMBeans( - new ObjectName("javax.cache:type=CacheStatistics,Cache=" - + cache.getName() + ",*"), null); + ObjectName name = new ObjectName("javax.cache:type=CacheStatistics,Cache=" + + cache.getName() + ",*"); + Set instances = getMBeanServer().queryMBeans(name, null); if (instances.size() == 1) { return instances.iterator().next().getObjectName(); } - return null; // None or more than one + // None or more than one + return null; } + @Override protected CacheStatistics getCacheStatistics(ObjectName objectName) { DefaultCacheStatistics statistics = new DefaultCacheStatistics(); - Float hitPercentage = getAttribute(objectName, "CacheHitPercentage", + Float hitPercentage = getAttribute(objectName, "CacheHitPercentage", Float.class); + Float missPercentage = getAttribute(objectName, "CacheMissPercentage", Float.class); - Float missPercentage = getAttribute(objectName, - "CacheMissPercentage", Float.class); if ((hitPercentage != null && missPercentage != null) && (hitPercentage > 0 || missPercentage > 0)) { statistics.setHitRatio(hitPercentage / (double) 100); diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfigurationTests.java index cec8aa3a478..04b7da0fd0b 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfigurationTests.java @@ -19,22 +19,15 @@ package org.springframework.boot.actuate.autoconfigure; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; + import javax.cache.Caching; import javax.cache.configuration.MutableConfiguration; -import com.google.common.cache.CacheBuilder; -import com.hazelcast.cache.HazelcastCachingProvider; -import com.hazelcast.config.Config; -import com.hazelcast.config.XmlConfigBuilder; -import com.hazelcast.core.Hazelcast; -import com.hazelcast.core.HazelcastInstance; -import com.hazelcast.spring.cache.HazelcastCacheManager; import org.infinispan.manager.DefaultCacheManager; import org.infinispan.manager.EmbeddedCacheManager; import org.infinispan.spring.provider.SpringEmbeddedCacheManager; import org.junit.After; import org.junit.Test; - import org.springframework.boot.actuate.cache.CacheStatistics; import org.springframework.boot.actuate.cache.CacheStatisticsProvider; import org.springframework.cache.Cache; @@ -52,6 +45,14 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.util.Assert; +import com.google.common.cache.CacheBuilder; +import com.hazelcast.cache.HazelcastCachingProvider; +import com.hazelcast.config.Config; +import com.hazelcast.config.XmlConfigBuilder; +import com.hazelcast.core.Hazelcast; +import com.hazelcast.core.HazelcastInstance; +import com.hazelcast.spring.cache.HazelcastCacheManager; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; 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 bdeb69a8de8..23d2504f880 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 @@ -51,8 +51,8 @@ abstract class CacheConfigFileCondition extends SpringBootCondition { RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( context.getEnvironment(), this.configPrefix); if (resolver.containsProperty("config")) { - return ConditionOutcome.match("A '" + this.configPrefix + ".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 dcccf644f1d..729cb1968b7 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 @@ -92,14 +92,15 @@ public class CacheProperties { /** * Resolve the config location if set. + * @param config the config resource * @return the location or {@code null} if it is not set * @throws IllegalArgumentException if the config attribute is set to a unknown * location */ public Resource resolveConfigLocation(Resource config) { if (config != null) { - Assert.isTrue(config.exists(), "Cache configuration does not " + - "exist '" + config.getDescription() + "'"); + Assert.isTrue(config.exists(), "Cache configuration does not " + "exist '" + + config.getDescription() + "'"); return config; } return null; @@ -116,12 +117,13 @@ public class CacheProperties { private Resource config; public Resource getConfig() { - return config; + return this.config; } public void setConfig(Resource config) { this.config = config; } + } /** @@ -135,7 +137,7 @@ public class CacheProperties { private Resource config; public Resource getConfig() { - return config; + return this.config; } public void setConfig(Resource config) { @@ -155,7 +157,7 @@ public class CacheProperties { private Resource config; public Resource getConfig() { - return config; + return this.config; } public void setConfig(Resource config) { @@ -170,8 +172,8 @@ public class CacheProperties { 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. + * 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; @@ -191,12 +193,13 @@ public class CacheProperties { } public Resource getConfig() { - return config; + return this.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 8194e7f939a..4f0a0ea621d 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 @@ -55,8 +55,8 @@ class EhCacheCacheConfiguration { @Bean @ConditionalOnMissingBean public CacheManager ehCacheCacheManager() { - Resource location = this.cacheProperties.resolveConfigLocation( - this.cacheProperties.getEhcache().getConfig()); + Resource location = this.cacheProperties + .resolveConfigLocation(this.cacheProperties.getEhcache().getConfig()); if (location != null) { return EhCacheManagerUtils.buildCacheManager(location); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/GuavaCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/GuavaCacheConfiguration.java index 3ae25e8f19c..fde991e1933 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/GuavaCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/GuavaCacheConfiguration.java @@ -40,7 +40,7 @@ import com.google.common.cache.CacheLoader; * @since 1.3.0 */ @Configuration -@ConditionalOnClass({CacheBuilder.class,GuavaCacheManager.class}) +@ConditionalOnClass({ CacheBuilder.class, GuavaCacheManager.class }) @ConditionalOnMissingBean(CacheManager.class) @Conditional(CacheCondition.class) class GuavaCacheConfiguration { 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 3be5a6dd8ef..427ebaa0211 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,8 +64,8 @@ class HazelcastCacheConfiguration { @Bean @ConditionalOnMissingBean public HazelcastInstance hazelcastInstance() throws IOException { - Resource location = this.cacheProperties.resolveConfigLocation( - this.cacheProperties.getHazelcast().getConfig()); + Resource location = this.cacheProperties + .resolveConfigLocation(this.cacheProperties.getHazelcast().getConfig()); if (location != null) { Config cfg = new XmlConfigBuilder(location.getURL()).build(); return Hazelcast.newHazelcastInstance(cfg); @@ -81,8 +81,8 @@ class HazelcastCacheConfiguration { static class ConfigAvailableCondition extends CacheConfigFileCondition { public ConfigAvailableCondition() { - super("Hazelcast", "spring.config.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 341dd3bf62b..0a639fd0376 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 @@ -24,7 +24,6 @@ import org.infinispan.configuration.cache.ConfigurationBuilder; import org.infinispan.manager.DefaultCacheManager; import org.infinispan.manager.EmbeddedCacheManager; import org.infinispan.spring.provider.SpringEmbeddedCacheManager; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -62,19 +61,20 @@ public class InfinispanCacheConfiguration { @Bean(destroyMethod = "stop") @ConditionalOnMissingBean public EmbeddedCacheManager infinispanCacheManager() throws IOException { - EmbeddedCacheManager infinispanCacheManager = createEmbeddedCacheManager(); + EmbeddedCacheManager cacheManager = createEmbeddedCacheManager(); List cacheNames = this.cacheProperties.getCacheNames(); if (!CollectionUtils.isEmpty(cacheNames)) { for (String cacheName : cacheNames) { - infinispanCacheManager.defineConfiguration(cacheName, getDefaultCacheConfiguration()); + cacheManager.defineConfiguration(cacheName, + getDefaultCacheConfiguration()); } } - return infinispanCacheManager; + return cacheManager; } private EmbeddedCacheManager createEmbeddedCacheManager() throws IOException { - Resource location = this.cacheProperties.resolveConfigLocation( - this.cacheProperties.getInfinispan().getConfig()); + Resource location = this.cacheProperties + .resolveConfigLocation(this.cacheProperties.getInfinispan().getConfig()); if (location != null) { InputStream in = location.getInputStream(); try { @@ -89,7 +89,7 @@ public class InfinispanCacheConfiguration { private org.infinispan.configuration.cache.Configuration getDefaultCacheConfiguration() { if (this.defaultConfigurationBuilder != null) { - return defaultConfigurationBuilder.build(); + return this.defaultConfigurationBuilder.build(); } return new ConfigurationBuilder().build(); } 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 e28df36b65e..56e20893fd9 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,8 +91,8 @@ class JCacheCacheConfiguration { private CacheManager createCacheManager() throws IOException { CachingProvider cachingProvider = getCachingProvider(this.cacheProperties .getJcache().getProvider()); - Resource configLocation = this.cacheProperties.resolveConfigLocation( - this.cacheProperties.getJcache().getConfig()); + Resource configLocation = this.cacheProperties + .resolveConfigLocation(this.cacheProperties.getJcache().getConfig()); if (configLocation != null) { return cachingProvider.getCacheManager(configLocation.getURI(), cachingProvider.getDefaultClassLoader(), @@ -135,8 +135,8 @@ class JCacheCacheConfiguration { /** * Determine if JCache is available. This either kicks in if a provider is available - * as defined per {@link JCacheProviderAvailableCondition} or if a {@link CacheManager} - * has already been defined. + * as defined per {@link JCacheProviderAvailableCondition} or if a + * {@link CacheManager} has already been defined. */ @Order(Ordered.LOWEST_PRECEDENCE) static class JCacheAvailableCondition extends AnyNestedCondition { @@ -146,10 +146,12 @@ class JCacheCacheConfiguration { } @Conditional(JCacheProviderAvailableCondition.class) - static class JCacheProvider {} + static class JCacheProvider { + } @ConditionalOnSingleCandidate(CacheManager.class) - static class CustomJCacheCacheManager {} + static class CustomJCacheCacheManager { + } } 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 885e7df3a82..6b6f6f0b2d5 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 @@ -19,16 +19,14 @@ package org.springframework.boot.autoconfigure.cache; import java.io.IOException; import java.util.Collection; import java.util.Collections; + import javax.cache.configuration.CompleteConfiguration; import javax.cache.configuration.MutableConfiguration; import javax.cache.expiry.CreatedExpiryPolicy; import javax.cache.expiry.Duration; -import com.google.common.cache.CacheBuilder; -import com.hazelcast.cache.HazelcastCachingProvider; -import com.hazelcast.core.HazelcastInstance; -import com.hazelcast.spring.cache.HazelcastCacheManager; import net.sf.ehcache.Status; + import org.infinispan.configuration.cache.ConfigurationBuilder; import org.infinispan.jcache.embedded.JCachingProvider; import org.infinispan.spring.provider.SpringEmbeddedCacheManager; @@ -36,7 +34,6 @@ import org.junit.After; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; - import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; @@ -65,6 +62,11 @@ import org.springframework.core.io.Resource; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.core.RedisTemplate; +import com.google.common.cache.CacheBuilder; +import com.hazelcast.cache.HazelcastCachingProvider; +import com.hazelcast.core.HazelcastInstance; +import com.hazelcast.spring.cache.HazelcastCacheManager; + import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.Matchers.contains; @@ -257,7 +259,8 @@ public class CacheAutoConfigurationTests { public void jCacheCacheWithExistingJCacheManager() { load(JCacheCustomCacheManager.class, "spring.cache.type=jcache"); JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class); - assertThat(cacheManager.getCacheManager(), is(this.context.getBean("customJCacheCacheManager"))); + assertThat(cacheManager.getCacheManager(), + is(this.context.getBean("customJCacheCacheManager"))); } @Test @@ -318,7 +321,8 @@ public class CacheAutoConfigurationTests { public void ehCacheCacheWithExistingCacheManager() { load(EhCacheCustomCacheManager.class, "spring.cache.type=ehcache"); EhCacheCacheManager cacheManager = validateCacheManager(EhCacheCacheManager.class); - assertThat(cacheManager.getCacheManager(), is(this.context.getBean("customEhCacheCacheManager"))); + assertThat(cacheManager.getCacheManager(), + is(this.context.getBean("customEhCacheCacheManager"))); } @Test @@ -330,12 +334,14 @@ public class CacheAutoConfigurationTests { assertThat(cacheManager.getCacheNames(), containsInAnyOrder("defaultCache")); assertThat(cacheManager.getCacheNames(), hasSize(1)); assertThat(this.context.getBean(HazelcastInstance.class), - is(new DirectFieldAccessor(cacheManager).getPropertyValue("hazelcastInstance"))); + is(new DirectFieldAccessor(cacheManager) + .getPropertyValue("hazelcastInstance"))); } @Test public void hazelcastCacheWithConfig() { - load(DefaultCacheConfiguration.class, "spring.cache.type=hazelcast", + load(DefaultCacheConfiguration.class, + "spring.cache.type=hazelcast", "spring.cache.hazelcast.config=org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"); HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class); cacheManager.getCache("foobar"); @@ -355,7 +361,9 @@ public class CacheAutoConfigurationTests { public void hazelcastCacheWithExistingHazelcastInstance() { load(HazelcastCustomHazelcastInstance.class, "spring.cache.type=hazelcast"); HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class); - assertThat(new DirectFieldAccessor(cacheManager).getPropertyValue("hazelcastInstance"), + assertThat( + new DirectFieldAccessor(cacheManager) + .getPropertyValue("hazelcastInstance"), is(this.context.getBean("customHazelcastInstance"))); } @@ -380,8 +388,7 @@ public class CacheAutoConfigurationTests { JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class); Resource configResource = new ClassPathResource(configLocation); - assertThat(cacheManager.getCacheManager().getURI(), - is(configResource.getURI())); + assertThat(cacheManager.getCacheManager().getURI(), is(configResource.getURI())); } @Test @@ -435,8 +442,7 @@ public class CacheAutoConfigurationTests { JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class); Resource configResource = new ClassPathResource(configLocation); - assertThat(cacheManager.getCacheManager().getURI(), - is(configResource.getURI())); + assertThat(cacheManager.getCacheManager().getURI(), is(configResource.getURI())); } @Test @@ -562,7 +568,8 @@ public class CacheAutoConfigurationTests { @Bean public javax.cache.CacheManager customJCacheCacheManager() { javax.cache.CacheManager cacheManager = mock(javax.cache.CacheManager.class); - when(cacheManager.getCacheNames()).thenReturn(Collections.emptyList()); + when(cacheManager.getCacheNames()).thenReturn( + Collections. emptyList()); return cacheManager; } diff --git a/spring-boot-samples/spring-boot-sample-cache/README.adoc b/spring-boot-samples/spring-boot-sample-cache/README.adoc index 6de6f0e05af..bc64c7dd808 100644 --- a/spring-boot-samples/spring-boot-sample-cache/README.adoc +++ b/spring-boot-samples/spring-boot-sample-cache/README.adoc @@ -16,8 +16,9 @@ the application starts a client invokes the service with a random code every 500 can look at the `/metrics` endpoint to review the cache statistics if your chosen caching provider is supported. -== Using the JSR-107 annotations + +== Using the JSR-107 annotations The sample uses Spring's cache annotation. If you want to use the JSR-107 annotations instead, simply add the `javax.cache:cache-api` dependency to the project. No further configuration is necessary. @@ -25,35 +26,40 @@ configuration is necessary. NOTE: You can use the JSR-107 annotations with _any_ cache provider; a JSR-107 compliant cache provider is not necessary. -== Using a different cache provider + +== Using a different cache provider Initially, the project does not define any caching library so the abstraction works on simple `ConcurrentHashMap`-based caches. You can try out your favorite caching library as explained below. -=== EhCache 2.x + +=== 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 used to configure the underlying `CacheManager`. -=== Hazelcast + +=== Hazelcast Both `com.hazelcast:hazelcast` and `com.hazelcast:hazelcast-spring` should be added to the project to enable support for Hazelcast. Since there is a default `hazelcast.xml` configuration file at the root of the classpath, it is used to automatically configure the underlying `HazelcastInstance`. -=== Infinispan + +=== Infinispan Simply add the `org.infinispan:infinispan-spring4` dependency to enable support for Infinispan. There is no default location that Infinispan uses to look for a config file so if you don't specify anything it will bootstrap on a hardcoded default. You can set the `spring.cache.infinispan.config` property to use the provided `infinispan.xml` configuration instead. -=== JCache (JSR-107) + +=== JCache (JSR-107) If you want to configure your cache infrastructure via the standard, you need a compliant implementation. You could try the following: @@ -65,14 +71,16 @@ 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 + +=== 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). -=== Guava + +=== Guava Spring Boot does not provide any dependency management for _Guava_ so you'll have to add the `com.google.guava:guava` dependency with a version. You can customize how caches are created in different ways, see `application.properties` for an example and the -documentation for more details. \ No newline at end of file +documentation for more details. diff --git a/spring-boot-samples/spring-boot-sample-cache/pom.xml b/spring-boot-samples/spring-boot-sample-cache/pom.xml index d035d055038..7d16731f3c6 100644 --- a/spring-boot-samples/spring-boot-sample-cache/pom.xml +++ b/spring-boot-samples/spring-boot-sample-cache/pom.xml @@ -24,7 +24,6 @@ org.springframework.boot spring-boot-starter-cache - org.springframework.boot @@ -34,12 +33,14 @@ org.springframework.boot spring-boot-starter-actuator - - + + + + + - org.springframework.boot spring-boot-starter-test diff --git a/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/CacheManagerCheck.java b/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/CacheManagerCheck.java new file mode 100644 index 00000000000..e6bdeb789bd --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/CacheManagerCheck.java @@ -0,0 +1,42 @@ +/* + * Copyright 2012-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.cache.CacheManager; +import org.springframework.stereotype.Component; + +@Component +public class CacheManagerCheck implements CommandLineRunner { + + private final CacheManager cacheManager; + + @Autowired + public CacheManagerCheck(CacheManager cacheManager) { + this.cacheManager = cacheManager; + } + + @Override + public void run(String... strings) throws Exception { + SampleCacheApplication.logger.info("\n\n" + + "=========================================================\n" + + "Using cache manager: " + this.cacheManager.getClass().getName() + "\n" + + "=========================================================\n\n"); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/Country.java b/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/Country.java index 9ec55fdcd84..24b2e2aec0a 100644 --- a/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/Country.java +++ b/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/Country.java @@ -28,22 +28,26 @@ public class Country implements Serializable { } public String getCode() { - return code; + return this.code; } @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } Country country = (Country) o; - return code.equals(country.code); + return this.code.equals(country.code); } @Override public int hashCode() { - return code.hashCode(); + return this.code.hashCode(); } } diff --git a/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/SampleCacheApplication.java b/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/SampleCacheApplication.java index 61c6ea80b7c..9165bd45393 100644 --- a/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/SampleCacheApplication.java +++ b/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/SampleCacheApplication.java @@ -18,46 +18,21 @@ package sample; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.stereotype.Component; @EnableCaching @EnableScheduling @SpringBootApplication public class SampleCacheApplication { - private static final Logger logger = LoggerFactory.getLogger(SampleCacheApplication.class); + static final Logger logger = LoggerFactory.getLogger(SampleCacheApplication.class); public static void main(String[] args) { - new SpringApplicationBuilder() - .sources(SampleCacheApplication.class) - .profiles("app") - .run(args); - } - - @Component - static class CacheManagerCheck implements CommandLineRunner { - - private final CacheManager cacheManager; - - @Autowired - CacheManagerCheck(CacheManager cacheManager) { - this.cacheManager = cacheManager; - } - - @Override - public void run(String... strings) throws Exception { - logger.info("\n\n" + "=========================================================\n" - + "Using cache manager: " + this.cacheManager.getClass().getName() + "\n" - + "=========================================================\n\n"); - } + new SpringApplicationBuilder().sources(SampleCacheApplication.class) + .profiles("app").run(args); } } diff --git a/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/SampleClient.java b/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/SampleClient.java index cdf0cbfed08..47334eefde5 100644 --- a/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/SampleClient.java +++ b/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/SampleClient.java @@ -29,23 +29,26 @@ import org.springframework.stereotype.Component; @Profile("app") class SampleClient { - private static final List SAMPLE_COUNTRY_CODES = Arrays.asList("AF", "AX", "AL", "DZ", "AS", - "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB", "BY", - "BE", "BZ", "BJ", "BM", "BT", "BO", "BQ", "BA", "BW", "BV", "BR", "IO", "BN", "BG", "BF", "BI", - "KH", "CM", "CA", "CV", "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CD", "CK", - "CR", "CI", "HR", "CU", "CW", "CY", "CZ", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", - "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF", "TF", "GA", "GM", "GE", "DE", "GH", "GI", - "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", "GW", "GY", "HT", "HM", "VA", "HN", "HK", "HU", - "IS", "IN", "ID", "IR", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI", - "KP", "KR", "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK", "MG", - "MW", "MY", "MV", "ML", "MT", "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "ME", - "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", - "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH", "PN", "PL", "PT", "PR", "QA", "RE", - "RO", "RU", "RW", "BL", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM", "ST", "SA", "SN", "RS", - "SC", "SL", "SG", "SX", "SK", "SI", "SB", "SO", "ZA", "GS", "SS", "ES", "LK", "SD", "SR", "SJ", - "SZ", "SE", "CH", "SY", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", - "TC", "TV", "UG", "UA", "AE", "GB", "US", "UM", "UY", "UZ", "VU", "VE", "VN", "VG", "VI", "WF", - "EH", "YE", "ZM", "ZW"); + private static final List SAMPLE_COUNTRY_CODES = Arrays.asList("AF", "AX", + "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", + "AZ", "BS", "BH", "BD", "BB", "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BQ", + "BA", "BW", "BV", "BR", "IO", "BN", "BG", "BF", "BI", "KH", "CM", "CA", "CV", + "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CD", "CK", "CR", + "CI", "HR", "CU", "CW", "CY", "CZ", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", + "GQ", "ER", "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF", "TF", "GA", + "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", + "GW", "GY", "HT", "HM", "VA", "HN", "HK", "HU", "IS", "IN", "ID", "IR", "IQ", + "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI", "KP", "KR", + "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK", + "MG", "MW", "MY", "MV", "ML", "MT", "MH", "MQ", "MR", "MU", "YT", "MX", "FM", + "MD", "MC", "MN", "ME", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "NC", + "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "NO", "OM", "PK", "PW", "PS", "PA", + "PG", "PY", "PE", "PH", "PN", "PL", "PT", "PR", "QA", "RE", "RO", "RU", "RW", + "BL", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM", "ST", "SA", "SN", "RS", + "SC", "SL", "SG", "SX", "SK", "SI", "SB", "SO", "ZA", "GS", "SS", "ES", "LK", + "SD", "SR", "SJ", "SZ", "SE", "CH", "SY", "TW", "TJ", "TZ", "TH", "TL", "TG", + "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "UG", "UA", "AE", "GB", "US", + "UM", "UY", "UZ", "VU", "VE", "VN", "VG", "VI", "WF", "EH", "YE", "ZM", "ZW"); private final CountryRepository countryService; @@ -59,7 +62,8 @@ class SampleClient { @Scheduled(fixedDelay = 500) public void retrieveCountry() { - String randomCode = SAMPLE_COUNTRY_CODES.get(random.nextInt(SAMPLE_COUNTRY_CODES.size())); + String randomCode = SAMPLE_COUNTRY_CODES.get(this.random + .nextInt(SAMPLE_COUNTRY_CODES.size())); System.out.println("Looking for country with code '" + randomCode + "'"); this.countryService.findByCode(randomCode); } diff --git a/spring-boot-samples/spring-boot-sample-cache/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-cache/src/main/resources/application.properties index b28030ed8b6..e67829f16c4 100644 --- a/spring-boot-samples/spring-boot-sample-cache/src/main/resources/application.properties +++ b/spring-boot-samples/spring-boot-sample-cache/src/main/resources/application.properties @@ -1,5 +1,3 @@ - - # # Infinispan configuration file location. # @@ -16,4 +14,4 @@ # # Guava configuration # -#spring.cache.guava.spec=maximumSize=200,expireAfterAccess=600s \ No newline at end of file +#spring.cache.guava.spec=maximumSize=200,expireAfterAccess=600s diff --git a/spring-boot-samples/spring-boot-sample-cache/src/test/java/sample/SampleCacheApplicationTests.java b/spring-boot-samples/spring-boot-sample-cache/src/test/java/sample/SampleCacheApplicationTests.java index fe9e6195a27..4b41c8e7fe8 100644 --- a/spring-boot-samples/spring-boot-sample-cache/src/test/java/sample/SampleCacheApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-cache/src/test/java/sample/SampleCacheApplicationTests.java @@ -18,7 +18,6 @@ package sample; import org.junit.Test; import org.junit.runner.RunWith; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.cache.Cache; @@ -31,10 +30,9 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.core.IsNull.notNullValue; @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = {SampleCacheApplication.class}) +@SpringApplicationConfiguration(classes = { SampleCacheApplication.class }) public class SampleCacheApplicationTests { - @Autowired private CacheManager cacheManager;