Skip to content

Commit c39a14a

Browse files
Costin Leaucbeams
authored andcommitted
Parse cache:annotation-driven key-generator attribute
Prior to this change, the spring-cache XSD allowed a 'key-generator' attribute, but it was not actually parsed by AnnotationDrivenCacheBDP. This commit adds the parsing logic as originally intended and the test to prove it. Issue: SPR-8939
1 parent e9ab1a7 commit c39a14a

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

org.springframework.aspects/src/test/java/org/springframework/cache/aspectj/AspectJAnnotationTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.cache.aspectj;
1818

19+
import org.junit.Assert;
20+
import org.junit.Test;
1921
import org.springframework.context.ApplicationContext;
2022
import org.springframework.context.support.GenericXmlApplicationContext;
2123

@@ -30,4 +32,10 @@ public class AspectJAnnotationTest extends AbstractAnnotationTest {
3032
protected ApplicationContext getApplicationContext() {
3133
return new GenericXmlApplicationContext("/org/springframework/cache/config/annotation-cache-aspectj.xml");
3234
}
35+
36+
@Test
37+
public void testKeyStrategy() throws Exception {
38+
AnnotationCacheAspect aspect = ctx.getBean("org.springframework.cache.config.internalCacheAspect", AnnotationCacheAspect.class);
39+
Assert.assertSame(ctx.getBean("keyGenerator"), aspect.getKeyGenerator());
40+
}
3341
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cache.config;
18+
19+
import org.springframework.cache.interceptor.DefaultKeyGenerator;
20+
21+
public class SomeKeyGenerator extends DefaultKeyGenerator {
22+
23+
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@
1414
<aop:config>
1515
<aop:advisor advice-ref="debugInterceptor" pointcut="execution(* *..CacheableService.*(..))" order="1"/>
1616
</aop:config>
17-
17+
1818
<bean id="cacheAspect" class="org.springframework.cache.aspectj.AnnotationCacheAspect" factory-method="aspectOf">
1919
<property name="cacheManager" ref="cacheManager"/>
2020
<property name="cacheOperationSources" ref="annotationSource"/>
2121
</bean>
2222
2323
<bean id="annotationSource" class="org.springframework.cache.annotation.AnnotationCacheOperationSource"/>
2424
-->
25-
26-
<cache:annotation-driven mode="aspectj"/>
25+
26+
<cache:annotation-driven mode="aspectj" key-generator="keyGenerator"/>
27+
28+
<bean id="keyGenerator" class="org.springframework.cache.config.SomeKeyGenerator" />
2729

2830
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
2931
<property name="caches">
@@ -36,8 +38,8 @@
3638
</bean>
3739

3840
<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor"/>
39-
41+
4042
<bean id="service" class="org.springframework.cache.config.DefaultCacheableService"/>
4143
<bean id="classService" class="org.springframework.cache.config.AnnotatedClassCacheableService"/>
4244

43-
</beans>
45+
</beans>

org.springframework.context/src/main/java/org/springframework/cache/config/AnnotationDrivenCacheBeanDefinitionParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ private static void parseCacheManagerProperty(Element element, BeanDefinition de
7676
* Registers a
7777
* <pre>
7878
* <bean id="cacheAspect" class="org.springframework.cache.aspectj.AnnotationCacheAspect" factory-method="aspectOf">
79-
* <property name="cacheManagerBeanName" value="cacheManager"/>
79+
* <property name="cacheManager" ref="cacheManager"/>
80+
* <property name="keyGenerator" ref="keyGenerator"/>
8081
* </bean>
8182
*
8283
* </pre>
@@ -89,6 +90,7 @@ private void registerCacheAspect(Element element, ParserContext parserContext) {
8990
def.setBeanClassName(CACHE_ASPECT_CLASS_NAME);
9091
def.setFactoryMethodName("aspectOf");
9192
parseCacheManagerProperty(element, def);
93+
CacheNamespaceHandler.parseKeyGenerator(element, def);
9294
parserContext.registerBeanComponent(new BeanComponentDefinition(def, CACHE_ASPECT_BEAN_NAME));
9395
}
9496
}

0 commit comments

Comments
 (0)