Reduced DefaultJCacheOperationSource's dependency from ApplicationContext to BeanFactory
Issue: SPR-12336
This commit is contained in:
parent
0e36402bd2
commit
e2518e0b7c
|
|
@ -17,6 +17,8 @@
|
|||
package org.springframework.cache.jcache.interceptor;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
|
||||
|
|
@ -26,8 +28,6 @@ import org.springframework.cache.interceptor.CacheResolver;
|
|||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.cache.interceptor.SimpleCacheResolver;
|
||||
import org.springframework.cache.interceptor.SimpleKeyGenerator;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
|
|
@ -39,19 +39,19 @@ import org.springframework.util.Assert;
|
|||
* @since 4.1
|
||||
*/
|
||||
public class DefaultJCacheOperationSource extends AnnotationJCacheOperationSource
|
||||
implements ApplicationContextAware, InitializingBean, SmartInitializingSingleton {
|
||||
implements BeanFactoryAware, InitializingBean, SmartInitializingSingleton {
|
||||
|
||||
private CacheManager cacheManager;
|
||||
|
||||
private KeyGenerator keyGenerator = new SimpleKeyGenerator();
|
||||
|
||||
private KeyGenerator adaptedKeyGenerator;
|
||||
|
||||
private CacheResolver cacheResolver;
|
||||
|
||||
private CacheResolver exceptionCacheResolver;
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
private KeyGenerator keyGenerator = new SimpleKeyGenerator();
|
||||
|
||||
private KeyGenerator adaptedKeyGenerator;
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -63,16 +63,10 @@ public class DefaultJCacheOperationSource extends AnnotationJCacheOperationSourc
|
|||
}
|
||||
|
||||
/**
|
||||
* Set the default {@link KeyGenerator}. If none is set, a {@link SimpleKeyGenerator}
|
||||
* honoringKe the JSR-107 {@link javax.cache.annotation.CacheKey} and
|
||||
* {@link javax.cache.annotation.CacheValue} will be used.
|
||||
* Return the specified cache manager to use, if any.
|
||||
*/
|
||||
public void setKeyGenerator(KeyGenerator keyGenerator) {
|
||||
this.keyGenerator = keyGenerator;
|
||||
}
|
||||
|
||||
public KeyGenerator getKeyGenerator() {
|
||||
return this.keyGenerator;
|
||||
public CacheManager getCacheManager() {
|
||||
return this.cacheManager;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -83,8 +77,11 @@ public class DefaultJCacheOperationSource extends AnnotationJCacheOperationSourc
|
|||
this.cacheResolver = cacheResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the specified cache resolver to use, if any.
|
||||
*/
|
||||
public CacheResolver getCacheResolver() {
|
||||
return getDefaultCacheResolver();
|
||||
return this.cacheResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -95,13 +92,32 @@ public class DefaultJCacheOperationSource extends AnnotationJCacheOperationSourc
|
|||
this.exceptionCacheResolver = exceptionCacheResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the specified exception cache resolver to use, if any.
|
||||
*/
|
||||
public CacheResolver getExceptionCacheResolver() {
|
||||
return getDefaultExceptionCacheResolver();
|
||||
return this.exceptionCacheResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default {@link KeyGenerator}. If none is set, a {@link SimpleKeyGenerator}
|
||||
* honoringKe the JSR-107 {@link javax.cache.annotation.CacheKey} and
|
||||
* {@link javax.cache.annotation.CacheValue} will be used.
|
||||
*/
|
||||
public void setKeyGenerator(KeyGenerator keyGenerator) {
|
||||
this.keyGenerator = keyGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the specified key generator to use, if any.
|
||||
*/
|
||||
public KeyGenerator getKeyGenerator() {
|
||||
return this.keyGenerator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -121,7 +137,7 @@ public class DefaultJCacheOperationSource extends AnnotationJCacheOperationSourc
|
|||
@Override
|
||||
protected <T> T getBean(Class<T> type) {
|
||||
try {
|
||||
return this.applicationContext.getBean(type);
|
||||
return this.beanFactory.getBean(type);
|
||||
}
|
||||
catch (NoUniqueBeanDefinitionException ex) {
|
||||
throw new IllegalStateException("No unique [" + type.getName() + "] bean found in application context - " +
|
||||
|
|
@ -135,31 +151,10 @@ public class DefaultJCacheOperationSource extends AnnotationJCacheOperationSourc
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CacheResolver getDefaultCacheResolver() {
|
||||
if (this.cacheResolver == null) {
|
||||
this.cacheResolver = new SimpleCacheResolver(getCacheManager());
|
||||
}
|
||||
return this.cacheResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CacheResolver getDefaultExceptionCacheResolver() {
|
||||
if (this.exceptionCacheResolver == null) {
|
||||
this.exceptionCacheResolver = new SimpleExceptionCacheResolver(getCacheManager());
|
||||
}
|
||||
return this.exceptionCacheResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected KeyGenerator getDefaultKeyGenerator() {
|
||||
return this.adaptedKeyGenerator;
|
||||
}
|
||||
|
||||
private CacheManager getCacheManager() {
|
||||
protected CacheManager getDefaultCacheManager() {
|
||||
if (this.cacheManager == null) {
|
||||
try {
|
||||
this.cacheManager = this.applicationContext.getBean(CacheManager.class);
|
||||
this.cacheManager = this.beanFactory.getBean(CacheManager.class);
|
||||
}
|
||||
catch (NoUniqueBeanDefinitionException ex) {
|
||||
throw new IllegalStateException("No unique bean of type CacheManager found. "+
|
||||
|
|
@ -173,4 +168,25 @@ public class DefaultJCacheOperationSource extends AnnotationJCacheOperationSourc
|
|||
return this.cacheManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CacheResolver getDefaultCacheResolver() {
|
||||
if (this.cacheResolver == null) {
|
||||
this.cacheResolver = new SimpleCacheResolver(getDefaultCacheManager());
|
||||
}
|
||||
return this.cacheResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CacheResolver getDefaultExceptionCacheResolver() {
|
||||
if (this.exceptionCacheResolver == null) {
|
||||
this.exceptionCacheResolver = new SimpleExceptionCacheResolver(getDefaultCacheManager());
|
||||
}
|
||||
return this.exceptionCacheResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected KeyGenerator getDefaultKeyGenerator() {
|
||||
return this.adaptedKeyGenerator;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,13 +27,13 @@ import javax.cache.annotation.CacheResult;
|
|||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.cache.jcache.AbstractJCacheTests;
|
||||
import org.springframework.cache.jcache.support.TestableCacheKeyGenerator;
|
||||
import org.springframework.cache.jcache.support.TestableCacheResolver;
|
||||
import org.springframework.cache.jcache.support.TestableCacheResolverFactory;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
|
|
@ -47,17 +47,19 @@ public class AnnotationCacheOperationSourceTests extends AbstractJCacheTests {
|
|||
|
||||
private final DefaultJCacheOperationSource source = new DefaultJCacheOperationSource();
|
||||
|
||||
private final StaticApplicationContext applicationContext = new StaticApplicationContext();
|
||||
private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
source.setApplicationContext(applicationContext);
|
||||
source.setKeyGenerator(defaultKeyGenerator);
|
||||
source.setCacheResolver(defaultCacheResolver);
|
||||
source.setExceptionCacheResolver(defaultExceptionCacheResolver);
|
||||
source.setKeyGenerator(defaultKeyGenerator);
|
||||
source.setBeanFactory(beanFactory);
|
||||
source.afterPropertiesSet();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void cache() {
|
||||
CacheResultOperation op = getDefaultCacheOperation(CacheResultOperation.class, String.class);
|
||||
|
|
@ -104,29 +106,29 @@ public class AnnotationCacheOperationSourceTests extends AbstractJCacheTests {
|
|||
|
||||
@Test
|
||||
public void defaultCacheNameWithCandidate() {
|
||||
Method m = ReflectionUtils.findMethod(Object.class, "toString");
|
||||
assertEquals("foo", source.determineCacheName(m, null, "foo"));
|
||||
Method method = ReflectionUtils.findMethod(Object.class, "toString");
|
||||
assertEquals("foo", source.determineCacheName(method, null, "foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultCacheNameWithDefaults() {
|
||||
Method m = ReflectionUtils.findMethod(Object.class, "toString");
|
||||
Method method = ReflectionUtils.findMethod(Object.class, "toString");
|
||||
CacheDefaults mock = mock(CacheDefaults.class);
|
||||
given(mock.cacheName()).willReturn("");
|
||||
assertEquals("java.lang.Object.toString()", source.determineCacheName(m, mock, ""));
|
||||
assertEquals("java.lang.Object.toString()", source.determineCacheName(method, mock, ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultCacheNameNoDefaults() {
|
||||
Method m = ReflectionUtils.findMethod(Object.class, "toString");
|
||||
assertEquals("java.lang.Object.toString()", source.determineCacheName(m, null, ""));
|
||||
Method method = ReflectionUtils.findMethod(Object.class, "toString");
|
||||
assertEquals("java.lang.Object.toString()", source.determineCacheName(method, null, ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultCacheNameWithParameters() {
|
||||
Method m = ReflectionUtils.findMethod(Comparator.class, "compare", Object.class, Object.class);
|
||||
Method method = ReflectionUtils.findMethod(Comparator.class, "compare", Object.class, Object.class);
|
||||
assertEquals("java.util.Comparator.compare(java.lang.Object,java.lang.Object)",
|
||||
source.determineCacheName(m, null, ""));
|
||||
source.determineCacheName(method, null, ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -151,7 +153,7 @@ public class AnnotationCacheOperationSourceTests extends AbstractJCacheTests {
|
|||
@Test
|
||||
public void customKeyGeneratorSpringBean() {
|
||||
TestableCacheKeyGenerator bean = new TestableCacheKeyGenerator();
|
||||
applicationContext.getBeanFactory().registerSingleton("fooBar", bean);
|
||||
beanFactory.registerSingleton("fooBar", bean);
|
||||
CacheResultOperation operation =
|
||||
getCacheOperation(CacheResultOperation.class, CustomService.class, name.getMethodName(), Long.class);
|
||||
assertEquals(defaultCacheResolver, operation.getCacheResolver());
|
||||
|
|
@ -188,8 +190,9 @@ public class AnnotationCacheOperationSourceTests extends AbstractJCacheTests {
|
|||
return getCacheOperation(operationType, AnnotatedJCacheableService.class, name.getMethodName(), parameterTypes);
|
||||
}
|
||||
|
||||
protected <T extends JCacheOperation<?>> T getCacheOperation(Class<T> operationType, Class<?> targetType,
|
||||
String methodName, Class<?>... parameterTypes) {
|
||||
protected <T extends JCacheOperation<?>> T getCacheOperation(
|
||||
Class<T> operationType, Class<?> targetType, String methodName, Class<?>... parameterTypes) {
|
||||
|
||||
JCacheOperation<?> result = getCacheOperation(targetType, methodName, parameterTypes);
|
||||
assertNotNull(result);
|
||||
assertEquals(operationType, result.getClass());
|
||||
|
|
@ -204,6 +207,7 @@ public class AnnotationCacheOperationSourceTests extends AbstractJCacheTests {
|
|||
|
||||
private void assertJCacheResolver(CacheResolver actual,
|
||||
Class<? extends javax.cache.annotation.CacheResolver> expectedTargetType) {
|
||||
|
||||
if (expectedTargetType == null) {
|
||||
assertNull(actual);
|
||||
}
|
||||
|
|
@ -240,6 +244,7 @@ public class AnnotationCacheOperationSourceTests extends AbstractJCacheTests {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@CacheDefaults(cacheResolverFactory = TestableCacheResolverFactory.class,
|
||||
cacheKeyGenerator = TestableCacheKeyGenerator.class)
|
||||
static class CustomServiceWithDefaults {
|
||||
|
|
@ -255,6 +260,7 @@ public class AnnotationCacheOperationSourceTests extends AbstractJCacheTests {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
static class InvalidCases {
|
||||
|
||||
@CacheRemove
|
||||
|
|
|
|||
|
|
@ -20,13 +20,13 @@ import java.lang.reflect.Method;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.support.StaticListableBeanFactory;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvoker;
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.cache.interceptor.NamedCacheResolver;
|
||||
import org.springframework.cache.jcache.AbstractJCacheTests;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
|
@ -108,11 +108,11 @@ public class JCacheInterceptorTests extends AbstractJCacheTests {
|
|||
CacheResolver cacheResolver, CacheResolver exceptionCacheResolver, KeyGenerator keyGenerator) {
|
||||
|
||||
DefaultJCacheOperationSource source = new DefaultJCacheOperationSource();
|
||||
source.setApplicationContext(new StaticApplicationContext());
|
||||
source.setCacheManager(cacheManager);
|
||||
source.setCacheResolver(cacheResolver);
|
||||
source.setExceptionCacheResolver(exceptionCacheResolver);
|
||||
source.setKeyGenerator(keyGenerator);
|
||||
source.setBeanFactory(new StaticListableBeanFactory());
|
||||
source.afterPropertiesSet();
|
||||
source.afterSingletonsInstantiated();
|
||||
return source;
|
||||
|
|
|
|||
Loading…
Reference in New Issue