001/* 002 * Copyright 2002-2019 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.interceptor; 018 019import org.springframework.aop.Pointcut; 020import org.springframework.aop.framework.AbstractSingletonProxyFactoryBean; 021import org.springframework.aop.support.DefaultPointcutAdvisor; 022import org.springframework.beans.factory.BeanFactory; 023import org.springframework.beans.factory.BeanFactoryAware; 024import org.springframework.beans.factory.SmartInitializingSingleton; 025import org.springframework.cache.CacheManager; 026 027/** 028 * Proxy factory bean for simplified declarative caching handling. 029 * This is a convenient alternative to a standard AOP 030 * {@link org.springframework.aop.framework.ProxyFactoryBean} 031 * with a separate {@link CacheInterceptor} definition. 032 * 033 * <p>This class is designed to facilitate declarative cache demarcation: namely, wrapping 034 * a singleton target object with a caching proxy, proxying all the interfaces that the 035 * target implements. Exists primarily for third-party framework integration. 036 * <strong>Users should favor the {@code cache:} XML namespace 037 * {@link org.springframework.cache.annotation.Cacheable @Cacheable} annotation.</strong> 038 * See the 039 * <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#cache-annotations">declarative annotation-based caching</a> 040 * section of the Spring reference documentation for more information. 041 * 042 * @author Costin Leau 043 * @author Juergen Hoeller 044 * @since 3.1 045 * @see org.springframework.aop.framework.ProxyFactoryBean 046 * @see CacheInterceptor 047 */ 048@SuppressWarnings("serial") 049public class CacheProxyFactoryBean extends AbstractSingletonProxyFactoryBean 050 implements BeanFactoryAware, SmartInitializingSingleton { 051 052 private final CacheInterceptor cacheInterceptor = new CacheInterceptor(); 053 054 private Pointcut pointcut = Pointcut.TRUE; 055 056 057 /** 058 * Set one or more sources to find cache operations. 059 * @see CacheInterceptor#setCacheOperationSources 060 */ 061 public void setCacheOperationSources(CacheOperationSource... cacheOperationSources) { 062 this.cacheInterceptor.setCacheOperationSources(cacheOperationSources); 063 } 064 065 /** 066 * Set the default {@link KeyGenerator} that this cache aspect should delegate to 067 * if no specific key generator has been set for the operation. 068 * <p>The default is a {@link SimpleKeyGenerator}. 069 * @since 5.0.3 070 * @see CacheInterceptor#setKeyGenerator 071 */ 072 public void setKeyGenerator(KeyGenerator keyGenerator) { 073 this.cacheInterceptor.setKeyGenerator(keyGenerator); 074 } 075 076 /** 077 * Set the default {@link CacheResolver} that this cache aspect should delegate 078 * to if no specific cache resolver has been set for the operation. 079 * <p>The default resolver resolves the caches against their names and the 080 * default cache manager. 081 * @since 5.0.3 082 * @see CacheInterceptor#setCacheResolver 083 */ 084 public void setCacheResolver(CacheResolver cacheResolver) { 085 this.cacheInterceptor.setCacheResolver(cacheResolver); 086 } 087 088 /** 089 * Set the {@link CacheManager} to use to create a default {@link CacheResolver}. 090 * Replace the current {@link CacheResolver}, if any. 091 * @since 5.0.3 092 * @see CacheInterceptor#setCacheManager 093 */ 094 public void setCacheManager(CacheManager cacheManager) { 095 this.cacheInterceptor.setCacheManager(cacheManager); 096 } 097 098 /** 099 * Set a pointcut, i.e. a bean that triggers conditional invocation of the 100 * {@link CacheInterceptor} depending on the method and attributes passed. 101 * <p>Note: Additional interceptors are always invoked. 102 * @see #setPreInterceptors 103 * @see #setPostInterceptors 104 */ 105 public void setPointcut(Pointcut pointcut) { 106 this.pointcut = pointcut; 107 } 108 109 @Override 110 public void setBeanFactory(BeanFactory beanFactory) { 111 this.cacheInterceptor.setBeanFactory(beanFactory); 112 } 113 114 @Override 115 public void afterSingletonsInstantiated() { 116 this.cacheInterceptor.afterSingletonsInstantiated(); 117 } 118 119 120 @Override 121 protected Object createMainInterceptor() { 122 this.cacheInterceptor.afterPropertiesSet(); 123 return new DefaultPointcutAdvisor(this.pointcut, this.cacheInterceptor); 124 } 125 126}