@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2012 the original author or authors .
* Copyright 2002 - 2015 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -17,7 +17,6 @@
@@ -17,7 +17,6 @@
package org.springframework.cache.annotation ;
import java.util.Collection ;
import javax.annotation.PostConstruct ;
import org.springframework.beans.factory.annotation.Autowired ;
@ -27,12 +26,11 @@ import org.springframework.context.annotation.Configuration;
@@ -27,12 +26,11 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportAware ;
import org.springframework.core.annotation.AnnotationAttributes ;
import org.springframework.core.type.AnnotationMetadata ;
import org.springframework.util.Assert ;
import org.springframework.util.CollectionUtils ;
/ * *
* Abstract base { @code @Configuration } class providing common structure for enabling
* Spring ' s annotation - driven cache management capability .
* Abstract base { @code @Configuration } class providing common structure
* for enabling Spring ' s annotation - driven cache management capability .
*
* @author Chris Beams
* @since 3 . 1
@ -42,22 +40,28 @@ import org.springframework.util.CollectionUtils;
@@ -42,22 +40,28 @@ import org.springframework.util.CollectionUtils;
public abstract class AbstractCachingConfiguration implements ImportAware {
protected AnnotationAttributes enableCaching ;
protected CacheManager cacheManager ;
protected KeyGenerator keyGenerator ;
@Autowired ( required = false )
@Autowired ( required = false )
private Collection < CacheManager > cacheManagerBeans ;
@Autowired ( required = false )
@Autowired ( required = false )
private Collection < CachingConfigurer > cachingConfigurers ;
public void setImportMetadata ( AnnotationMetadata importMetadata ) {
this . enableCaching = AnnotationAttributes . fromMap (
importMetadata . getAnnotationAttributes ( EnableCaching . class . getName ( ) , false ) ) ;
Assert . notNull ( this . enableCaching ,
"@EnableCaching is not present on importing class " +
importMetadata . getClassName ( ) ) ;
if ( this . enableCaching = = null ) {
throw new IllegalArgumentException (
"@EnableCaching is not present on importing class " + importMetadata . getClassName ( ) ) ;
}
}
/ * *
* Determine which { @code CacheManager } bean to use . Prefer the result of
* { @link CachingConfigurer # cacheManager ( ) } over any by - type matching . If none , fall
@ -68,20 +72,20 @@ public abstract class AbstractCachingConfiguration implements ImportAware {
@@ -68,20 +72,20 @@ public abstract class AbstractCachingConfiguration implements ImportAware {
* /
@PostConstruct
protected void reconcileCacheManager ( ) {
if ( ! CollectionUtils . isEmpty ( cachingConfigurers ) ) {
int nConfigurers = cachingConfigurers . size ( ) ;
if ( ! CollectionUtils . isEmpty ( this . cachingConfigurers ) ) {
int nConfigurers = this . cachingConfigurers . size ( ) ;
if ( nConfigurers > 1 ) {
throw new IllegalStateException ( nConfigurers + " implementations of " +
"CachingConfigurer were found when only 1 was expected. " +
"Refactor the configuration such that CachingConfigurer is " +
"implemented only once or not at all." ) ;
}
CachingConfigurer cachingConfigurer = cachingConfigurers . iterator ( ) . next ( ) ;
CachingConfigurer cachingConfigurer = this . cachingConfigurers . iterator ( ) . next ( ) ;
this . cacheManager = cachingConfigurer . cacheManager ( ) ;
this . keyGenerator = cachingConfigurer . keyGenerator ( ) ;
}
else if ( ! CollectionUtils . isEmpty ( cacheManagerBeans ) ) {
int nManagers = cacheManagerBeans . size ( ) ;
else if ( ! CollectionUtils . isEmpty ( this . cacheManagerBeans ) ) {
int nManagers = this . cacheManagerBeans . size ( ) ;
if ( nManagers > 1 ) {
throw new IllegalStateException ( nManagers + " beans of type CacheManager " +
"were found when only 1 was expected. Remove all but one of the " +
@ -89,8 +93,7 @@ public abstract class AbstractCachingConfiguration implements ImportAware {
@@ -89,8 +93,7 @@ public abstract class AbstractCachingConfiguration implements ImportAware {
"to make explicit which CacheManager should be used for " +
"annotation-driven cache management." ) ;
}
CacheManager cacheManager = cacheManagerBeans . iterator ( ) . next ( ) ;
this . cacheManager = cacheManager ;
this . cacheManager = cacheManager = this . cacheManagerBeans . iterator ( ) . next ( ) ;
// keyGenerator remains null; will fall back to default within CacheInterceptor
}
else {
@ -99,4 +102,5 @@ public abstract class AbstractCachingConfiguration implements ImportAware {
@@ -99,4 +102,5 @@ public abstract class AbstractCachingConfiguration implements ImportAware {
"from your configuration." ) ;
}
}
}