我想在ehcache.xml文件中放置属性占位符(如${}),以便在运行时可以从外部属性文件(.properties)中替换这些值。类似于:
ehcache.xml(在类路径中):
<defaultCache maxElementsInMemory="20000" eternal="false" timeToIdleSeconds="${default_TTI}" timeToLiveSeconds="86400" overflowToDisk="true" ... />
ehcache.properties(war/classpath之外):
... default_TTI=21600 ...
其目的是能够更改缓存配置,而无需重新构建应用程序。Spring的PropertyPlaceHolder将只与我不想要的ehcache的Springbean定义一起工作(需要将ehcache.xml作为一个文件)
这里也有类似的帖子,但没有什么能让我找到解决方案。我已经找了一个星期了!!
Im使用Spring 2.5.6、Hibernate 3.2.6和Ehcache 2.4.6
任何帮助或想法都将不胜感激!!
非常感谢,Tripti。
作为一个工作组解决方案,您可以将属性值设置为系统范围(system.setProperty(…))。EhCahe在解析其配置文件时使用这些属性来解析占位符。
我终于找到了解决方案!感谢勇敢的特里为我指明了方向。我在上下文启动时使用了这个:
Inputstream = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
cacheManager = CacheManager.create(stream);
结合hibernate配置:
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
这将从上下文类路径之外的ehcache.xml文件创建一个singleton CacheManager。我之前也做过同样的事情,但在这之前意外地使用类路径中的默认ehcache.xml创建了另一个CacheManager。
谢谢,Tripti。
svaor,我理解你的意思,我定义了一个这样的bean:
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.lang.System" />
<property name="targetMethod" value="setProperty" />
<property name="arguments">
<list>
<value>system.project_name</value>
<value>${system.project_name}</value>
</list>
</property>
</bean>
system.project_name在system.properties文件中定义,该文件位于类路径中
我还在classpath中创建了一个ehcache.xml,在ehcache.xml中有这样的代码:
<diskStore path="${java.io.tmpdir}/${system.project_name}/cache" />
但是当我部署我的项目时,我发现它不能使用system.properties中定义的system.project_name,为什么
如果您只想在启动时从磁盘读取配置,您可以在EHCache 2.5中执行以下操作:
InputStream fis =
new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
try
{
CacheManager manager = new CacheManager(fis);
}
finally
{
fis.close();
}