CacheProxyFactoryBean exposes all relevant CacheInterceptor callbacks
Issue: SPR-16295
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2017 the original author or authors.
|
* Copyright 2002-2018 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -51,22 +51,20 @@ import org.springframework.util.ObjectUtils;
|
|||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for caching aspects, such as the {@link CacheInterceptor}
|
* Base class for caching aspects, such as the {@link CacheInterceptor} or an
|
||||||
* or an AspectJ aspect.
|
* AspectJ aspect.
|
||||||
*
|
*
|
||||||
* <p>This enables the underlying Spring caching infrastructure to be
|
* <p>This enables the underlying Spring caching infrastructure to be used easily
|
||||||
* used easily to implement an aspect for any aspect system.
|
* to implement an aspect for any aspect system.
|
||||||
*
|
*
|
||||||
* <p>Subclasses are responsible for calling methods in this class in
|
* <p>Subclasses are responsible for calling relevant methods in the correct order.
|
||||||
* the correct order.
|
|
||||||
*
|
*
|
||||||
* <p>Uses the <b>Strategy</b> design pattern. A {@link CacheResolver}
|
* <p>Uses the <b>Strategy</b> design pattern. A {@link CacheOperationSource} is
|
||||||
* implementation will resolve the actual cache(s) to use, and a
|
* used for determining caching operations, a {@link KeyGenerator} will build the
|
||||||
* {@link CacheOperationSource} is used for determining caching
|
* cache keys, and a {@link CacheResolver} will resolve the actual cache(s) to use.
|
||||||
* operations.
|
|
||||||
*
|
*
|
||||||
* <p>A cache aspect is serializable if its {@code CacheResolver} and
|
* <p>Note: A cache aspect is serializable but does not perform any actual caching
|
||||||
* {@code CacheOperationSource} are serializable.
|
* after deserialization.
|
||||||
*
|
*
|
||||||
* @author Costin Leau
|
* @author Costin Leau
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
@@ -121,7 +119,7 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
|
|||||||
/**
|
/**
|
||||||
* Set the default {@link KeyGenerator} that this cache aspect should delegate to
|
* Set the default {@link KeyGenerator} that this cache aspect should delegate to
|
||||||
* if no specific key generator has been set for the operation.
|
* if no specific key generator has been set for the operation.
|
||||||
* <p>The default is a {@link SimpleKeyGenerator}
|
* <p>The default is a {@link SimpleKeyGenerator}.
|
||||||
*/
|
*/
|
||||||
public void setKeyGenerator(KeyGenerator keyGenerator) {
|
public void setKeyGenerator(KeyGenerator keyGenerator) {
|
||||||
this.keyGenerator = keyGenerator;
|
this.keyGenerator = keyGenerator;
|
||||||
@@ -134,22 +132,12 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
|
|||||||
return this.keyGenerator;
|
return this.keyGenerator;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the {@link CacheManager} to use to create a default {@link CacheResolver}.
|
|
||||||
* Replace the current {@link CacheResolver}, if any.
|
|
||||||
* @see #setCacheResolver(CacheResolver)
|
|
||||||
* @see SimpleCacheResolver
|
|
||||||
*/
|
|
||||||
public void setCacheManager(CacheManager cacheManager) {
|
|
||||||
this.cacheResolver = new SimpleCacheResolver(cacheManager);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the default {@link CacheResolver} that this cache aspect should delegate
|
* Set the default {@link CacheResolver} that this cache aspect should delegate
|
||||||
* to if no specific cache resolver has been set for the operation.
|
* to if no specific cache resolver has been set for the operation.
|
||||||
* <p>The default resolver resolves the caches against their names and the
|
* <p>The default resolver resolves the caches against their names and the
|
||||||
* default cache manager.
|
* default cache manager.
|
||||||
* @see #setCacheManager(org.springframework.cache.CacheManager)
|
* @see #setCacheManager
|
||||||
* @see SimpleCacheResolver
|
* @see SimpleCacheResolver
|
||||||
*/
|
*/
|
||||||
public void setCacheResolver(@Nullable CacheResolver cacheResolver) {
|
public void setCacheResolver(@Nullable CacheResolver cacheResolver) {
|
||||||
@@ -164,6 +152,16 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
|
|||||||
return this.cacheResolver;
|
return this.cacheResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the {@link CacheManager} to use to create a default {@link CacheResolver}.
|
||||||
|
* Replace the current {@link CacheResolver}, if any.
|
||||||
|
* @see #setCacheResolver
|
||||||
|
* @see SimpleCacheResolver
|
||||||
|
*/
|
||||||
|
public void setCacheManager(CacheManager cacheManager) {
|
||||||
|
this.cacheResolver = new SimpleCacheResolver(cacheManager);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the containing {@link BeanFactory} for {@link CacheManager} and other
|
* Set the containing {@link BeanFactory} for {@link CacheManager} and other
|
||||||
* service lookups.
|
* service lookups.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2017 the original author or authors.
|
* Copyright 2002-2018 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,6 +19,10 @@ package org.springframework.cache.interceptor;
|
|||||||
import org.springframework.aop.Pointcut;
|
import org.springframework.aop.Pointcut;
|
||||||
import org.springframework.aop.framework.AbstractSingletonProxyFactoryBean;
|
import org.springframework.aop.framework.AbstractSingletonProxyFactoryBean;
|
||||||
import org.springframework.aop.support.DefaultPointcutAdvisor;
|
import org.springframework.aop.support.DefaultPointcutAdvisor;
|
||||||
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
import org.springframework.beans.factory.BeanFactoryAware;
|
||||||
|
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||||
|
import org.springframework.cache.CacheManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Proxy factory bean for simplified declarative caching handling.
|
* Proxy factory bean for simplified declarative caching handling.
|
||||||
@@ -41,18 +45,53 @@ import org.springframework.aop.support.DefaultPointcutAdvisor;
|
|||||||
* @see CacheInterceptor
|
* @see CacheInterceptor
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class CacheProxyFactoryBean extends AbstractSingletonProxyFactoryBean {
|
public class CacheProxyFactoryBean extends AbstractSingletonProxyFactoryBean
|
||||||
|
implements BeanFactoryAware, SmartInitializingSingleton {
|
||||||
|
|
||||||
private final CacheInterceptor cachingInterceptor = new CacheInterceptor();
|
private final CacheInterceptor cacheInterceptor = new CacheInterceptor();
|
||||||
|
|
||||||
private Pointcut pointcut = Pointcut.TRUE;
|
private Pointcut pointcut = Pointcut.TRUE;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the sources used to find cache operations.
|
* Set the sources used to find cache operations.
|
||||||
|
* @see CacheInterceptor#setCacheOperationSources
|
||||||
*/
|
*/
|
||||||
public void setCacheOperationSources(CacheOperationSource... cacheOperationSources) {
|
public void setCacheOperationSources(CacheOperationSource... cacheOperationSources) {
|
||||||
this.cachingInterceptor.setCacheOperationSources(cacheOperationSources);
|
this.cacheInterceptor.setCacheOperationSources(cacheOperationSources);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the default {@link KeyGenerator} that this cache aspect should delegate to
|
||||||
|
* if no specific key generator has been set for the operation.
|
||||||
|
* <p>The default is a {@link SimpleKeyGenerator}.
|
||||||
|
* @since 5.0.3
|
||||||
|
* @see CacheInterceptor#setKeyGenerator
|
||||||
|
*/
|
||||||
|
public void setKeyGenerator(KeyGenerator keyGenerator) {
|
||||||
|
this.cacheInterceptor.setKeyGenerator(keyGenerator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the default {@link CacheResolver} that this cache aspect should delegate
|
||||||
|
* to if no specific cache resolver has been set for the operation.
|
||||||
|
* <p>The default resolver resolves the caches against their names and the
|
||||||
|
* default cache manager.
|
||||||
|
* @since 5.0.3
|
||||||
|
* @see CacheInterceptor#setCacheResolver
|
||||||
|
*/
|
||||||
|
public void setCacheResolver(CacheResolver cacheResolver) {
|
||||||
|
this.cacheInterceptor.setCacheResolver(cacheResolver);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the {@link CacheManager} to use to create a default {@link CacheResolver}.
|
||||||
|
* Replace the current {@link CacheResolver}, if any.
|
||||||
|
* @since 5.0.3
|
||||||
|
* @see CacheInterceptor#setCacheManager
|
||||||
|
*/
|
||||||
|
public void setCacheManager(CacheManager cacheManager) {
|
||||||
|
this.cacheInterceptor.setCacheManager(cacheManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -66,11 +105,21 @@ public class CacheProxyFactoryBean extends AbstractSingletonProxyFactoryBean {
|
|||||||
this.pointcut = pointcut;
|
this.pointcut = pointcut;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setBeanFactory(BeanFactory beanFactory) {
|
||||||
|
this.cacheInterceptor.setBeanFactory(beanFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterSingletonsInstantiated() {
|
||||||
|
this.cacheInterceptor.afterSingletonsInstantiated();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object createMainInterceptor() {
|
protected Object createMainInterceptor() {
|
||||||
this.cachingInterceptor.afterPropertiesSet();
|
this.cacheInterceptor.afterPropertiesSet();
|
||||||
return new DefaultPointcutAdvisor(this.pointcut, this.cachingInterceptor);
|
return new DefaultPointcutAdvisor(this.pointcut, this.cacheInterceptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
129
spring-context/src/test/java/org/springframework/cache/interceptor/CacheProxyFactoryBeanTests.java
vendored
Normal file
129
spring-context/src/test/java/org/springframework/cache/interceptor/CacheProxyFactoryBeanTests.java
vendored
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2002-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.cache.interceptor;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
|
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integration tests for {@link CacheProxyFactoryBean}.
|
||||||
|
*
|
||||||
|
* @author John Blum
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
*/
|
||||||
|
public class CacheProxyFactoryBeanTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void configurationClassWithCacheProxyFactoryBean() {
|
||||||
|
try (AnnotationConfigApplicationContext applicationContext =
|
||||||
|
new AnnotationConfigApplicationContext(CacheProxyFactoryBeanConfiguration.class)) {
|
||||||
|
Greeter greeter = applicationContext.getBean("greeter", Greeter.class);
|
||||||
|
assertNotNull(greeter);
|
||||||
|
assertFalse(greeter.isCacheMiss());
|
||||||
|
assertEquals("Hello John!", greeter.greet("John"));
|
||||||
|
assertTrue(greeter.isCacheMiss());
|
||||||
|
assertEquals("Hello Jon!", greeter.greet("Jon"));
|
||||||
|
assertTrue(greeter.isCacheMiss());
|
||||||
|
assertEquals("Hello John!", greeter.greet("John"));
|
||||||
|
assertFalse(greeter.isCacheMiss());
|
||||||
|
assertEquals("Hello World!", greeter.greet());
|
||||||
|
assertTrue(greeter.isCacheMiss());
|
||||||
|
assertEquals("Hello World!", greeter.greet());
|
||||||
|
assertFalse(greeter.isCacheMiss());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableCaching
|
||||||
|
static class CacheProxyFactoryBeanConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
ConcurrentMapCacheManager cacheManager() {
|
||||||
|
return new ConcurrentMapCacheManager("Greetings");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
CacheProxyFactoryBean greeter() {
|
||||||
|
CacheProxyFactoryBean factoryBean = new CacheProxyFactoryBean();
|
||||||
|
factoryBean.setCacheOperationSources(newCacheOperationSource("greet", newCacheOperation("Greetings")));
|
||||||
|
factoryBean.setTarget(new SimpleGreeter());
|
||||||
|
return factoryBean;
|
||||||
|
}
|
||||||
|
|
||||||
|
CacheOperationSource newCacheOperationSource(String methodName, CacheOperation... cacheOperations) {
|
||||||
|
NameMatchCacheOperationSource cacheOperationSource = new NameMatchCacheOperationSource();
|
||||||
|
cacheOperationSource.addCacheMethod(methodName, Arrays.asList(cacheOperations));
|
||||||
|
return cacheOperationSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
CacheableOperation newCacheOperation(String cacheName) {
|
||||||
|
CacheableOperation.Builder builder = new CacheableOperation.Builder();
|
||||||
|
builder.setCacheManager("cacheManager");
|
||||||
|
builder.setCacheName(cacheName);
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
interface Greeter {
|
||||||
|
|
||||||
|
default boolean isCacheHit() {
|
||||||
|
return !isCacheMiss();
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean isCacheMiss();
|
||||||
|
|
||||||
|
void setCacheMiss();
|
||||||
|
|
||||||
|
default String greet() {
|
||||||
|
return greet("World");
|
||||||
|
}
|
||||||
|
|
||||||
|
default String greet(String name) {
|
||||||
|
setCacheMiss();
|
||||||
|
return String.format("Hello %s!", name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static class SimpleGreeter implements Greeter {
|
||||||
|
|
||||||
|
private final AtomicBoolean cacheMiss = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCacheMiss() {
|
||||||
|
return this.cacheMiss.getAndSet(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setCacheMiss() {
|
||||||
|
this.cacheMiss.set(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user