java.lang.NoSuchMethodError: org.springframework.cache.ehcache.EhCacheFactoryBean.setMaxEntriesLocal

来源:互联网 发布:数据库简答题 编辑:程序博客网 时间:2024/06/05 15:51

遇到的问题:Spring Boot工程,引入ehCache作为2级缓存,在eclipse中运行main方法启动工程没有问题,但是在命令行中运行 mvn spring-boot:run时,却出错了,报错如下:

...org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheFactoryBean' defined in class path resource [com/xxxx/xxxx/CacheConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.ehcache.EhCacheFactoryBean]: Factory method 'ehCacheFactoryBean' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.cache.ehcache.EhCacheFactoryBean.setMaxEntriesLocalHeap(J)V...
来看看工程中的代码:

@Autowiredprivate EhCacheFactoryBean ehCacheFactoryBean;
我在实现缓存的类中自动注入了一个EhCacheFactoryBean,接着看看EhCacheFactoryBean的源码:
public class EhCacheFactoryBean extends CacheConfiguration implements FactoryBean<Ehcache>, BeanNameAware, InitializingBean {  ...}
也就是说EhCacheFactoryBean是CacheConfiguration的一个子类,在Spring 注入Bean的时候,先注入父类,再注入子类,而且错误提示

Error creating bean with name 'ehCacheFactoryBean' defined in class path resource [com/xxxx/xxxx/CacheConfiguration.class]
说明Spring没有把CacheConfiguration注到容器中,那么问题来了

1.CacheConfiguration为什么没有被注入?CacheConfiguration位于 net.sf.ehcache.config包下,这样很有可能是系统中存在两个CacheConfiguration类,导致了冲突,所以注入失败,在查看maven引入的jar包时,发现了一个ehcache-2.10.2.jar和ehcache-core-2.4.3.jar,再看包里面的内容时,发现都有CacheConfiguration类,所以必然是同名的类导致了冲突。



2.为什么会引入了两个ehcache的包?接着就去查看了pom,原来是在引入hibernate二级缓存依赖时引入了ehcache-core包:

        <dependency>              <groupId>net.sf.ehcache</groupId>              <artifactId>ehcache</artifactId>          </dependency>          <dependency>      <groupId>org.hibernate</groupId>      <artifactId>hibernate-ehcache</artifactId></dependency>


解决办法就是把ehcache-core包排除掉就可以了

        <dependency>              <groupId>net.sf.ehcache</groupId>              <artifactId>ehcache</artifactId>          </dependency>           <dependency>  <groupId>org.hibernate</groupId>  <artifactId>hibernate-ehcache</artifactId>          <exclusions>              <exclusion>          <groupId>net.sf.ehcache</groupId>          <artifactId>ehcache-core</artifactId>              </exclusion>          </exclusions></dependency>


但是为什么在有jar包冲突的情况下,在eclipse下运行会不报错,在命令行下执行会报错呢?小白坐等大神的解答呀。



阅读全文
2 0
原创粉丝点击