【问题标题】:Externarlize ehcache.xml to use properties from external properties fileExternarlize ehcache.xml 以使用来自外部属性文件的属性
【发布时间】:2012-04-08 10:39:33
【问题描述】:

我想在 ehcache.xml 文件(如 ${})中放置属性占位符,以便可以在运行时从外部属性文件 (.properties) 替换值。 比如:

ehcache.xml(在类路径中):

 <defaultCache
maxElementsInMemory="20000"
eternal="false"
timeToIdleSeconds="${default_TTI}"
timeToLiveSeconds="86400"
overflowToDisk="true"
... />

ehcache.properties(在战争/类路径之外):

...
default_TTI=21600
...

目的是能够更改缓存配置而无需重新构建应用程序。 Spring 的 PropertyPlaceHolder 仅适用于我不想要的 ehcache 的 Spring bean 定义(需要将 ehcache.xml 保留为文件)

这里有类似的帖子,但没有让我找到解决方案。我已经找了一个星期了!!

我正在使用 Spring 2.5.6、Hibernate 3.2.6 和 Ehcache 2.4.6

任何帮助或想法都非常感谢!

非常感谢, Tripti。

【问题讨论】:

    标签: java spring hibernate ehcache second-level-cache


    【解决方案1】:

    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,为什么?

    【讨论】:

      【解决方案2】:

      我终于找到了解决方案!感谢勇敢者为我指出了那个方向。 我在上下文启动时使用了这个:

      Inputstream = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath()); 
      cacheManager = CacheManager.create(stream);
      

      结合休眠配置:

      <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
      

      这会从上下文类路径之外的 ehcache.xml 文件创建一个单例 CacheManager。我之前也这样做过,但在使用类路径中的默认 ehcache.xml 之前意外地创建了另一个 CacheManager。

      谢谢, Tripti。

      【讨论】:

        【解决方案3】:

        作为一种临时解决方案,您可以将属性值设置为系统范围 (System.setProperty(...))。 EhCahe 在解析其配置文件时使用这些属性来解析占位符。

        【讨论】:

        • 可悲的是,ehcache 仅包含一个简单的属性名称替换,即使在 3.8.0 中也是如此。我的意思是其他系统中的配置允许使用许多属性名称甚至默认值,例如${global.property,subsystem.property:100000} .
        【解决方案4】:

        如果你只是想在启动时read the config in from disk,你可以在 EHCache 2.5 中执行以下操作:

        InputStream fis = 
            new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
        try 
        {
          CacheManager manager = new CacheManager(fis);
        } 
        finally 
        {
          fis.close();
        }
        

        【讨论】:

        • 抱歉输入太早了!我也尝试过这样做,但问题是如何将这个 CacheManager 挂接到休眠状态?我的休眠上下文文件中有以下代码:&lt;prop key="hibernate.cache.use_second_level_cache"&gt;true&lt;/prop&gt;&lt;prop key="hibernate.cache.provider_class"&gt;net.sf.ehcache.hibernate.SingletonEhCacheProvider&lt;/prop&gt; 所以休眠开始使用默认的 ehcache.xml(在 jar 中可用)并使用它而不是使用我创建的 CacheManger 创建缓存。有什么办法可以告诉休眠使用我的 CacheManager 而不是创建新的?谢谢,特里普蒂。
        猜你喜欢
        • 2018-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-27
        • 2013-06-30
        • 2015-05-17
        • 2013-08-12
        • 2021-08-16
        相关资源
        最近更新 更多