Skip to content

Instantly share code, notes, and snippets.

@vinaynair
Created September 2, 2014 20:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinaynair/ceb3a1b0284c90848044 to your computer and use it in GitHub Desktop.
Save vinaynair/ceb3a1b0284c90848044 to your computer and use it in GitHub Desktop.
# see ehcache.xml for how these properties are used
cachemanager.name=cacheManagerOne
cache.name=cacheOne
cache.local.heap=1000
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- standard spring property configurator with overrides-->
<context:property-placeholder location="classpath:app.properties,app-override.properties"
ignore-resource-not-found="true"
ignore-unresolvable="true"
order="1"/>
<!-- load specific properties relevant to ehcache configuration into java.lang.System
See ehcache.xml for -->
<bean id="ehcacheSystemProperties"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" value="#{@systemProperties}"/>
<property name="targetMethod" value="putAll"/>
<property name="arguments">
<util:properties>
<prop key="cachemanager.name">${cachemanager.name}</prop>
<prop key="cache.name">${cache.name}</prop>
<prop key="cache.local.heap">${cache.local.heap}</prop>
</util:properties>
</property>
</bean>
<!-- EhCache Manager. Note that this bean has to depend on the system properties setter bean-->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml" depends-on="ehcacheSystemProperties"/>
<!-- EhCache with the name provided within the app.properties. Note that this bean is injected into the Test class -->
<bean id="cache" class="net.sf.ehcache.Cache" factory-bean="cacheManager" factory-method="getCache">
<constructor-arg value="${cache.name}"/>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="${cachemanager.name}" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<!-- configuring cache from spring app.properties or its env override -->
<cache name="${cache.name}" maxEntriesLocalHeap="${cache.local.heap}"/>
</ehcache>
import net.sf.ehcache.Cache;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
/**
* Created by vinay on 9/2/14.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:appContext.xml"})
public class SpringEhCachePropertyLoadTest {
@Resource
Cache cache;
@Test
public void testEhCachePropertyIsSetFromSpringAppPropertiesFile() {
Assert.assertNotNull(cache);
Assert.assertEquals("cacheOne",cache.getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment