Spring 使用注释将EhCache XML转换为Java类并在JMX中注册

Spring 使用注释将EhCache XML转换为Java类并在JMX中注册,spring,ehcache,mbeans,Spring,Ehcache,Mbeans,在我们的项目中,我们有两个模块:api和api组件,每个模块都有一个cache.xml文件,该文件引用另一个ehcache.xml文件,如下所示: cache.xml(在两个模块中) ehcache.xml(在两个模块中) ​ 为了尝试使用注释将这些XML文件转换为Java类,我提出了这些配置,同样在两个模块中:ApiCacheConfiguration.Java和ApiComponentCacheConfiguration.Java import net.sf.ehcache.Cach

在我们的项目中,我们有两个模块:api和api组件,每个模块都有一个cache.xml文件,该文件引用另一个ehcache.xml文件,如下所示:

cache.xml(在两个模块中)


ehcache.xml(在两个模块中)

为了尝试使用注释将这些XML文件转换为Java类,我提出了这些配置,同样在两个模块中:ApiCacheConfiguration.Java和ApiComponentCacheConfiguration.Java

import net.sf.ehcache.Cache;
导入net.sf.ehcache.config.CacheConfiguration;
导入net.sf.ehcache.config.DiskStoreConfiguration;
导入net.sf.ehcache.management.ManagementService;
导入org.springframework.cache.CacheManager;
导入org.springframework.cache.annotation.CachingConfigurerSupport;
导入org.springframework.cache.annotation.EnableCaching;
导入org.springframework.cache.ehcache.ehcachemanager;
导入org.springframework.cache.interceptor.*;
导入org.springframework.cache.support.CompositeCacheManager;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.context.annotation.Primary;
导入org.springframework.jmx.support.MBeanServerFactoryBean;
导入java.util.Collections;
@配置
@启用缓存
公共类ApicheConfigureConfiguration扩展了CachingConfigurerSupport{
@初级的
@豆子
公共缓存管理器缓存管理器(){
CompositeCacheManager CompositeCacheManager=新CompositeCacheManager();
setCacheManager(Collections.singleton(ehCacheManager());
返回复合化学管理器;
}
@豆子
公共ehCacheManager ehCacheManager(){
ehcachemanager cacheManager=新的ehcachemanager(ehcachemanager());
ManagementService.registerMBeans(
cacheManager.getCacheManager(),
mBeanServer().getObject(),
是的,
是的,
是的,
正确的);
返回缓存管理器;
}
@豆子
公共MBeanServerFactoryBean mBeanServer(){
MBeanServerFactoryBean MBeanServerFactoryBean=新MBeanServerFactoryBean();
mBeanServerFactoryBean.setLocateExistingServerIfEnable(true);
返回mBeanServerFactoryBean;
}
@豆子
@凌驾
公共CacheResolver CacheResolver(){
返回新的SimpleCacheSolver(cacheManager());
}
@豆子
@凌驾
公钥生成器KeyGenerator(){
返回新的SimpleKeyGenerator();
}
@豆子
@凌驾
公共CacheErrorHandler errorHandler(){
返回新的SimpleCacherRorHandler();
}
private net.sf.ehcache.CacheManager ehcachemanager(){
net.sf.ehcache.CacheManager CacheManager=new net.sf.ehcache.CacheManager(ehCacheConfiguration());
cacheManager.setName(“ApiCacheManager”);//如果未设置名称,则不会获得“InstanceAlreadyExistsException”
addCache(myCache());
返回缓存管理器;
}
私有缓存myCache(){
CacheConfiguration=newcacheconfiguration(“myCache”,100)//每个文件中有不同的名称
.永恒(错)
.TimeToIdles(600)
.timeToLiveSeconds(3600)
.overflowToDisk(false)
.统计数字(真实);
返回新缓存(配置);
}
private net.sf.ehcache.config.Configuration ehCacheConfiguration(){
DiskStoreConfiguration DiskStoreConfiguration=新的DiskStoreConfiguration();
setPath(“java.io.tmpdir”);
CacheConfiguration defaultCacheConfiguration=新的CacheConfiguration();
defaultCacheConfiguration.maxElementsInMemory(1000);
defaultCacheConfiguration.external(true);
defaultCacheConfiguration.overflowToDisk(false);
net.sf.ehcache.config.Configuration=new net.sf.ehcache.config.Configuration();
setDefaultCacheConfiguration(defaultCacheConfiguration);
配置.addDiskStore(diskStoreConfiguration);
返回配置;
}
}
一切都与前面的ehCache.xml中所写的差不多,但我不知道如何创建ApiCacheManager和ApicComponentCacheManager的多个bean,并抛出以下错误:

原因:javax.management.instancealreadyexistException:net.sf.ehcache:type=CacheManager,name=apichemanager
原因:javax.management.instancealreadyexistException:net.sf.ehcache:type=CacheManager,name=ApiComponentCacheManager
如果未设置CacheManager名称,但在JMX中注册了多个bean,则不会出现此错误:

如何仅将我的两个CacheManager与我设置的名称一起放入JMX? CacheManager的创建是否有问题?也许不是在创造一个单身汉