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.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;
025
026/**
027 * Proxy factory bean for simplified declarative caching handling.
028 * This is a convenient alternative to a standard AOP
029 * {@link org.springframework.aop.framework.ProxyFactoryBean}
030 * with a separate {@link CacheInterceptor} definition.
031 *
032 * <p>This class is designed to facilitate declarative cache demarcation: namely, wrapping
033 * a singleton target object with a caching proxy, proxying all the interfaces that the
034 * target implements. Exists primarily for third-party framework integration.
035 * <strong>Users should favor the {@code cache:} XML namespace
036 * {@link org.springframework.cache.annotation.Cacheable @Cacheable} annotation.</strong>
037 * See the <a href="https://bit.ly/p9rIvx">declarative annotation-based caching</a> section
038 * of the Spring reference documentation for more information.
039 *
040 * @author Costin Leau
041 * @author Juergen Hoeller
042 * @since 3.1
043 * @see org.springframework.aop.framework.ProxyFactoryBean
044 * @see CacheInterceptor
045 */
046@SuppressWarnings("serial")
047public class CacheProxyFactoryBean extends AbstractSingletonProxyFactoryBean
048                implements BeanFactoryAware, SmartInitializingSingleton {
049
050        private final CacheInterceptor cacheInterceptor = new CacheInterceptor();
051
052        private Pointcut pointcut = Pointcut.TRUE;
053
054
055        /**
056         * Set one or more sources to find cache operations.
057         * @see CacheInterceptor#setCacheOperationSources
058         */
059        public void setCacheOperationSources(CacheOperationSource... cacheOperationSources) {
060                this.cacheInterceptor.setCacheOperationSources(cacheOperationSources);
061        }
062
063        /**
064         * Set a pointcut, i.e. a bean that triggers conditional invocation of the
065         * {@link CacheInterceptor} depending on the method and attributes passed.
066         * <p>Note: Additional interceptors are always invoked.
067         * @see #setPreInterceptors
068         * @see #setPostInterceptors
069         */
070        public void setPointcut(Pointcut pointcut) {
071                this.pointcut = pointcut;
072        }
073
074        @Override
075        public void setBeanFactory(BeanFactory beanFactory) {
076                this.cacheInterceptor.setBeanFactory(beanFactory);
077        }
078
079        @Override
080        public void afterSingletonsInstantiated() {
081                this.cacheInterceptor.afterSingletonsInstantiated();
082        }
083
084
085        @Override
086        protected Object createMainInterceptor() {
087                this.cacheInterceptor.afterPropertiesSet();
088                return new DefaultPointcutAdvisor(this.pointcut, this.cacheInterceptor);
089        }
090
091}