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.cache.CacheManager;
020import org.springframework.cache.interceptor.CacheErrorHandler;
021import org.springframework.cache.interceptor.CacheResolver;
022import org.springframework.cache.interceptor.KeyGenerator;
023import org.springframework.lang.Nullable;
024
025/**
026 * Interface to be implemented by @{@link org.springframework.context.annotation.Configuration
027 * Configuration} classes annotated with @{@link EnableCaching} that wish or need to
028 * specify explicitly how caches are resolved and how keys are generated for annotation-driven
029 * cache management. Consider extending {@link CachingConfigurerSupport}, which provides a
030 * stub implementation of all interface methods.
031 *
032 * <p>See @{@link EnableCaching} for general examples and context; see
033 * {@link #cacheManager()}, {@link #cacheResolver()} and {@link #keyGenerator()}
034 * for detailed instructions.
035 *
036 * @author Chris Beams
037 * @author Stephane Nicoll
038 * @since 3.1
039 * @see EnableCaching
040 * @see CachingConfigurerSupport
041 */
042public interface CachingConfigurer {
043
044        /**
045         * Return the cache manager bean to use for annotation-driven cache
046         * management. A default {@link CacheResolver} will be initialized
047         * behind the scenes with this cache manager. For more fine-grained
048         * management of the cache resolution, consider setting the
049         * {@link CacheResolver} directly.
050         * <p>Implementations must explicitly declare
051         * {@link org.springframework.context.annotation.Bean @Bean}, e.g.
052         * <pre class="code">
053         * &#064;Configuration
054         * &#064;EnableCaching
055         * public class AppConfig extends CachingConfigurerSupport {
056         *     &#064;Bean // important!
057         *     &#064;Override
058         *     public CacheManager cacheManager() {
059         *         // configure and return CacheManager instance
060         *     }
061         *     // ...
062         * }
063         * </pre>
064         * See @{@link EnableCaching} for more complete examples.
065         */
066        @Nullable
067        CacheManager cacheManager();
068
069        /**
070         * Return the {@link CacheResolver} bean to use to resolve regular caches for
071         * annotation-driven cache management. This is an alternative and more powerful
072         * option of specifying the {@link CacheManager} to use.
073         * <p>If both a {@link #cacheManager()} and {@code #cacheResolver()} are set,
074         * the cache manager is ignored.
075         * <p>Implementations must explicitly declare
076         * {@link org.springframework.context.annotation.Bean @Bean}, e.g.
077         * <pre class="code">
078         * &#064;Configuration
079         * &#064;EnableCaching
080         * public class AppConfig extends CachingConfigurerSupport {
081         *     &#064;Bean // important!
082         *     &#064;Override
083         *     public CacheResolver cacheResolver() {
084         *         // configure and return CacheResolver instance
085         *     }
086         *     // ...
087         * }
088         * </pre>
089         * See {@link EnableCaching} for more complete examples.
090         */
091        @Nullable
092        CacheResolver cacheResolver();
093
094        /**
095         * Return the key generator bean to use for annotation-driven cache management.
096         * Implementations must explicitly declare
097         * {@link org.springframework.context.annotation.Bean @Bean}, e.g.
098         * <pre class="code">
099         * &#064;Configuration
100         * &#064;EnableCaching
101         * public class AppConfig extends CachingConfigurerSupport {
102         *     &#064;Bean // important!
103         *     &#064;Override
104         *     public KeyGenerator keyGenerator() {
105         *         // configure and return KeyGenerator instance
106         *     }
107         *     // ...
108         * }
109         * </pre>
110         * See @{@link EnableCaching} for more complete examples.
111         */
112        @Nullable
113        KeyGenerator keyGenerator();
114
115        /**
116         * Return the {@link CacheErrorHandler} to use to handle cache-related errors.
117         * <p>By default,{@link org.springframework.cache.interceptor.SimpleCacheErrorHandler}
118         * is used and simply throws the exception back at the client.
119         * <p>Implementations must explicitly declare
120         * {@link org.springframework.context.annotation.Bean @Bean}, e.g.
121         * <pre class="code">
122         * &#064;Configuration
123         * &#064;EnableCaching
124         * public class AppConfig extends CachingConfigurerSupport {
125         *     &#064;Bean // important!
126         *     &#064;Override
127         *     public CacheErrorHandler errorHandler() {
128         *         // configure and return CacheErrorHandler instance
129         *     }
130         *     // ...
131         * }
132         * </pre>
133         * See @{@link EnableCaching} for more complete examples.
134         */
135        @Nullable
136        CacheErrorHandler errorHandler();
137
138}