【问题标题】:How to disable ehcache using an external property in spring如何在春季使用外部属性禁用ehcache
【发布时间】:2023-04-03 01:12:01
【问题描述】:

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

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

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

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

然后我想到在我的 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>

显然ehcache.disabled 是通过我的外部属性文件控制的。

管道工程很好,但我猜订单没有到位。到应用程序上下文设置系统属性时,缓存已初始化,并且在初始化缓存时没有属性net.sf.ehcache.disabled,因此缓存没有被禁用。当我在调试模式下运行应用程序并在应用程序上下文初始化后尝试获取System.getProperty("net.sf.ehcache.disabled") 时,它给了我正确的值。 (我尝试了真假)。

还有一件事我想提到我正在使用 spring 包装器来初始化我的缓存。

<ehcache:annotation-driven self-populating-cache-scope="method"/>

<ehcache:config cache-manager="cacheManager">
    <ehcache:evict-expired-elements interval="20"/>
</ehcache:config>

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

我相信禁用缓存不会那么难。

我错过了什么?除了命令行还有什么简单的方法可以在spring中实现吗?

【问题讨论】:

    标签: java spring ehcache


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        如果您的外部属性不正确,您可以返回 NoOpCacheManager 对象

            @Value("${cache.enabled}")
            private Boolean cacheEnabled;
        
            @Bean(destroyMethod="shutdown")
            public net.sf.ehcache.CacheManager ehCacheManager() {
        
                net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
        
                CacheConfiguration cacheConfiguration = new CacheConfiguration();
                cacheConfiguration.setName("mycache");
                cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
                cacheConfiguration.setMaxEntriesLocalHeap(1000);
                cacheConfiguration.setTimeToIdleSeconds(0);
                cacheConfiguration.setTimeToLiveSeconds(3600);
                cacheConfiguration.persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE));
        
                config.addCache(cacheConfiguration);
         cacheMaxEntriesLocalHeap, cacheEvictionPolicy);
                return net.sf.ehcache.CacheManager.newInstance(config);
        
            }
        
            @Bean
            @Override
            public CacheManager cacheManager() {
                if(cacheEnabled) {
                    log.info("Cache is enabled");
                    return new EhCacheCacheManager(ehCacheManager());
                }else{
                    log.info("Cache is disabled");
                    return new NoOpCacheManager();
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-07-18
          • 1970-01-01
          • 2010-11-20
          • 2018-06-26
          • 2017-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多