Skip to content

Commit 4fd778f

Browse files
committed
Polish CacheManagerCustomizers
1 parent 7ba16e3 commit 4fd778f

14 files changed

+58
-52
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,7 +66,7 @@
6666
@EnableConfigurationProperties(CacheProperties.class)
6767
@AutoConfigureBefore(HibernateJpaAutoConfiguration.class)
6868
@AutoConfigureAfter({ HazelcastAutoConfiguration.class, RedisAutoConfiguration.class })
69-
@Import({ CacheManagerCustomizerInvoker.class, CacheConfigurationImportSelector.class })
69+
@Import({ CacheManagerCustomizers.class, CacheConfigurationImportSelector.class })
7070
public class CacheAutoConfiguration {
7171

7272
static final String VALIDATOR_BEAN_NAME = "cacheAutoConfigurationValidator";

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheManagerCustomizer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
* Callback interface that can be implemented by beans wishing to customize the cache
2323
* manager before it is fully initialized, in particular to tune its configuration.
2424
*
25-
* @param <C> The type of the {@link CacheManager}
25+
* @param <T> The type of the {@link CacheManager}
2626
* @author Stephane Nicoll
2727
* @since 1.3.3
2828
*/
29-
public interface CacheManagerCustomizer<C extends CacheManager> {
29+
public interface CacheManagerCustomizer<T extends CacheManager> {
3030

3131
/**
3232
* Customize the cache manager.
3333
* @param cacheManager the {@code CacheManager} to customize
3434
*/
35-
void customize(C cacheManager);
35+
void customize(T cacheManager);
3636

3737
}
Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package org.springframework.boot.autoconfigure.cache;
1818

1919
import java.util.ArrayList;
20+
import java.util.Collection;
2021
import java.util.Collections;
2122
import java.util.List;
22-
import java.util.Map;
2323

2424
import org.springframework.beans.BeansException;
2525
import org.springframework.beans.factory.BeanFactoryUtils;
@@ -31,12 +31,12 @@
3131
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
3232

3333
/**
34-
* Invoke the available {@link CacheManagerCustomizer} instances in the context for a
34+
* Invokes the available {@link CacheManagerCustomizer} instances in the context for a
3535
* given {@link CacheManager}.
3636
*
3737
* @author Stephane Nicoll
3838
*/
39-
class CacheManagerCustomizerInvoker implements ApplicationContextAware {
39+
class CacheManagerCustomizers implements ApplicationContextAware {
4040

4141
private ConfigurableApplicationContext applicationContext;
4242

@@ -45,14 +45,16 @@ class CacheManagerCustomizerInvoker implements ApplicationContextAware {
4545
* {@link CacheManagerCustomizer} beans able to handle the specified instance and
4646
* invoke {@link CacheManagerCustomizer#customize(CacheManager)} on them.
4747
* @param cacheManager the cache manager to customize
48+
* @return the cache manager
4849
*/
49-
public void customize(CacheManager cacheManager) {
50+
public <T extends CacheManager> T customize(T cacheManager) {
5051
List<CacheManagerCustomizer<CacheManager>> customizers = findCustomizers(
5152
cacheManager);
5253
AnnotationAwareOrderComparator.sort(customizers);
5354
for (CacheManagerCustomizer<CacheManager> customizer : customizers) {
5455
customizer.customize(cacheManager);
5556
}
57+
return cacheManager;
5658
}
5759

5860
@SuppressWarnings({ "unchecked", "rawtypes" })
@@ -61,20 +63,28 @@ private List<CacheManagerCustomizer<CacheManager>> findCustomizers(
6163
if (this.applicationContext == null) {
6264
return Collections.emptyList();
6365
}
64-
Map<String, CacheManagerCustomizer> map = BeanFactoryUtils
65-
.beansOfTypeIncludingAncestors(this.applicationContext.getBeanFactory(),
66-
CacheManagerCustomizer.class);
66+
Class<?> cacheManagerClass = cacheManager.getClass();
6767
List<CacheManagerCustomizer<CacheManager>> customizers = new ArrayList<CacheManagerCustomizer<CacheManager>>();
68-
for (CacheManagerCustomizer customizer : map.values()) {
69-
Class<?> target = GenericTypeResolver.resolveTypeArgument(
70-
customizer.getClass(), CacheManagerCustomizer.class);
71-
if (target == null || target.isAssignableFrom(cacheManager.getClass())) {
68+
for (CacheManagerCustomizer customizer : getBeans(CacheManagerCustomizer.class)) {
69+
if (canCustomize(customizer, cacheManagerClass)) {
7270
customizers.add(customizer);
7371
}
7472
}
7573
return customizers;
7674
}
7775

76+
private <T> Collection<T> getBeans(Class<T> type) {
77+
return BeanFactoryUtils.beansOfTypeIncludingAncestors(
78+
this.applicationContext.getBeanFactory(), type).values();
79+
}
80+
81+
private boolean canCustomize(CacheManagerCustomizer<?> customizer,
82+
Class<?> cacheManagerClass) {
83+
Class<?> target = GenericTypeResolver.resolveTypeArgument(customizer.getClass(),
84+
CacheManagerCustomizer.class);
85+
return (target == null || target.isAssignableFrom(cacheManagerClass));
86+
}
87+
7888
@Override
7989
public void setApplicationContext(ApplicationContext applicationContext)
8090
throws BeansException {

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,13 +49,11 @@ class EhCacheCacheConfiguration {
4949
private CacheProperties cacheProperties;
5050

5151
@Autowired
52-
CacheManagerCustomizerInvoker customizerInvoker;
52+
private CacheManagerCustomizers customizers;
5353

5454
@Bean
5555
public EhCacheCacheManager cacheManager(CacheManager ehCacheCacheManager) {
56-
EhCacheCacheManager cacheManager = new EhCacheCacheManager(ehCacheCacheManager);
57-
this.customizerInvoker.customize(cacheManager);
58-
return cacheManager;
56+
return this.customizers.customize(new EhCacheCacheManager(ehCacheCacheManager));
5957
}
6058

6159
@Bean

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/GenericCacheConfiguration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@
4242
class GenericCacheConfiguration {
4343

4444
@Autowired
45-
CacheManagerCustomizerInvoker customizerInvoker;
45+
private CacheManagerCustomizers customizers;
4646

4747
@Bean
4848
public SimpleCacheManager cacheManager(Collection<Cache> caches) {
4949
SimpleCacheManager cacheManager = new SimpleCacheManager();
5050
cacheManager.setCaches(caches);
51-
this.customizerInvoker.customize(cacheManager);
52-
return cacheManager;
51+
return this.customizers.customize(cacheManager);
5352
}
5453

5554
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/GuavaCacheConfiguration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class GuavaCacheConfiguration {
4949
private CacheProperties cacheProperties;
5050

5151
@Autowired
52-
CacheManagerCustomizerInvoker customizerInvoker;
52+
private CacheManagerCustomizers customizers;
5353

5454
@Autowired(required = false)
5555
private CacheBuilder<Object, Object> cacheBuilder;
@@ -67,8 +67,7 @@ public GuavaCacheManager cacheManager() {
6767
if (!CollectionUtils.isEmpty(cacheNames)) {
6868
cacheManager.setCacheNames(cacheNames);
6969
}
70-
this.customizerInvoker.customize(cacheManager);
71-
return cacheManager;
70+
return this.customizers.customize(cacheManager);
7271
}
7372

7473
private GuavaCacheManager createCacheManager() {

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastInstanceConfiguration.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static class Existing {
4949
private CacheProperties cacheProperties;
5050

5151
@Autowired
52-
CacheManagerCustomizerInvoker customizerInvoker;
52+
private CacheManagerCustomizers customizers;
5353

5454
@Bean
5555
public HazelcastCacheManager cacheManager(
@@ -63,8 +63,7 @@ public HazelcastCacheManager cacheManager(
6363
}
6464
HazelcastCacheManager cacheManager = new HazelcastCacheManager(
6565
existingHazelcastInstance);
66-
this.customizerInvoker.customize(cacheManager);
67-
return cacheManager;
66+
return this.customizers.customize(cacheManager);
6867
}
6968
}
7069

@@ -77,7 +76,7 @@ static class Specific {
7776
private CacheProperties cacheProperties;
7877

7978
@Autowired
80-
CacheManagerCustomizerInvoker customizerInvoker;
79+
private CacheManagerCustomizers customizers;
8180

8281
@Bean
8382
public HazelcastInstance hazelcastInstance() throws IOException {
@@ -93,8 +92,7 @@ public HazelcastInstance hazelcastInstance() throws IOException {
9392
public HazelcastCacheManager cacheManager() throws IOException {
9493
HazelcastCacheManager cacheManager = new HazelcastCacheManager(
9594
hazelcastInstance());
96-
this.customizerInvoker.customize(cacheManager);
97-
return cacheManager;
95+
return this.customizers.customize(cacheManager);
9896
}
9997

10098
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/InfinispanCacheConfiguration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class InfinispanCacheConfiguration {
5252
private CacheProperties cacheProperties;
5353

5454
@Autowired
55-
CacheManagerCustomizerInvoker customizerInvoker;
55+
private CacheManagerCustomizers customizers;
5656

5757
@Autowired(required = false)
5858
private ConfigurationBuilder defaultConfigurationBuilder;
@@ -62,8 +62,7 @@ public SpringEmbeddedCacheManager cacheManager(
6262
EmbeddedCacheManager embeddedCacheManager) {
6363
SpringEmbeddedCacheManager cacheManager = new SpringEmbeddedCacheManager(
6464
embeddedCacheManager);
65-
this.customizerInvoker.customize(cacheManager);
66-
return cacheManager;
65+
return this.customizers.customize(cacheManager);
6766
}
6867

6968
@Bean(destroyMethod = "stop")

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class JCacheCacheConfiguration {
6464
private CacheProperties cacheProperties;
6565

6666
@Autowired
67-
CacheManagerCustomizerInvoker customizerInvoker;
67+
private CacheManagerCustomizers customizers;
6868

6969
@Autowired(required = false)
7070
private javax.cache.configuration.Configuration<?, ?> defaultCacheConfiguration;
@@ -75,8 +75,7 @@ class JCacheCacheConfiguration {
7575
@Bean
7676
public JCacheCacheManager cacheManager(CacheManager jCacheCacheManager) {
7777
JCacheCacheManager cacheManager = new JCacheCacheManager(jCacheCacheManager);
78-
this.customizerInvoker.customize(cacheManager);
79-
return cacheManager;
78+
return this.customizers.customize(cacheManager);
8079
}
8180

8281
@Bean

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class RedisCacheConfiguration {
4747
private CacheProperties cacheProperties;
4848

4949
@Autowired
50-
CacheManagerCustomizerInvoker customizerInvoker;
50+
private CacheManagerCustomizers customizerInvoker;
5151

5252
@Bean
5353
public RedisCacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate) {
@@ -56,8 +56,7 @@ public RedisCacheManager cacheManager(RedisTemplate<Object, Object> redisTemplat
5656
if (!cacheNames.isEmpty()) {
5757
cacheManager.setCacheNames(cacheNames);
5858
}
59-
this.customizerInvoker.customize(cacheManager);
60-
return cacheManager;
59+
return this.customizerInvoker.customize(cacheManager);
6160
}
6261

6362
}

0 commit comments

Comments
 (0)