Java 如何在spring中使用外部属性禁用ehcache

Java 如何在spring中使用外部属性禁用ehcache,java,spring,ehcache,Java,Spring,Ehcache,我需要你的快速帮助来解决一个小问题 在我的一个项目中(使用spring作为核心容器),我使用ehcache来缓存数据。我正在使用springehcache注释项目(http://code.google.com/p/ehcache-spring-annotations/)同样的 我希望能够灵活地基于外部属性启用和禁用ehcache。我阅读了ehcache文档,发现它在内部读取系统属性net.sf.ehcache.disabled,如果将其设置为true,则将禁用缓存,他们建议在命令行中将其作为-D

我需要你的快速帮助来解决一个小问题

在我的一个项目中(使用spring作为核心容器),我使用ehcache来缓存数据。我正在使用springehcache注释项目(http://code.google.com/p/ehcache-spring-annotations/)同样的

我希望能够灵活地基于外部属性启用和禁用ehcache。我阅读了ehcache文档,发现它在内部读取系统属性net.sf.ehcache.disabled,如果将其设置为true,则将禁用缓存,他们建议在命令行中将其作为
-Dnet.sf.ehcache.disabled=true传递

我想通过外部化的spring属性文件来控制它

然后,我考虑在我的spring应用程序上下文文件中基于外部化属性使用MethodInvokingFactoryBean设置这个系统属性

这是密码

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject">
        <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
            <property name="targetClass" value="java.lang.System"/>
            <property name="targetMethod" value="getProperties"/>
        </bean>
    </property>
    <property name="targetMethod" value="putAll"/>
    <property name="arguments">
        <util:properties>
            <prop key="net.sf.ehcache.disabled">"${ehcache.disabled}"</prop>
        </util:properties>
    </property>
</bean>
我相信禁用缓存不会那么难


我错过了什么?在spring中,除了命令行之外,还有其他简单的方法吗?

从spring 3.1开始,可以使用bean配置文件从外部属性读取属性。您可以将声明的bean包装在bean元素中:

<beans profile="cache-enabled"> 
  <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml"/> 
</beans>

然后使用博客文章中提到的外部属性激活它。

最简单的方法(在Spring 3.1之前)是向MethodInvokingFactoryBean声明中添加一个bean id,然后添加依赖关系

<beans profile="cache-enabled"> 
  <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml"/> 
</beans>