001/*
002 * Copyright 2002-2018 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 org.springframework.beans.factory.config.BeanDefinition;
020import org.springframework.cache.config.CacheManagementConfigUtils;
021import org.springframework.cache.interceptor.BeanFactoryCacheOperationSourceAdvisor;
022import org.springframework.cache.interceptor.CacheInterceptor;
023import org.springframework.cache.interceptor.CacheOperationSource;
024import org.springframework.context.annotation.Bean;
025import org.springframework.context.annotation.Configuration;
026import org.springframework.context.annotation.Role;
027
028/**
029 * {@code @Configuration} class that registers the Spring infrastructure beans necessary
030 * to enable proxy-based annotation-driven cache management.
031 *
032 * @author Chris Beams
033 * @author Juergen Hoeller
034 * @since 3.1
035 * @see EnableCaching
036 * @see CachingConfigurationSelector
037 */
038@Configuration
039@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
040public class ProxyCachingConfiguration extends AbstractCachingConfiguration {
041
042        @Bean(name = CacheManagementConfigUtils.CACHE_ADVISOR_BEAN_NAME)
043        @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
044        public BeanFactoryCacheOperationSourceAdvisor cacheAdvisor() {
045                BeanFactoryCacheOperationSourceAdvisor advisor = new BeanFactoryCacheOperationSourceAdvisor();
046                advisor.setCacheOperationSource(cacheOperationSource());
047                advisor.setAdvice(cacheInterceptor());
048                advisor.setOrder(this.enableCaching.<Integer>getNumber("order"));
049                return advisor;
050        }
051
052        @Bean
053        @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
054        public CacheOperationSource cacheOperationSource() {
055                return new AnnotationCacheOperationSource();
056        }
057
058        @Bean
059        @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
060        public CacheInterceptor cacheInterceptor() {
061                CacheInterceptor interceptor = new CacheInterceptor();
062                interceptor.setCacheOperationSources(cacheOperationSource());
063                if (this.cacheResolver != null) {
064                        interceptor.setCacheResolver(this.cacheResolver);
065                }
066                else if (this.cacheManager != null) {
067                        interceptor.setCacheManager(this.cacheManager);
068                }
069                if (this.keyGenerator != null) {
070                        interceptor.setKeyGenerator(this.keyGenerator);
071                }
072                if (this.errorHandler != null) {
073                        interceptor.setErrorHandler(this.errorHandler);
074                }
075                return interceptor;
076        }
077
078}