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