使用 Spring 实现的 ehcache 配置 JMX?

Configure JMX with Spring implementation of ehcache?

我正在尝试按照文章 here & here 为 ehCache 实施 JMX。我的应用程序使用 Spring 实现,但是下面的配置条目出现此异常:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'managementService' defined in class path resource [trs.application.finance.businessactivites.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [net.sf.ehcache.CacheManager]: Could not convert constructor argument value of type [org.springframework.cache.ehcache.EhCacheCacheManager] to required type [net.sf.ehcache.CacheManager]: Failed to convert value of type 'org.springframework.cache.ehcache.EhCacheCacheManager' to required type 'net.sf.ehcache.CacheManager'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.cache.ehcache.EhCacheCacheManager] to required type [net.sf.ehcache.CacheManager]: no matching editors or conversion strategy found

spring 是否有我可以使用的 net.sf.ehcache.management.ManagementService 的实现?如果是这样,我该如何配置它,以便我可以在我的应用程序中为 ehcache 提供 JMX 支持。

注意,"managementService" & "mbeanServer" bean 是我添加的,用于尝试让 ehcache 注册到 jmx。 "myCacheManager" 已经存在于我的 spring 配置中并且似乎工作正常,就 ehcache 而言。

 <bean id="managementService" class="net.sf.ehcache.management.ManagementService"
      init-method="init"
      destroy-method="dispose">
    <constructor-arg ref="myCacheManager"/>
    <constructor-arg ref="mbeanServer"/>
    <constructor-arg index="2" value="true"/>
    <constructor-arg index="3" value="true"/>
    <constructor-arg index="4" value="true"/>
    <constructor-arg index="5" value="true"/>
</bean>
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
      <property name="locateExistingServerIfPossible" value="true"/>
</bean>
<bean id="myCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager">
        <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
            <property name="configLocation" value="classpath:ehcache.xml"/>
        </bean>
    </property>
</bean>

问题出在连接 bean 的方式上。

您不想在 managementService 中使用 myCacheManager,您确实想使用作为 myCacheManagercacheManager 返回和传递的 bean。

所以我建议移动以下内容:

<bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml"/>
</bean>

到顶级 bean,以便可以将其注入 managementService