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