Introduce alias for 'value' attribute in caching annotations

This commit introduces new 'cacheNames' attributes (analogous to the
existing attribute of the same name in @CacheConfig) as aliases for the
'value' attributes in @Cacheable, @CachePut, and @CacheEvict.

In addition, SpringCacheAnnotationParser.getAnnotations() has been
refactored to support synthesized annotations.

Issue: SPR-11393
This commit is contained in:
Sam Brannen
2015-05-31 22:51:37 +02:00
parent de06f422f3
commit 4dffeeee64
10 changed files with 172 additions and 134 deletions

View File

@@ -23,6 +23,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
/**
* Annotation indicating that a method (or all methods on a class) triggers
* a cache eviction operation.
@@ -39,12 +41,22 @@ import java.lang.annotation.Target;
@Documented
public @interface CacheEvict {
/**
* Alias for {@link #cacheNames}.
*/
@AliasFor(attribute = "cacheNames")
String[] value() default {};
/**
* Names of the caches to use for the cache eviction operation.
* <p>Names may be used to determine the target cache (or caches), matching
* the qualifier value or bean name of a specific bean definition.
* @since 4.2
* @see #value
* @see CacheConfig#cacheNames
*/
String[] value() default {};
@AliasFor(attribute = "value")
String[] cacheNames() default {};
/**
* Spring Expression Language (SpEL) expression for computing the key dynamically.

View File

@@ -24,6 +24,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.cache.Cache;
import org.springframework.core.annotation.AliasFor;
/**
* Annotation indicating that a method (or all methods on a class) triggers
@@ -46,12 +47,22 @@ import org.springframework.cache.Cache;
@Documented
public @interface CachePut {
/**
* Alias for {@link #cacheNames}.
*/
@AliasFor(attribute = "cacheNames")
String[] value() default {};
/**
* Names of the caches to use for the cache put operation.
* <p>Names may be used to determine the target cache (or caches), matching
* the qualifier value or bean name of a specific bean definition.
* @since 4.2
* @see #value
* @see CacheConfig#cacheNames
*/
String[] value() default {};
@AliasFor(attribute = "value")
String[] cacheNames() default {};
/**
* Spring Expression Language (SpEL) expression for computing the key dynamically.

View File

@@ -23,6 +23,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
/**
* Annotation indicating that the result of invoking a method (or all methods
* in a class) can be cached.
@@ -50,12 +52,22 @@ import java.lang.annotation.Target;
@Documented
public @interface Cacheable {
/**
* Alias for {@link #cacheNames}.
*/
@AliasFor(attribute = "cacheNames")
String[] value() default {};
/**
* Names of the caches in which method invocation results are stored.
* <p>Names may be used to determine the target cache (or caches), matching
* the qualifier value or bean name of a specific bean definition.
* @since 4.2
* @see #value
* @see CacheConfig#cacheNames
*/
String[] value() default {};
@AliasFor(attribute = "value")
String[] cacheNames() default {};
/**
* Spring Expression Language (SpEL) expression for computing the key dynamically.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-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.
@@ -33,13 +33,14 @@ import org.springframework.util.StringUtils;
/**
* Strategy implementation for parsing Spring's {@link Caching}, {@link Cacheable},
* {@link CacheEvict} and {@link CachePut} annotations.
* {@link CacheEvict}, and {@link CachePut} annotations.
*
* @author Costin Leau
* @author Juergen Hoeller
* @author Chris Beams
* @author Phillip Webb
* @author Stephane Nicoll
* @author Sam Brannen
* @since 3.1
*/
@SuppressWarnings("serial")
@@ -96,16 +97,16 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
return (ops != null ? ops : new ArrayList<CacheOperation>(1));
}
CacheableOperation parseCacheableAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, Cacheable caching) {
CacheableOperation parseCacheableAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, Cacheable cacheable) {
CacheableOperation op = new CacheableOperation();
op.setCacheNames(caching.value());
op.setCondition(caching.condition());
op.setUnless(caching.unless());
op.setKey(caching.key());
op.setKeyGenerator(caching.keyGenerator());
op.setCacheManager(caching.cacheManager());
op.setCacheResolver(caching.cacheResolver());
op.setCacheNames(cacheable.cacheNames());
op.setCondition(cacheable.condition());
op.setUnless(cacheable.unless());
op.setKey(cacheable.key());
op.setKeyGenerator(cacheable.keyGenerator());
op.setCacheManager(cacheable.cacheManager());
op.setCacheResolver(cacheable.cacheResolver());
op.setName(ae.toString());
defaultConfig.applyDefault(op);
@@ -114,17 +115,17 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
return op;
}
CacheEvictOperation parseEvictAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, CacheEvict caching) {
CacheEvictOperation parseEvictAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, CacheEvict cacheEvict) {
CacheEvictOperation op = new CacheEvictOperation();
op.setCacheNames(caching.value());
op.setCondition(caching.condition());
op.setKey(caching.key());
op.setKeyGenerator(caching.keyGenerator());
op.setCacheManager(caching.cacheManager());
op.setCacheResolver(caching.cacheResolver());
op.setCacheWide(caching.allEntries());
op.setBeforeInvocation(caching.beforeInvocation());
op.setCacheNames(cacheEvict.cacheNames());
op.setCondition(cacheEvict.condition());
op.setKey(cacheEvict.key());
op.setKeyGenerator(cacheEvict.keyGenerator());
op.setCacheManager(cacheEvict.cacheManager());
op.setCacheResolver(cacheEvict.cacheResolver());
op.setCacheWide(cacheEvict.allEntries());
op.setBeforeInvocation(cacheEvict.beforeInvocation());
op.setName(ae.toString());
defaultConfig.applyDefault(op);
@@ -133,16 +134,16 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
return op;
}
CacheOperation parsePutAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, CachePut caching) {
CacheOperation parsePutAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, CachePut cachePut) {
CachePutOperation op = new CachePutOperation();
op.setCacheNames(caching.value());
op.setCondition(caching.condition());
op.setUnless(caching.unless());
op.setKey(caching.key());
op.setKeyGenerator(caching.keyGenerator());
op.setCacheManager(caching.cacheManager());
op.setCacheResolver(caching.cacheResolver());
op.setCacheNames(cachePut.cacheNames());
op.setCondition(cachePut.condition());
op.setUnless(cachePut.unless());
op.setKey(cachePut.key());
op.setKeyGenerator(cachePut.keyGenerator());
op.setCacheManager(cachePut.cacheManager());
op.setCacheResolver(cachePut.cacheResolver());
op.setName(ae.toString());
defaultConfig.applyDefault(op);
@@ -161,18 +162,18 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
ops.add(parseCacheableAnnotation(ae, defaultConfig, cacheable));
}
}
CacheEvict[] evicts = caching.evict();
if (!ObjectUtils.isEmpty(evicts)) {
CacheEvict[] cacheEvicts = caching.evict();
if (!ObjectUtils.isEmpty(cacheEvicts)) {
ops = lazyInit(ops);
for (CacheEvict evict : evicts) {
ops.add(parseEvictAnnotation(ae, defaultConfig, evict));
for (CacheEvict cacheEvict : cacheEvicts) {
ops.add(parseEvictAnnotation(ae, defaultConfig, cacheEvict));
}
}
CachePut[] updates = caching.put();
if (!ObjectUtils.isEmpty(updates)) {
CachePut[] cachePuts = caching.put();
if (!ObjectUtils.isEmpty(cachePuts)) {
ops = lazyInit(ops);
for (CachePut update : updates) {
ops.add(parsePutAnnotation(ae, defaultConfig, update));
for (CachePut cachePut : cachePuts) {
ops.add(parsePutAnnotation(ae, defaultConfig, cachePut));
}
}
@@ -199,14 +200,14 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
// look at raw annotation
T ann = ae.getAnnotation(annotationType);
if (ann != null) {
anns.add(ann);
anns.add(AnnotationUtils.synthesizeAnnotation(ann, ae));
}
// scan meta-annotations
for (Annotation metaAnn : ae.getAnnotations()) {
ann = metaAnn.annotationType().getAnnotation(annotationType);
if (ann != null) {
anns.add(ann);
anns.add(AnnotationUtils.synthesizeAnnotation(ann, ae));
}
}