Skip to content

Commit 0690b58

Browse files
sslaviccbeams
authored andcommitted
Predict specific object type in EhCacheFactoryBean
Prior to this change, before a bean is created by EhCacheFactoryBean, its #getObjectType would return only an Ehcache interface. This caused unwanted wiring issues as described in the related JIRA issue. This fix makes use of EhCacheFactoryBean's configuration to determine the specific Ehcache object type even before it's created, such that the container is provided with as much information as possible when resolving dependencies. Nevertheless, users are advised to code to the Ehcache interface. Issue: SPR-7843
1 parent b2291ff commit 0690b58

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

spring-context/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -31,6 +31,7 @@
3131
import net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache;
3232
import net.sf.ehcache.event.CacheEventListener;
3333
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
34+
3435
import org.apache.commons.logging.Log;
3536
import org.apache.commons.logging.LogFactory;
3637

@@ -398,8 +399,27 @@ public Ehcache getObject() {
398399
return this.cache;
399400
}
400401

402+
/**
403+
* Predict the particular {@code Ehcache} implementation that will be returned from
404+
* {@link #getObject()} based on logic in {@link #createCache()} and
405+
* {@link #decorateCache(Ehcache)} as orchestrated by {@link #afterPropertiesSet()}.
406+
*/
401407
public Class<? extends Ehcache> getObjectType() {
402-
return (this.cache != null ? this.cache.getClass() : Ehcache.class);
408+
if (this.cache != null) {
409+
return this.cache.getClass();
410+
}
411+
if (this.cacheEntryFactory != null) {
412+
if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) {
413+
return UpdatingSelfPopulatingCache.class;
414+
}
415+
else {
416+
return SelfPopulatingCache.class;
417+
}
418+
}
419+
if (this.blocking) {
420+
return BlockingCache.class;
421+
}
422+
return Cache.class;
403423
}
404424

405425
public boolean isSingleton() {

spring-context/src/test/java/org/springframework/cache/ehcache/EhCacheSupportTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.cache.ehcache;
1818

1919
import junit.framework.TestCase;
20+
2021
import net.sf.ehcache.Cache;
2122
import net.sf.ehcache.CacheManager;
2223
import net.sf.ehcache.Ehcache;
@@ -82,7 +83,8 @@ private void doTestEhCacheFactoryBean(boolean useCacheManagerFb) throws Exceptio
8283
EhCacheManagerFactoryBean cacheManagerFb = null;
8384
try {
8485
EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
85-
assertEquals(Ehcache.class, cacheFb.getObjectType());
86+
Class<? extends Ehcache> objectType = cacheFb.getObjectType();
87+
assertTrue(Ehcache.class.isAssignableFrom(objectType));
8688
assertTrue("Singleton property", cacheFb.isSingleton());
8789
if (useCacheManagerFb) {
8890
cacheManagerFb = new EhCacheManagerFactoryBean();
@@ -94,6 +96,8 @@ private void doTestEhCacheFactoryBean(boolean useCacheManagerFb) throws Exceptio
9496
cacheFb.setCacheName("myCache1");
9597
cacheFb.afterPropertiesSet();
9698
cache = (Cache) cacheFb.getObject();
99+
Class<? extends Ehcache> objectType2 = cacheFb.getObjectType();
100+
assertSame(objectType, objectType2);
97101
CacheConfiguration config = cache.getCacheConfiguration();
98102
assertEquals("myCache1", cache.getName());
99103
if (useCacheManagerFb){
@@ -166,6 +170,7 @@ public void testEhCacheFactoryBeanWithBlockingCache() throws Exception {
166170
cacheFb.setCacheManager(cm);
167171
cacheFb.setCacheName("myCache1");
168172
cacheFb.setBlocking(true);
173+
assertEquals(cacheFb.getObjectType(), BlockingCache.class);
169174
cacheFb.afterPropertiesSet();
170175
Ehcache myCache1 = cm.getEhcache("myCache1");
171176
assertTrue(myCache1 instanceof BlockingCache);
@@ -188,6 +193,7 @@ public Object createEntry(Object key) throws Exception {
188193
return key;
189194
}
190195
});
196+
assertEquals(cacheFb.getObjectType(), SelfPopulatingCache.class);
191197
cacheFb.afterPropertiesSet();
192198
Ehcache myCache1 = cm.getEhcache("myCache1");
193199
assertTrue(myCache1 instanceof SelfPopulatingCache);
@@ -213,6 +219,7 @@ public Object createEntry(Object key) throws Exception {
213219
public void updateEntryValue(Object key, Object value) throws Exception {
214220
}
215221
});
222+
assertEquals(cacheFb.getObjectType(), UpdatingSelfPopulatingCache.class);
216223
cacheFb.afterPropertiesSet();
217224
Ehcache myCache1 = cm.getEhcache("myCache1");
218225
assertTrue(myCache1 instanceof UpdatingSelfPopulatingCache);

0 commit comments

Comments
 (0)