Spring context property-placholder ehcahe configuration

ehcache, java, spring

Solution

Your example uses `EhCacheManagerFactoryBean` to expose a reference to the `CacheManager`, with caches defined in the external `cacheConfig.xml` file. As @ChssPly76 pointed out, Spring's property resolver only works within Spring's own bean definition files.

However, you don't have to define the individual caches in the external file, you can define them right within the Spring bean definition file, using `EhCacheFactoryBean`:

FactoryBean that creates a named EHCache Cache instance... If the specified named cache is not configured in the cache configuration descriptor, this FactoryBean will construct an instance of a Cache with the provided name and the specified cache properties and add it to the CacheManager for later retrieval.

In other words, if you use `EhCacheFactoryBean` to refer to a named cache that isn't already defined in `cacheConfig.xml`, then Spring will create and configure a new cache instance and register it with the `CacheManager` at runtime. That includes specifying things like `maxElementsInMemory`, and because this would be specified in the Spring bean definition file, you get full support of the property resolver:

<context:property-placeholder location="classpath:cacheConfig.properties"/>

<bean id="myCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
    <property name="cacheManager" ref="cacheManager"/>
    <property name="maxElementsInMemory" value="${cache.maxMemoryElements}"/>
</bean>

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="shared" value="false"/>
    <property name="configLocation" value="classpath:cacheConfig.xml"/>
</bean>

Problem

I have a spring context xml file with this ``` <context:property-placeholder location="classpath:cacheConfig.properties"/> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="cacheManagerName" value="cacheName"/> <property name="shared" value="false"/> <property name="configLocation" value="classpath:cacheConfig.xml"/> </bean> ``` the goal is to allow the customer to edit the properties file, like this ``` cache.maxMemoryElements="2000" ``` and then in the actual cacheConfig.xml file have this ``` <cache name="someCacheName" maxElementsInMemory="${cache.maxMemoryElements}" ... /> ``` so that items we do not want the customer to change are not exposed. Of course the above details are only partially detailed and NOT working. Currently I see this in the log file ``` Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Error configuring from input stream. Initial cause was null:149: Could not set attribute "maxElementsInMemory". ``` Thanks in advance...

Original source