CacheProxyFactoryBean exposes all relevant CacheInterceptor callbacks
Issue: SPR-16295
This commit is contained in:
parent
44fd2d96cc
commit
b160f93495
|
|
@ -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");
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Base class for caching aspects, such as the {@link CacheInterceptor}
|
||||
* or an AspectJ aspect.
|
||||
* Base class for caching aspects, such as the {@link CacheInterceptor} or an
|
||||
* AspectJ aspect.
|
||||
*
|
||||
* <p>This enables the underlying Spring caching infrastructure to be
|
||||
* used easily to implement an aspect for any aspect system.
|
||||
* <p>This enables the underlying Spring caching infrastructure to be used easily
|
||||
* to implement an aspect for any aspect system.
|
||||
*
|
||||
* <p>Subclasses are responsible for calling methods in this class in
|
||||
* the correct order.
|
||||
* <p>Subclasses are responsible for calling relevant methods in the correct order.
|
||||
*
|
||||
* <p>Uses the <b>Strategy</b> design pattern. A {@link CacheResolver}
|
||||
* implementation will resolve the actual cache(s) to use, and a
|
||||
* {@link CacheOperationSource} is used for determining caching
|
||||
* operations.
|
||||
* <p>Uses the <b>Strategy</b> design pattern. A {@link CacheOperationSource} is
|
||||
* used for determining caching operations, a {@link KeyGenerator} will build the
|
||||
* cache keys, and a {@link CacheResolver} will resolve the actual cache(s) to use.
|
||||
*
|
||||
* <p>A cache aspect is serializable if its {@code CacheResolver} and
|
||||
* {@code CacheOperationSource} are serializable.
|
||||
* <p>Note: A cache aspect is serializable but does not perform any actual caching
|
||||
* after deserialization.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @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
|
||||
* 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) {
|
||||
this.keyGenerator = keyGenerator;
|
||||
|
|
@ -134,22 +132,12 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
|
|||
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
|
||||
* 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.
|
||||
* @see #setCacheManager(org.springframework.cache.CacheManager)
|
||||
* @see #setCacheManager
|
||||
* @see SimpleCacheResolver
|
||||
*/
|
||||
public void setCacheResolver(@Nullable CacheResolver cacheResolver) {
|
||||
|
|
@ -164,6 +152,16 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
|
|||
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
|
||||
* 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");
|
||||
* 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.framework.AbstractSingletonProxyFactoryBean;
|
||||
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.
|
||||
|
|
@ -41,18 +45,53 @@ import org.springframework.aop.support.DefaultPointcutAdvisor;
|
|||
* @see CacheInterceptor
|
||||
*/
|
||||
@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;
|
||||
|
||||
|
||||
/**
|
||||
* Set the sources used to find cache operations.
|
||||
* @see CacheInterceptor#setCacheOperationSources
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.cacheInterceptor.setBeanFactory(beanFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterSingletonsInstantiated() {
|
||||
this.cacheInterceptor.afterSingletonsInstantiated();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Object createMainInterceptor() {
|
||||
this.cachingInterceptor.afterPropertiesSet();
|
||||
return new DefaultPointcutAdvisor(this.pointcut, this.cachingInterceptor);
|
||||
this.cacheInterceptor.afterPropertiesSet();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue