Support for bean refs in caching SpEL expressions

Issue: SPR-13182
This commit is contained in:
Stephane Nicoll
2015-12-22 11:05:24 +01:00
parent 1ce9788a66
commit 9b5e47026c
4 changed files with 103 additions and 21 deletions

View File

@@ -2,6 +2,7 @@ package org.springframework.cache.config;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.After;
import org.junit.Test;
import org.springframework.cache.Cache;
@@ -21,29 +22,37 @@ import org.springframework.context.annotation.Import;
import static org.springframework.cache.CacheTestUtils.*;
/**
* Tests that represent real use cases with advanced configuration
* Tests that represent real use cases with advanced configuration.
*
* @author Stephane Nicoll
*/
public class EnableCachingIntegrationTests {
private ConfigurableApplicationContext context;
@After
public void closeContext() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void fooServiceWithInterface() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(FooConfig.class);
FooService service = context.getBean(FooService.class);
fooGetSimple(context, service);
this.context = new AnnotationConfigApplicationContext(FooConfig.class);
FooService service = this.context.getBean(FooService.class);
fooGetSimple(service);
}
@Test
public void fooServiceWithInterfaceCglib() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(FooConfigCglib.class);
FooService service = context.getBean(FooService.class);
fooGetSimple(context, service);
this.context = new AnnotationConfigApplicationContext(FooConfigCglib.class);
FooService service = this.context.getBean(FooService.class);
fooGetSimple(service);
}
private void fooGetSimple(ApplicationContext context, FooService service) {
CacheManager cacheManager = context.getBean(CacheManager.class);
Cache cache = cacheManager.getCache("testCache");
private void fooGetSimple(FooService service) {
Cache cache = getCache();
Object key = new Object();
assertCacheMiss(key, cache);
@@ -52,6 +61,21 @@ public class EnableCachingIntegrationTests {
assertCacheHit(key, value, cache);
}
@Test
public void beanCondition() {
this.context = new AnnotationConfigApplicationContext(BeanConditionConfig.class);
Cache cache = getCache();
FooService service = context.getBean(FooService.class);
Object key = new Object();
service.getWithCondition(key);
assertCacheMiss(key, cache);
}
private Cache getCache() {
return this.context.getBean(CacheManager.class).getCache("testCache");
}
@Configuration
static class SharedConfig extends CachingConfigurerSupport {
@Override
@@ -81,8 +105,10 @@ public class EnableCachingIntegrationTests {
}
}
private static interface FooService {
public Object getSimple(Object key);
private interface FooService {
Object getSimple(Object key);
Object getWithCondition(Object key);
}
@CacheConfig(cacheNames = "testCache")
@@ -94,6 +120,35 @@ public class EnableCachingIntegrationTests {
public Object getSimple(Object key) {
return counter.getAndIncrement();
}
@Override
@Cacheable(condition = "@bar.enabled")
public Object getWithCondition(Object key) {
return counter.getAndIncrement();
}
}
@Configuration
@Import(FooConfig.class)
@EnableCaching
static class BeanConditionConfig {
@Bean
public Bar bar() {
return new Bar(false);
}
static class Bar {
private final boolean enabled;
public Bar(boolean enabled) {
this.enabled = enabled;
}
public boolean isEnabled() {
return enabled;
}
}
}
}

View File

@@ -23,11 +23,15 @@ import java.util.Iterator;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
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.context.expression.AnnotatedElementKey;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.util.ReflectionUtils;
@@ -75,7 +79,8 @@ public class ExpressionEvaluatorTests {
Object[] args = new Object[] { new Object(), new Object() };
Collection<ConcurrentMapCache> caches = Collections.singleton(new ConcurrentMapCache("test"));
EvaluationContext evalCtx = eval.createEvaluationContext(caches, method, args, target, target.getClass());
EvaluationContext evalCtx = eval.createEvaluationContext(caches, method, args,
target, target.getClass(), null);
Collection<CacheOperation> ops = getOps("multipleCaching");
Iterator<CacheOperation> it = ops.iterator();
@@ -122,14 +127,29 @@ public class ExpressionEvaluatorTests {
}
}
@Test
public void resolveBeanReference() throws Exception {
StaticApplicationContext applicationContext = new StaticApplicationContext();
BeanDefinition beanDefinition = new RootBeanDefinition(String.class);
applicationContext.registerBeanDefinition("myBean", beanDefinition);
applicationContext.refresh();
EvaluationContext context = createEvaluationContext(ExpressionEvaluator.NO_RESULT, applicationContext);
Object value = new SpelExpressionParser().parseExpression("@myBean.class.getName()").getValue(context);
assertThat(value, is(String.class.getName()));
}
private EvaluationContext createEvaluationContext(Object result) {
return createEvaluationContext(result, null);
}
private EvaluationContext createEvaluationContext(Object result, BeanFactory beanFactory) {
AnnotatedClass target = new AnnotatedClass();
Method method = ReflectionUtils.findMethod(AnnotatedClass.class, "multipleCaching", Object.class,
Object.class);
Object[] args = new Object[] { new Object(), new Object() };
Collection<ConcurrentMapCache> caches = Collections.singleton(new ConcurrentMapCache("test"));
EvaluationContext context = eval.createEvaluationContext(caches, method, args, target, target.getClass(), result);
return context;
return eval.createEvaluationContext(caches, method, args, target, target.getClass(), result, beanFactory);
}