001/*
002 * Copyright 2002-2020 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.jcache.config;
018
019import java.util.function.Supplier;
020
021import org.springframework.beans.factory.config.BeanDefinition;
022import org.springframework.cache.annotation.AbstractCachingConfiguration;
023import org.springframework.cache.annotation.CachingConfigurer;
024import org.springframework.cache.interceptor.CacheResolver;
025import org.springframework.cache.jcache.interceptor.DefaultJCacheOperationSource;
026import org.springframework.cache.jcache.interceptor.JCacheOperationSource;
027import org.springframework.context.annotation.Bean;
028import org.springframework.context.annotation.Configuration;
029import org.springframework.context.annotation.Role;
030import org.springframework.lang.Nullable;
031
032/**
033 * Abstract JSR-107 specific {@code @Configuration} class providing common
034 * structure for enabling JSR-107 annotation-driven cache management capability.
035 *
036 * @author Stephane Nicoll
037 * @author Juergen Hoeller
038 * @since 4.1
039 * @see JCacheConfigurer
040 */
041@Configuration
042public abstract class AbstractJCacheConfiguration extends AbstractCachingConfiguration {
043
044        @Nullable
045        protected Supplier<CacheResolver> exceptionCacheResolver;
046
047
048        @Override
049        protected void useCachingConfigurer(CachingConfigurer config) {
050                super.useCachingConfigurer(config);
051                if (config instanceof JCacheConfigurer) {
052                        this.exceptionCacheResolver = ((JCacheConfigurer) config)::exceptionCacheResolver;
053                }
054        }
055
056        @Bean(name = "jCacheOperationSource")
057        @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
058        public JCacheOperationSource cacheOperationSource() {
059                return new DefaultJCacheOperationSource(
060                                this.cacheManager, this.cacheResolver, this.exceptionCacheResolver, this.keyGenerator);
061        }
062
063}