001/*
002 * Copyright 2002-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.cache.annotation;
018
019import java.util.Collection;
020
021import org.springframework.beans.factory.annotation.Autowired;
022import org.springframework.cache.CacheManager;
023import org.springframework.cache.interceptor.CacheErrorHandler;
024import org.springframework.cache.interceptor.CacheResolver;
025import org.springframework.cache.interceptor.KeyGenerator;
026import org.springframework.context.annotation.Configuration;
027import org.springframework.context.annotation.ImportAware;
028import org.springframework.core.annotation.AnnotationAttributes;
029import org.springframework.core.type.AnnotationMetadata;
030import org.springframework.util.CollectionUtils;
031
032/**
033 * Abstract base {@code @Configuration} class providing common structure
034 * for enabling Spring's annotation-driven cache management capability.
035 *
036 * @author Chris Beams
037 * @author Stephane Nicoll
038 * @since 3.1
039 * @see EnableCaching
040 */
041@Configuration
042public abstract class AbstractCachingConfiguration implements ImportAware {
043
044        protected AnnotationAttributes enableCaching;
045
046        protected CacheManager cacheManager;
047
048        protected CacheResolver cacheResolver;
049
050        protected KeyGenerator keyGenerator;
051
052        protected CacheErrorHandler errorHandler;
053
054
055        @Override
056        public void setImportMetadata(AnnotationMetadata importMetadata) {
057                this.enableCaching = AnnotationAttributes.fromMap(
058                                importMetadata.getAnnotationAttributes(EnableCaching.class.getName(), false));
059                if (this.enableCaching == null) {
060                        throw new IllegalArgumentException(
061                                        "@EnableCaching is not present on importing class " + importMetadata.getClassName());
062                }
063        }
064
065        @Autowired(required = false)
066        void setConfigurers(Collection<CachingConfigurer> configurers) {
067                if (CollectionUtils.isEmpty(configurers)) {
068                        return;
069                }
070                if (configurers.size() > 1) {
071                        throw new IllegalStateException(configurers.size() + " implementations of " +
072                                        "CachingConfigurer were found when only 1 was expected. " +
073                                        "Refactor the configuration such that CachingConfigurer is " +
074                                        "implemented only once or not at all.");
075                }
076                CachingConfigurer configurer = configurers.iterator().next();
077                useCachingConfigurer(configurer);
078        }
079
080        /**
081         * Extract the configuration from the nominated {@link CachingConfigurer}.
082         */
083        protected void useCachingConfigurer(CachingConfigurer config) {
084                this.cacheManager = config.cacheManager();
085                this.cacheResolver = config.cacheResolver();
086                this.keyGenerator = config.keyGenerator();
087                this.errorHandler = config.errorHandler();
088        }
089
090}