Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPR-8938 #9

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.cache.aspectj;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

Expand All @@ -30,4 +32,10 @@ public class AspectJAnnotationTest extends AbstractAnnotationTest {
protected ApplicationContext getApplicationContext() {
return new GenericXmlApplicationContext("/org/springframework/cache/config/annotation-cache-aspectj.xml");
}

@Test
public void testKeyStrategy() throws Exception {
AnnotationCacheAspect aspect = ctx.getBean("org.springframework.cache.config.internalCacheAspect", AnnotationCacheAspect.class);
Assert.assertSame(ctx.getBean("keyGenerator"), aspect.getKeyGenerator());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2011 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 org.springframework.cache.config;

import org.springframework.cache.interceptor.DefaultKeyGenerator;

public class SomeKeyGenerator extends DefaultKeyGenerator {

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
<bean id="annotationSource" class="org.springframework.cache.annotation.AnnotationCacheOperationSource"/>
-->

<cache:annotation-driven mode="aspectj"/>
<cache:annotation-driven mode="aspectj" key-generator="keyGenerator"/>

<bean id="keyGenerator" class="org.springframework.cache.config.SomeKeyGenerator" />

<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ private static void parseCacheManagerProperty(Element element, BeanDefinition de
* Registers a
* <pre>
* <bean id="cacheAspect" class="org.springframework.cache.aspectj.AnnotationCacheAspect" factory-method="aspectOf">
* <property name="cacheManagerBeanName" value="cacheManager"/>
* <property name="cacheManager" ref="cacheManager"/>
* <property name="keyGenerator" ref="keyGenerator"/>
* </bean>
*
* </pre>
Expand All @@ -89,6 +90,7 @@ private void registerCacheAspect(Element element, ParserContext parserContext) {
def.setBeanClassName(CACHE_ASPECT_CLASS_NAME);
def.setFactoryMethodName("aspectOf");
parseCacheManagerProperty(element, def);
CacheNamespaceHandler.parseKeyGenerator(element, def);
parserContext.registerBeanComponent(new BeanComponentDefinition(def, CACHE_ASPECT_BEAN_NAME));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ private void loadArgsAsVariables() {

// save arguments as indexed variables
for (int i = 0; i < this.args.length; i++) {
setVariable("a" + i, this.args[i]);
setVariable("p" + i, this.args[i]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public void singleStereotype() {
@EvictBar
public void multipleStereotype() {
}

@Caching(cacheable = { @Cacheable(value = "test", key = "a"), @Cacheable(value = "test", key = "b") })
public void multipleCaching() {
}
}

@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public Object multiCache(Object arg1) {
return counter.getAndIncrement();
}

@Caching(evict = { @CacheEvict("primary"), @CacheEvict(value = "secondary", key = "#p0"), @CacheEvict(value = "primary", key = "#p0 + 'A'") })
@Caching(evict = { @CacheEvict("primary"), @CacheEvict(value = "secondary", key = "#a0"), @CacheEvict(value = "primary", key = "#p0 + 'A'") })
public Object multiEvict(Object arg1) {
return counter.getAndIncrement();
}
Expand All @@ -126,7 +126,7 @@ public Object multiCacheAndEvict(Object arg1) {
return counter.getAndIncrement();
}

@Caching(cacheable = { @Cacheable(value = "primary", condition = "#p0 == 3") }, evict = { @CacheEvict("secondary") })
@Caching(cacheable = { @Cacheable(value = "primary", condition = "#a0 == 3") }, evict = { @CacheEvict("secondary") })
public Object multiConditionalCacheAndEvict(Object arg1) {
return counter.getAndIncrement();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2011 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 org.springframework.cache.interceptor;

import static org.junit.Assert.*;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;

import org.junit.Test;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.AnnotationCacheOperationSource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.expression.EvaluationContext;
import org.springframework.util.ReflectionUtils;

import edu.emory.mathcs.backport.java.util.Collections;

public class ExpressionEvalutatorTest {
private ExpressionEvaluator eval = new ExpressionEvaluator();

private AnnotationCacheOperationSource source = new AnnotationCacheOperationSource();

private Collection<CacheOperation> getOps(String name) {
Method method = ReflectionUtils.findMethod(AnnotatedClass.class, name, Object.class, Object.class);
return source.getCacheOperations(method, AnnotatedClass.class);
}

@Test
public void testMultipleCachingSource() throws Exception {
Collection<CacheOperation> ops = getOps("multipleCaching");
assertEquals(2, ops.size());
Iterator<CacheOperation> it = ops.iterator();
CacheOperation next = it.next();
assertTrue(next instanceof CacheableOperation);
assertTrue(next.getCacheNames().contains("test"));
assertEquals("#a", next.getKey());
next = it.next();
assertTrue(next instanceof CacheableOperation);
assertTrue(next.getCacheNames().contains("test"));
assertEquals("#b", next.getKey());
}

@Test
public void testMultipleCachingEval() throws Exception {
AnnotatedClass target = new AnnotatedClass();
Method method = ReflectionUtils.findMethod(AnnotatedClass.class, "multipleCaching", Object.class,
Object.class);
Object[] args = new Object[] { new Object(), new Object() };
Collection<Cache> map = Collections.singleton(new ConcurrentMapCache("test"));

EvaluationContext evalCtx = eval.createEvaluationContext(map, method, args, target, target.getClass());
Collection<CacheOperation> ops = getOps("multipleCaching");

Iterator<CacheOperation> it = ops.iterator();

Object keyA = eval.key(it.next().getKey(), method, evalCtx);
Object keyB = eval.key(it.next().getKey(), method, evalCtx);

assertEquals(args[0], keyA);
assertEquals(args[1], keyB);
}

private static class AnnotatedClass {
@Caching(cacheable = { @Cacheable(value = "test", key = "#a"), @Cacheable(value = "test", key = "#b") })
public void multipleCaching(Object a, Object b) {
}
}
}
14 changes: 7 additions & 7 deletions spring-framework-reference/src/cache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ public Book findBook(String name)]]></programlisting>
<entry><screen>#root.targetClass</screen></entry>
</row>
<row>
<entry>params</entry>
<entry>args</entry>
<entry>root object</entry>
<entry>The arguments (as array) used for invoking the target</entry>
<entry><screen>#root.params[0]</screen></entry>
<entry><screen>#root.args[0]</screen></entry>
</row>
<row>
<entry>caches</entry>
Expand All @@ -207,12 +207,12 @@ public Book findBook(String name)]]></programlisting>
<entry><screen>#root.caches[0].name</screen></entry>
</row>
<row>
<entry><emphasis>parameter name</emphasis></entry>
<entry><emphasis>argument name</emphasis></entry>
<entry>evaluation context</entry>
<entry>Name of any of the method parameter. If for some reason the names are not available (ex: no debug information),
the parameter names are also available under the <literal><![CDATA[p<#arg>]]></literal> where
<emphasis><![CDATA[#arg]]></emphasis> stands for the parameter index (starting from 0).</entry>
<entry><screen>iban</screen> or <screen>p0</screen></entry>
<entry>Name of any of the method argument. If for some reason the names are not available (ex: no debug information),
the argument names are also available under the <literal><![CDATA[a<#arg>]]></literal> where
<emphasis><![CDATA[#arg]]></emphasis> stands for the argument index (starting from 0).</entry>
<entry><screen>iban</screen> or <screen>a0</screen> (one can also use <screen>p0</screen> or <literal><![CDATA[p<#arg>]]></literal> notation as an alias).</entry>
</row>
</tbody>
</tgroup>
Expand Down