Reduced DefaultJCacheOperationSource's dependency from ApplicationContext to BeanFactory

Issue: SPR-12336
This commit is contained in:
Juergen Hoeller
2014-11-01 09:06:55 +01:00
parent 0e36402bd2
commit e2518e0b7c
3 changed files with 84 additions and 62 deletions

View File

@@ -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;
}
}