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