我在 Spring 框架中使用 ehcache。我正在使用 ehcache.xml 来初始化 ehcache。但是我想在运行时添加某些属性,例如 terracottaconfig。为此,我重写了 EhCacheManagerFactoryBean 类。目前我正在重写这个类的 getObject() 方法(我不知道这是否是一个正确的重写方法,因为在使用 ehcache.xml 文件初始化类之前调用了其他方法 setResource() 和 afterPropertiesSet()。)
这是我的弹簧配置文件
<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">