我在Spring框架中使用ehcache。我正在使用 ehcache.xml 来初始化 ehcache。但是我想在运行时添加某些属性,例如 terracottaconfig。为此,我重写了 EhCacheManagerFactoryBean 类。目前我正在重写此类的 getObject() 方法(我不知道这是否是正确的重写方法,因为在使用 ehcache.xml 文件初始化类之前调用了其他方法 setResource() 和 afterPropertiesSet() 。)
这是我的 spring 配置文件
<bean id="cacheManager"
class="com.dexknows.util.CustomEhCacheManagerFactoryBean">
<property name="configLocation">
<value>file:///${vp_data_dir}/ehcache.xml</value>
</property>
<property name="terracottaConfigUrl" value="#{dexProperties['terracottaConfig.Url']}"/>
</bean>
这是我重写的类方法
public class CustomEhCacheManagerFactoryBean extends EhCacheManagerFactoryBean {
private String terracottaConfigUrl;
private Resource resourceConfiguration;
@Override
public void setConfigLocation(Resource configLocation) {
super.setConfigLocation(configLocation);
}
@Override
public void afterPropertiesSet() throws IOException, CacheException {
super.afterPropertiesSet();
}
@Override
public CacheManager getObject() {
CacheManager manager = super.getObject();
TerracottaClientConfiguration terracottaClientConfiguration = new TerracottaClientConfiguration();
terracottaClientConfiguration.setRejoin(true);
terracottaClientConfiguration.setUrl(terracottaConfigUrl);
manager.getConfiguration().setDynamicConfig(true);
manager.getConfiguration().terracotta(terracottaClientConfiguration);
Configuration().terracotta(terracottaClientConfiguration)));
return manager;
}
public String getTerracottaConfigUrl() {
return terracottaConfigUrl;
}
public void setTerracottaConfigUrl(String terracottaConfigUrl) {
this.terracottaConfigUrl = terracottaConfigUrl;
}
}
我遇到以下异常:
创建名称为“cacheManager”的 bean 时出错:FactoryBean 在创建对象时抛出异常;嵌套异常为 java.lang.IllegalStateException: net.sf.ehcache.config.Configuration.dynamicConfig 无法动态更改
我设置了dynamicConfig="true"
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false"
monitoring="autodetect"
dynamicConfig="true"
name="xyz">
最佳答案
分析EhCacheManagerFactoryBean类的源代码后,我发现'terracottaConfigConfiguration'不能动态更改。 只有枚举 DynamicProperty 中提到的属性可以动态更改。不过,如果有人找到文档并提到相同的内容,那将会很有帮助。
关于java - 运行时修改Ehcache配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11441606/