Issue Details (XML | Word | Printable)

Key: SPR-4187
Type: Improvement Improvement
Status: Open Open
Priority: Major Major
Assignee: Juergen Hoeller
Reporter: Josh Devins
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Spring Framework

Add multiple config locations to EhCacheManagerFactoryBean to allow for deploy-time config overriding

Created: 28/Nov/07 07:46 PM   Updated: 14/May/08 06:08 PM
Component/s: SpringCORE
Affects Version/s: 2.5 final
Fix Version/s: None

Time Tracking:
Not Specified

File Attachments: 1. Java Source File EhCacheManagerFactoryBean.java (1 kB)
2. Java Source File EhCacheManagerFactoryBean2.java (5 kB)
3. XML File EhCacheManagerFactoryBean2.xml (0.5 kB)
4. Java Source File EhCacheManagerFactoryBeanTest.java (4 kB)
5. Java Source File EhCacheManagerFactoryBeanTest2.java (6 kB)



 Description  « Hide
Much like providing a list of properties files in priority for PropertyPlaceholderConfigurer, it would be nice to be able to do the same for EhCacheManagerFactoryBean. This would allow a default EhCache configuration to be inside your Jar/War on the classpath, and if need be, you can override that configuration by providing another location to load from.

Example XML:

<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocations">
<list>
<value>classpath*:ehcache.override.xml</value>
<value>classpath*:META-INF/ehcache.default.xml</value>
</list>
</property>
</bean>

Example code that should be added to EhCacheManagerFactoryBean (or in a subclass of it):

/**

  • Based on an array of configuration location {@link Resource}s listed in
    * order of decending priority, this will use the first available
    * {@link Resource} to configure the EhCache CacheManager.
  • @return the config location {@link Resource} to use
    */
    public void setConfigLocations(final Resource[] configLocations) {

if (configLocations == null || configLocations.length < 0) { logger.info("No valid configLocations were set."); return; }

// uses the first available config location
super.setConfigLocation(configLocations[0]);
}



 All   Comments   Work Log   Change History   FishEye   Builds      Sort Order: Ascending order - Click to sort in descending order
Juergen Hoeller added a comment - 29/Nov/07 08:02 AM
How would the fallback locations kick in? Seems like your code simply sets the first location and ignores the rest...

Juergen


Josh Devins added a comment - 29/Nov/07 12:02 PM
The config locations are listed in order of priority, so the fallback occurs automatically (or so it seems). In the above example, if ehcache.override.xml does not exist, the first element in the array (to the property setter setConfigLocations) is META-INF/ehcache.default.xml. I'm using this code right now and it works as described. I assume that Spring doesn't populate the array with null Resources or Resources that don't exist. Otherwise, I would have just looped on the array and searched for the first Resource.exists() that was true. I believe this is the same or similar to how PropertyPlaceholderConfigurer works.

Josh Devins added a comment - 29/Nov/07 01:30 PM
A new version that extends EhCacheManagerFactoryBean and loops through the array, using the first available configuration.

Josh Devins added a comment - 29/Nov/07 01:31 PM
JUnit 4.4 test class (uses JMock) for EhCacheManagerFactoryBean subclass.

Josh Devins added a comment - 14/May/08 06:08 PM
Attached new versions (appended 2 to the filename). These have the additional capability of registering the Ehcache MBeans.