Browse Source

Add custom cache manager per cache operation

It is now possible to specify the CacheManager to use per operation.
The related cache annotation now has an extra attribute that defines
the name of the CacheManager bean to use.  The cache manager that
was previously used is therefore a 'default' cache manager (i.e. the
one to use if no custom cache manager has been set on the operation).

Issue: SPR-8696
pull/442/merge
Stephane Nicoll 12 years ago
parent
commit
f06cad91c0
  1. 12
      spring-aspects/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java
  2. 4
      spring-aspects/src/test/java/org/springframework/cache/config/CacheableService.java
  3. 12
      spring-aspects/src/test/java/org/springframework/cache/config/DefaultCacheableService.java
  4. 17
      spring-aspects/src/test/java/org/springframework/cache/config/annotation-cache-aspectj.xml
  5. 6
      spring-context/src/main/java/org/springframework/cache/annotation/CacheEvict.java
  6. 6
      spring-context/src/main/java/org/springframework/cache/annotation/CachePut.java
  7. 6
      spring-context/src/main/java/org/springframework/cache/annotation/Cacheable.java
  8. 4
      spring-context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java
  9. 5
      spring-context/src/main/java/org/springframework/cache/config/CacheAdviceParser.java
  10. 24
      spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java
  11. 14
      spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java
  12. 6
      spring-context/src/main/resources/org/springframework/cache/config/spring-cache-4.0.xsd
  13. 32
      spring-context/src/test/java/org/springframework/cache/annotation/AnnotationCacheOperationSourceTests.java
  14. 27
      spring-context/src/test/java/org/springframework/cache/config/AbstractAnnotationTests.java
  15. 13
      spring-context/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java
  16. 5
      spring-context/src/test/java/org/springframework/cache/config/CacheableService.java
  17. 13
      spring-context/src/test/java/org/springframework/cache/config/DefaultCacheableService.java
  18. 8
      spring-context/src/test/java/org/springframework/cache/config/EnableCachingTests.java
  19. 24
      spring-context/src/test/resources/org/springframework/cache/config/annotationDrivenCacheConfig.xml
  20. 18
      spring-context/src/test/resources/org/springframework/cache/config/annotationDrivenCacheNamespace.xml
  21. 18
      spring-context/src/test/resources/org/springframework/cache/config/cache-advice.xml

12
spring-aspects/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java vendored

