2016-11-04 84 views
0

我想監視與JMX MBean的春天緩存/ ehcache的,所以我創建了以下豆類:ClassCastException異常:EhCacheCacheManager不能轉換到CacheManager的

import java.lang.management.ManagementFactory; 
import java.lang.management.ManagementFactory; 
import java.util.List; 

import javax.management.MBeanServer; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.cache.CacheManager; 
import org.springframework.cache.annotation.EnableCaching; 
import org.springframework.cache.ehcache.EhCacheCacheManager; 
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean; 
import org.springframework.context.MessageSource; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.ComponentScan.Filter; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.DependsOn; 
import org.springframework.context.annotation.FilterType; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.context.annotation.PropertySources; 
import org.springframework.context.support.ResourceBundleMessageSource; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.data.web.config.EnableSpringDataWebSupport; 
import org.springframework.format.FormatterRegistry; 
import org.springframework.http.converter.ByteArrayHttpMessageConverter; 
import org.springframework.http.converter.HttpMessageConverter; 
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; 
import org.springframework.validation.Validator; 
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; 
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; 
import org.springframework.web.method.support.HandlerMethodArgumentResolver; 
import org.springframework.web.method.support.HandlerMethodReturnValueHandler; 
import org.springframework.web.multipart.MultipartResolver; 
import org.springframework.web.multipart.support.StandardServletMultipartResolver; 
import org.springframework.web.servlet.HandlerExceptionResolver; 
import org.springframework.web.servlet.ViewResolver; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
import org.springframework.web.servlet.view.InternalResourceViewResolver; 

import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module; 
import com.google.common.collect.Lists; 

import net.sf.ehcache.management.ManagementService; 

@EnableWebMvc 
@EnableSpringDataWebSupport 
@EnableCaching 
@Configuration 
public class SpringWebConfig extends WebMvcConfigurerAdapter { 

    ... 

    @Bean 
    public CacheManager ehCacheManager() { 
     EhCacheCacheManager cacheManager = new EhCacheCacheManager(); 
     cacheManager.setCacheManager((net.sf.ehcache.CacheManager) ehCacheManagerFactory().getObject()); 

     return cacheManager; 
    } 

    @Bean 
    public EhCacheManagerFactoryBean ehCacheManagerFactory() { 
     EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean(); 
     cmfb.setConfigLocation(new ClassPathResource("ehcache.xml")); 
     cmfb.setShared(true); 
     return cmfb; 
    } 

    @Bean 
    public ManagementService managementService() { 
     return new ManagementService((net.sf.ehcache.CacheManager) ehCacheManager(), mBeanServer(), true, true, true, true); 
    } 


    @Bean 
    public MBeanServer mBeanServer() { 
     MBeanServer bean = ManagementFactory.getPlatformMBeanServer(); 
     return bean; 
    } 
} 

我POM是:

<dependency> 
     <groupId>net.sf.ehcache</groupId> 
     <artifactId>ehcache-core</artifactId> 
     <version>2.6.11</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>4.3.2.RELEASE</version> 
     <exclusions> 
      <!-- Exclude Commons Logging in favor of SLF4j --> 
      <exclusion> 
       <groupId>commons-logging</groupId> 
       <artifactId>commons-logging</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 

但是每次我得到一個錯誤:

ERROR 2016-11-04 15:07:55,827 org.springframework.web.servlet.DispatcherServlet - Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'managementService' defined in pl.softwarehut.config.SpringWebConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [net.sf.ehcache.management.ManagementService]: Factory method 'managementService' threw exception; nested exception is java.lang.ClassCastException: org.springframework.cache.ehcache.EhCacheCacheManager cannot be cast to net.sf.ehcache.CacheManager 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) 
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668) 
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:540) 
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494) 
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) 
    at javax.servlet.GenericServlet.init(GenericServlet.java:158) 
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1194) 
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1003) 
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4921) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5231) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152) 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) 
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:596) 
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1805) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [net.sf.ehcache.management.ManagementService]: Factory method 'managementService' threw exception; nested exception is java.lang.ClassCastException: org.springframework.cache.ehcache.EhCacheCacheManager cannot be cast to net.sf.ehcache.CacheManager 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) 
    ... 31 more 
Caused by: java.lang.ClassCastException: org.springframework.cache.ehcache.EhCacheCacheManager cannot be cast to net.sf.ehcache.CacheManager 

你知道爲什麼嗎?如何解決它?

+0

爲什麼在'Public ManagementService managementService()'中執行這樣的強制轉換'(net.sf.ehcache.CacheManager)ehCacheManager()'? 'EhCacheCacheManager'通過'org.springframework.cache.ehcache.EhCacheCacheManager#getCacheManager'包含對'net.sf.ehcache.CacheManager'的引用。 – pleft

回答

1

如果改變這一行:

@Bean 
public ManagementService managementService() { 
    return new ManagementService((net.sf.ehcache.CacheManager) ehCacheManager(), mBeanServer(), true, true, true, true); 
} 

@Bean 
public ManagementService managementService() { 
    return new ManagementService(ehCacheManager().getCac‌​heManager(), mBeanServer(), true, true, true, true); 
} 

應該解決您的偏色問題。

相關問題