Question

There is this tag <terracottaConfig url="host1:9510,host2:9510,host3:9510"/> in ehcache.xml file inside spring web application. I want to externalize url attribute of this tag. Value of URL should be replaced by a property from external file. It will be very helpful if you suggest any solution to this problem.

Was it helpful?

Solution

You can put something like this - <terracottaConfig url="${terracotta.config.location}" /> , however the big catch is that this will be loaded only from the system properties. It is not resolved from PropertyPlaceHolder as it is not a Spring configuration file.

So if you want to use an external config file, you will basically have to programatically set this system property before the Spring Application starts loading up the ehcache.xml file - one way to do that will be write your custom ServletContextListener to load up your properties file and set the system property based on that, this way when the ehcache.xml is loaded up it would be able to resolve the place holder properly.


Your answer helped me to solve my problem. I just want to add that instead of setting system property through program, I am using util:properties as follows

<bean id="sysProperties" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
       <property name="targetObject" value="#{@systemProperties}"/>
       <property name="targetMethod" value="putAll"/>
       <property name="arguments">
           <util:properties>
               <prop key="propertyname_used_in_ecache_xml">#{proerties_defined_using_property_factory['propertyname_defined_in_external_properties_file']}</prop>
           </util:properties>
       </property>
    </bean>

    <bean id="cacheManager"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" depends-on="sysProperties">
        <property name="configLocation">
            <value>classpath:ehcache.xml</value>
        </property>
    </bean>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top