@ -117,6 +117,18 @@ public class AnnotatedClassCacheableService implements CacheableService<Object> @@ -117,6 +117,18 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
return counter.getAndIncrement();
}
@Override
@Cacheable(value = "default", cacheManager = "customCacheManager")
public Object customCacheManager(Object arg1) {
return counter.getAndIncrement();
}
@Override
@Cacheable(value = "default", cacheManager = "unknownBeanName")
public Object unknownCustomCacheManager(Object arg1) {
return counter.getAndIncrement();
}
@Override
@CachePut("default")
public Object update(Object arg1) {

4
spring-aspects/src/test/java/org/springframework/cache/config/CacheableService.java vendored

@ -62,6 +62,10 @@ public interface CacheableService<T> { @@ -62,6 +62,10 @@ public interface CacheableService<T> {
T unknownCustomKeyGenerator(Object arg1);
T customCacheManager(Object arg1);
T unknownCustomCacheManager(Object arg1);
T throwChecked(Object arg1) throws Exception;
T throwUnchecked(Object arg1);

12
spring-aspects/src/test/java/org/springframework/cache/config/DefaultCacheableService.java vendored

@ -121,6 +121,18 @@ public class DefaultCacheableService implements CacheableService<Long> { @@ -121,6 +121,18 @@ public class DefaultCacheableService implements CacheableService<Long> {
return counter.getAndIncrement();
}
@Override
@Cacheable(value = "default", cacheManager = "customCacheManager")
public Long customCacheManager(Object arg1) {
return counter.getAndIncrement();
}
@Override
@Cacheable(value = "default", cacheManager = "unknownBeanName")
public Long unknownCustomCacheManager(Object arg1) {
return counter.getAndIncrement();
}
@Override
@CachePut("default")
public Long update(Object arg1) {

17
spring-aspects/src/test/java/org/springframework/cache/config/annotation-cache-aspectj.xml vendored

@ -1,13 +1,10 @@ @@ -1,13 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!--
@ -45,6 +42,16 @@ @@ -45,6 +42,16 @@
<bean id="customKeyGenerator" class="org.springframework.cache.config.SomeCustomKeyGenerator" />
<bean id="customCacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<property name="name" value="default"/>
</bean>
</set>
</property>
</bean>
<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor"/>
<bean id="service" class="org.springframework.cache.config.DefaultCacheableService"/>

6
spring-context/src/main/java/org/springframework/cache/annotation/CacheEvict.java vendored

@ -28,6 +28,7 @@ import java.lang.annotation.Target; @@ -28,6 +28,7 @@ import java.lang.annotation.Target;
* a cache invalidate operation.
*
* @author Costin Leau
* @author Stephane Nicoll
* @since 3.1
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@ -56,6 +57,11 @@ public @interface CacheEvict { @@ -56,6 +57,11 @@ public @interface CacheEvict {
*/
String keyGenerator() default "";
/**
* The bean name of the custom {@link org.springframework.cache.CacheManager} to use.
*/
String cacheManager() default "";
/**
* Spring Expression Language (SpEL) attribute used for conditioning the method caching.
* <p>Default is "", meaning the method is always cached.

6
spring-context/src/main/java/org/springframework/cache/annotation/CachePut.java vendored

@ -33,6 +33,7 @@ import org.springframework.cache.Cache; @@ -33,6 +33,7 @@ import org.springframework.cache.Cache;
*
* @author Costin Leau
* @author Phillip Webb
* @author Stephane Nicoll
* @since 3.1
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@ -61,6 +62,11 @@ public @interface CachePut { @@ -61,6 +62,11 @@ public @interface CachePut {
*/
String keyGenerator() default "";
/**
* The bean name of the custom {@link org.springframework.cache.CacheManager} to use.
*/
String cacheManager() default "";
/**
* Spring Expression Language (SpEL) attribute used for conditioning the cache update.
* <p>Default is "", meaning the method result is always cached.

6
spring-context/src/main/java/org/springframework/cache/annotation/Cacheable.java vendored

@ -31,6 +31,7 @@ import java.lang.annotation.Target; @@ -31,6 +31,7 @@ import java.lang.annotation.Target;
*
* @author Costin Leau
* @author Phillip Webb
* @author Stephane Nicoll
* @since 3.1
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@ -59,6 +60,11 @@ public @interface Cacheable { @@ -59,6 +60,11 @@ public @interface Cacheable {
*/
String keyGenerator() default "";
/**
* The bean name of the custom {@link org.springframework.cache.CacheManager} to use.
*/
String cacheManager() default "";
/**
* Spring Expression Language (SpEL) attribute used for conditioning the method caching.
* <p>Default is "", meaning the method is always cached.

4
spring-context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java vendored

@ -37,6 +37,7 @@ import org.springframework.util.StringUtils; @@ -37,6 +37,7 @@ import org.springframework.util.StringUtils;
* @author Juergen Hoeller
* @author Chris Beams
* @author Phillip Webb
* @author Stephane Nicoll
* @since 3.1
*/
@SuppressWarnings("serial")
@ -88,6 +89,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria @@ -88,6 +89,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
cuo.setUnless(caching.unless());
cuo.setKey(caching.key());
cuo.setKeyGenerator(caching.keyGenerator());
cuo.setCacheManager(caching.cacheManager());
cuo.setName(ae.toString());
checkKeySourceConsistency(ae, caching.key(), caching.keyGenerator());
@ -100,6 +102,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria @@ -100,6 +102,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
ceo.setCondition(caching.condition());
ceo.setKey(caching.key());
ceo.setKeyGenerator(caching.keyGenerator());
ceo.setCacheManager(caching.cacheManager());
ceo.setCacheWide(caching.allEntries());
ceo.setBeforeInvocation(caching.beforeInvocation());
ceo.setName(ae.toString());
@ -115,6 +118,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria @@ -115,6 +118,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
cuo.setUnless(caching.unless());
cuo.setKey(caching.key());
cuo.setKeyGenerator(caching.keyGenerator());
cuo.setCacheManager(caching.cacheManager());
cuo.setName(ae.toString());
checkKeySourceConsistency(ae, caching.key(), caching.keyGenerator());

5
spring-context/src/main/java/org/springframework/cache/config/CacheAdviceParser.java vendored

@ -45,6 +45,7 @@ import org.w3c.dom.Element; @@ -45,6 +45,7 @@ import org.w3c.dom.Element;
*
* @author Costin Leau
* @author Phillip Webb
* @author Stephane Nicoll
*/
class CacheAdviceParser extends AbstractSingleBeanDefinitionParser {
@ -186,6 +187,8 @@ class CacheAdviceParser extends AbstractSingleBeanDefinitionParser { @@ -186,6 +187,8 @@ class CacheAdviceParser extends AbstractSingleBeanDefinitionParser {
private String keyGenerator;
private String cacheManager;
private String condition;
private String method;
@ -197,6 +200,7 @@ class CacheAdviceParser extends AbstractSingleBeanDefinitionParser { @@ -197,6 +200,7 @@ class CacheAdviceParser extends AbstractSingleBeanDefinitionParser {
String defaultCache = root.getAttribute("cache");
key = root.getAttribute("key");
keyGenerator = root.getAttribute("key-generator");
cacheManager = root.getAttribute("cache-manager");
condition = root.getAttribute("condition");
method = root.getAttribute(METHOD_ATTRIBUTE);
@ -222,6 +226,7 @@ class CacheAdviceParser extends AbstractSingleBeanDefinitionParser { @@ -222,6 +226,7 @@ class CacheAdviceParser extends AbstractSingleBeanDefinitionParser {
op.setKey(getAttributeValue(element, "key", this.key));
op.setKeyGenerator(getAttributeValue(element, "key-generator", this.keyGenerator));
op.setCacheManager(getAttributeValue(element, "cache-manager", this.cacheManager));
op.setCondition(getAttributeValue(element, "condition", this.condition));
if (StringUtils.hasText(op.getKey()) && StringUtils.hasText(op.getKeyGenerator())) {

24
spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java vendored

@ -88,14 +88,15 @@ public abstract class CacheAspectSupport implements InitializingBean, Applicatio @@ -88,14 +88,15 @@ public abstract class CacheAspectSupport implements InitializingBean, Applicatio
/**
* Set the CacheManager that this cache aspect should delegate to.
* Set the default {@link CacheManager} that this cache aspect should delegate to
* if no specific cache manager has been set for the operation.
*/
public void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
/**
* Return the CacheManager that this cache aspect delegates to.
* Return the default {@link CacheManager} that this cache aspect delegates to.
*/
public CacheManager getCacheManager() {
return this.cacheManager;
@ -164,14 +165,12 @@ public abstract class CacheAspectSupport implements InitializingBean, Applicatio @@ -164,14 +165,12 @@ public abstract class CacheAspectSupport implements InitializingBean, Applicatio
return ClassUtils.getQualifiedMethodName(specificMethod);
}
protected Collection<? extends Cache> getCaches(CacheOperation operation) {
protected Collection<? extends Cache> getCaches(CacheOperation operation, CacheManager cacheManager) {
Set<String> cacheNames = operation.getCacheNames();
Collection<Cache> caches = new ArrayList<Cache>(cacheNames.size());
for (String cacheName : cacheNames) {
Cache cache = this.cacheManager.getCache(cacheName);
if (cache == null) {
throw new IllegalArgumentException("Cannot find cache named '" + cacheName + "' for " + operation);
}
Cache cache = cacheManager.getCache(cacheName);
Assert.notNull(cache, "Cannot find cache named '" + cacheName + "' for " + operation);
caches.add(cache);
}
return caches;
@ -386,6 +385,8 @@ public abstract class CacheAspectSupport implements InitializingBean, Applicatio @@ -386,6 +385,8 @@ public abstract class CacheAspectSupport implements InitializingBean, Applicatio
private final KeyGenerator operationKeyGenerator;
private final CacheManager operationCacheManager;
public CacheOperationContext(CacheOperation operation, Method method,
Object[] args, Object target, Class<?> targetClass) {
this.operation = operation;
@ -393,13 +394,20 @@ public abstract class CacheAspectSupport implements InitializingBean, Applicatio @@ -393,13 +394,20 @@ public abstract class CacheAspectSupport implements InitializingBean, Applicatio
this.args = extractArgs(method, args);
this.target = target;
this.targetClass = targetClass;
this.caches = CacheAspectSupport.this.getCaches(operation);
if (StringUtils.hasText(operation.getKeyGenerator())) { // TODO: exception mgt?
this.operationKeyGenerator = BeanFactoryAnnotationUtils.qualifiedBeanOfType(
applicationContext, KeyGenerator.class, operation.getKeyGenerator());
} else {
this.operationKeyGenerator = keyGenerator;
}
if (StringUtils.hasText(operation.getCacheManager())) {
this.operationCacheManager = BeanFactoryAnnotationUtils.qualifiedBeanOfType(
applicationContext, CacheManager.class, operation.getCacheManager());
}
else {
this.operationCacheManager = cacheManager;
}
this.caches = CacheAspectSupport.this.getCaches(operation, operationCacheManager);
}
private Object[] extractArgs(Method method, Object[] args) {

14
spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java vendored

@ -26,6 +26,7 @@ import org.springframework.util.Assert; @@ -26,6 +26,7 @@ import org.springframework.util.Assert;
* Base class for cache operations.
*
* @author Costin Leau
* @author Stephane Nicoll
*/
public abstract class CacheOperation {
@ -37,6 +38,8 @@ public abstract class CacheOperation { @@ -37,6 +38,8 @@ public abstract class CacheOperation {
private String keyGenerator = "";
private String cacheManager = "";
private String name = "";
@ -56,6 +59,10 @@ public abstract class CacheOperation { @@ -56,6 +59,10 @@ public abstract class CacheOperation {
return keyGenerator;
}
public String getCacheManager() {
return cacheManager;
}
public String getName() {
return name;
}
@ -88,6 +95,11 @@ public abstract class CacheOperation { @@ -88,6 +95,11 @@ public abstract class CacheOperation {
this.keyGenerator = keyGenerator;
}
public void setCacheManager(String cacheManager) {
Assert.notNull(cacheManager);
this.cacheManager = cacheManager;
}
public void setName(String name) {
Assert.hasText(name);
this.name = name;
@ -137,6 +149,8 @@ public abstract class CacheOperation { @@ -137,6 +149,8 @@ public abstract class CacheOperation {
result.append(this.key);
result.append("' | keyGenerator='");
result.append(this.keyGenerator);
result.append("' | cacheManager='");
result.append(this.cacheManager);
result.append("' | condition='");
result.append(this.condition);
result.append("'");

6
spring-context/src/main/resources/org/springframework/cache/config/spring-cache-4.0.xsd vendored

@ -185,6 +185,12 @@ @@ -185,6 +185,12 @@
The name of the KeyGenerator bean responsible to compute the key, mutually exclusive with the key parameter.]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="cache-manager" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The name of the CacheManager bean responsible to manage the operation.]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="condition" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[

32
spring-context/src/test/java/org/springframework/cache/annotation/AnnotationCacheOperationSourceTests.java vendored

@ -34,6 +34,7 @@ import org.springframework.util.ReflectionUtils; @@ -34,6 +34,7 @@ import org.springframework.util.ReflectionUtils;
/**
* @author Costin Leau
* @author Stephane Nicoll
*/
public class AnnotationCacheOperationSourceTests {
@ -116,6 +117,22 @@ public class AnnotationCacheOperationSourceTests { @@ -116,6 +117,22 @@ public class AnnotationCacheOperationSourceTests {
}
}
@Test
public void testCustomCacheManager() {
Collection<CacheOperation> ops = getOps("customCacheManager");
assertEquals(1, ops.size());
CacheOperation cacheOperation = ops.iterator().next();
assertEquals("Custom cache manager not set", "custom", cacheOperation.getCacheManager());
}
@Test
public void testCustomCacheManagerInherited() {
Collection<CacheOperation> ops = getOps("customCacheManagerInherited");
assertEquals(1, ops.size());
CacheOperation cacheOperation = ops.iterator().next();
assertEquals("Custom cache manager not set", "custom", cacheOperation.getCacheManager());
}
private static class AnnotatedClass {
@Cacheable("test")
public void singular() {
@ -134,6 +151,10 @@ public class AnnotationCacheOperationSourceTests { @@ -134,6 +151,10 @@ public class AnnotationCacheOperationSourceTests {
public void customKeyGenerator() {
}
@Cacheable(value = "test", cacheManager = "custom")
public void customCacheManager() {
}
@EvictFoo
public void singleStereotype() {
}
@ -155,6 +176,10 @@ public class AnnotationCacheOperationSourceTests { @@ -155,6 +176,10 @@ public class AnnotationCacheOperationSourceTests {
@Cacheable(value = "test", key = "#root.methodName", keyGenerator = "custom")
public void invalidKeyAndKeyGeneratorSet() {
}
@CacheableFooCustomCacheManager
public void customCacheManagerInherited() {
}
}
@Retention(RetentionPolicy.RUNTIME)
@ -169,6 +194,13 @@ public class AnnotationCacheOperationSourceTests { @@ -169,6 +194,13 @@ public class AnnotationCacheOperationSourceTests {
public @interface CacheableFooCustomKeyGenerator {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Cacheable(value = "foo", cacheManager = "custom")
public @interface CacheableFooCustomCacheManager {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@CacheEvict(value = "foo")

27
spring-context/src/test/java/org/springframework/cache/config/AbstractAnnotationTests.java vendored

@ -36,6 +36,7 @@ import org.springframework.context.ApplicationContext; @@ -36,6 +36,7 @@ import org.springframework.context.ApplicationContext;
* @author Costin Leau
* @author Chris Beams
* @author Phillip Webb
* @author Stephane Nicoll
*/
public abstract class AbstractAnnotationTests {
@ -47,6 +48,8 @@ public abstract class AbstractAnnotationTests { @@ -47,6 +48,8 @@ public abstract class AbstractAnnotationTests {
protected CacheManager cm;
protected CacheManager customCm;
/** @return a refreshed application context */
protected abstract ApplicationContext getApplicationContext();
@ -55,7 +58,9 @@ public abstract class AbstractAnnotationTests { @@ -55,7 +58,9 @@ public abstract class AbstractAnnotationTests {
ctx = getApplicationContext();
cs = ctx.getBean("service", CacheableService.class);
ccs = ctx.getBean("classService", CacheableService.class);
cm = ctx.getBean(CacheManager.class);
cm = ctx.getBean("cacheManager", CacheManager.class);
customCm = ctx.getBean("customCacheManager", CacheManager.class);
Collection<String> cn = cm.getCacheNames();
assertTrue(cn.contains("default"));
assertTrue(cn.contains("secondary"));
@ -591,6 +596,26 @@ public abstract class AbstractAnnotationTests { @@ -591,6 +596,26 @@ public abstract class AbstractAnnotationTests {
}
}
@Test
public void testCustomCacheManager() {
Object key = new Object();
Object r1 = cs.customCacheManager(key);
assertSame(r1, cs.customCacheManager(key));
Cache cache = customCm.getCache("default");
assertNotNull(cache.get(key));
}
@Test
public void testUnknownCustomCacheManager() {
try {
Object param = new Object();
cs.unknownCustomCacheManager(param);
fail("should have failed with NoSuchBeanDefinitionException");
} catch (NoSuchBeanDefinitionException e) {
// expected
}
}
@Test
public void testNullArg() throws Exception {
testNullArg(cs);

13
spring-context/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java vendored

@ -26,6 +26,7 @@ import org.springframework.cache.annotation.Caching; @@ -26,6 +26,7 @@ import org.springframework.cache.annotation.Caching;
/**
* @author Costin Leau
* @author Phillip Webb
* @author Stephane Nicoll
*/
@Cacheable("default")
public class AnnotatedClassCacheableService implements CacheableService<Object> {
@ -119,6 +120,18 @@ public class AnnotatedClassCacheableService implements CacheableService<Object> @@ -119,6 +120,18 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
return counter.getAndIncrement();
}
@Override
@Cacheable(value = "default", cacheManager = "customCacheManager")
public Object customCacheManager(Object arg1) {
return counter.getAndIncrement();
}
@Override
@Cacheable(value = "default", cacheManager = "unknownBeanName")
public Object unknownCustomCacheManager(Object arg1) {
return counter.getAndIncrement();
}
@Override
@CachePut("default")
public Object update(Object arg1) {

5
spring-context/src/test/java/org/springframework/cache/config/CacheableService.java vendored

@ -21,6 +21,7 @@ package org.springframework.cache.config; @@ -21,6 +21,7 @@ package org.springframework.cache.config;
*
* @author Costin Leau
* @author Phillip Webb
* @author Stephane Nicoll
*/
public interface CacheableService<T> {
@ -62,6 +63,10 @@ public interface CacheableService<T> { @@ -62,6 +63,10 @@ public interface CacheableService<T> {
T unknownCustomKeyGenerator(Object arg1);
T customCacheManager(Object arg1);
T unknownCustomCacheManager(Object arg1);
T throwChecked(Object arg1) throws Exception;
T throwUnchecked(Object arg1);

13
spring-context/src/test/java/org/springframework/cache/config/DefaultCacheableService.java vendored

@ -28,6 +28,7 @@ import org.springframework.cache.annotation.Caching; @@ -28,6 +28,7 @@ import org.springframework.cache.annotation.Caching;
*
* @author Costin Leau
* @author Phillip Webb
* @author Stephane Nicoll
*/
public class DefaultCacheableService implements CacheableService<Long> {
@ -121,6 +122,18 @@ public class DefaultCacheableService implements CacheableService<Long> { @@ -121,6 +122,18 @@ public class DefaultCacheableService implements CacheableService<Long> {
return counter.getAndIncrement();
}
@Override
@Cacheable(value = "default", cacheManager = "customCacheManager")
public Long customCacheManager(Object arg1) {
return counter.getAndIncrement();
}
@Override
@Cacheable(value = "default", cacheManager = "unknownBeanName")
public Long unknownCustomCacheManager(Object arg1) {
return counter.getAndIncrement();
}
@Override
@CachePut("default")
public Long update(Object arg1) {

8
spring-context/src/test/java/org/springframework/cache/config/EnableCachingTests.java vendored

@ -40,6 +40,7 @@ import org.springframework.context.annotation.Configuration; @@ -40,6 +40,7 @@ import org.springframework.context.annotation.Configuration;
* Integration tests for @EnableCaching and its related @Configuration classes.
*
* @author Chris Beams
* @author Stephane Nicoll
*/
public class EnableCachingTests extends AbstractAnnotationTests {
@ -145,6 +146,13 @@ public class EnableCachingTests extends AbstractAnnotationTests { @@ -145,6 +146,13 @@ public class EnableCachingTests extends AbstractAnnotationTests {
public KeyGenerator customKeyGenerator() {
return new SomeCustomKeyGenerator();
}
@Bean
public CacheManager customCacheManager() {
SimpleCacheManager cm = new SimpleCacheManager();
cm.setCaches(Arrays.asList(new ConcurrentMapCache("default")));
return cm;
}
}

24
spring-context/src/test/resources/org/springframework/cache/config/annotationDrivenCacheConfig.xml vendored

@ -2,27 +2,27 @@ @@ -2,27 +2,27 @@
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="apc" class="org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator"/>
<bean id="annotationSource" class="org.springframework.cache.annotation.AnnotationCacheOperationSource"/>
<aop:config>
<aop:advisor advice-ref="debugInterceptor" pointcut="execution(* *..CacheableService.*(..))" order="1"/>
</aop:config>
<bean id="cacheInterceptor" class="org.springframework.cache.interceptor.CacheInterceptor">
<property name="cacheManager" ref="cacheManager"/>
<property name="cacheOperationSources" ref="annotationSource"/>
</bean>
<bean id="advisor" class="org.springframework.cache.interceptor.BeanFactoryCacheOperationSourceAdvisor">
<property name="cacheOperationSource" ref="annotationSource"/>
<property name="adviceBeanName" value="cacheInterceptor"/>
</bean>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
@ -35,13 +35,21 @@ @@ -35,13 +35,21 @@
</bean>
<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor"/>
<bean id="service" class="org.springframework.cache.config.DefaultCacheableService"/>
<bean id="classService" class="org.springframework.cache.config.AnnotatedClassCacheableService"/>
<bean id="keyGenerator" class="org.springframework.cache.config.SomeKeyGenerator"/>
<bean id="customKeyGenerator" class="org.springframework.cache.config.SomeCustomKeyGenerator"/>
<bean id="customCacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default"/>
</set>
</property>
</bean>
</beans>

18
spring-context/src/test/resources/org/springframework/cache/config/annotationDrivenCacheNamespace.xml vendored

@ -3,16 +3,16 @@ @@ -3,16 +3,16 @@
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven proxy-target-class="false" order="0" key-generator="keyGenerator"/>
<aop:config>
<aop:advisor advice-ref="debugInterceptor" pointcut="execution(* *..CacheableService.*(..))" order="1"/>
</aop:config>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
@ -24,13 +24,21 @@ @@ -24,13 +24,21 @@
</bean>
<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor"/>
<bean id="service" class="org.springframework.cache.config.DefaultCacheableService"/>
<bean id="classService" class="org.springframework.cache.config.AnnotatedClassCacheableService"/>
<bean id="keyGenerator" class="org.springframework.cache.config.SomeKeyGenerator"/>
<bean id="customKeyGenerator" class="org.springframework.cache.config.SomeCustomKeyGenerator"/>
<bean id="customCacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default"/>
</set>
</property>
</bean>
</beans>

18
spring-context/src/test/resources/org/springframework/cache/config/cache-advice.xml vendored

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
@ -18,6 +18,8 @@ @@ -18,6 +18,8 @@
<cache:cacheable method="rootVars" key="#root.methodName + #root.method.name + #root.targetClass + #root.target"/>
<cache:cacheable method="customKeyGenerator" key-generator="customKeyGenerator"/>
<cache:cacheable method="unknownCustomKeyGenerator" key-generator="unknownBeanName"/>
<cache:cacheable method="customCacheManager" cache-manager="customCacheManager"/>
<cache:cacheable method="unknownCustomCacheManager" cache-manager="unknownBeanName"/>
<cache:cacheable method="nullValue" cache="default"/>
</cache:caching>
<cache:caching>
@ -111,6 +113,14 @@ @@ -111,6 +113,14 @@
<bean id="customKeyGenerator" class="org.springframework.cache.config.SomeCustomKeyGenerator"/>
<bean id="customCacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default"/>
</set>
</property>
</bean>
<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor"/>
<bean id="service" class="org.springframework.cache.config.DefaultCacheableService"/>

Loading…
Cancel
Save