Spring ehcache no such method error

我的未来我决定 提交于 2019-12-20 02:38:17

问题


Caused by: java.lang.NoSuchMethodError: org.springframework.cache.ehcache.EhCach
eFactoryBean.setMaxEntriesLocalHeap(J)V


    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.ehcache.EhCacheFactoryBean]:
     Constructor threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework
    .cache.ehcache.EhCacheFactoryBean.setMaxEntriesLocalHeap(J)V
            at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
            at org.springframework.beans.factory.support.SimpleInstantiationStrategy
    .instantiate(SimpleInstantiationStrategy.java:89)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBean
    Factory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1086)
            ... 192 more
    Caused by: java.lang.NoSuchMethodError: org.springframework.cache.ehcache.EhCach
    eFactoryBean.setMaxEntriesLocalHeap(J)V
            at org.springframework.cache.ehcache.EhCacheFactoryBean.<init>(EhCacheFa
    ctoryBean.java:101)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

            at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct
    orAccessorImpl.java:62)
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC
    onstructorAccessorImpl.java:45)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
            at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:1
    47)
            ... 194 more

回答1:


Error seems to be quite clear

Version 2.5 that you use does not have the method.

Version 4.1 of EhCacheFactoryBean seems to have the method.

I guess you have mixed incompatible versions of jars.




回答2:


The method setMaxEntriesLocalHeap used in EhCacheFactoryBean is in the father class CacheConfiguration, you can see the source code:

public class EhCacheFactoryBean extends CacheConfiguration implements FactoryBean<Ehcache>, BeanNameAware, InitializingBean { }

it's not found maybe caused by CacheConfiguration hadn't been auto wired to the bean factory, you can check this.




回答3:


There should be a version mismatch in the lib you are trying to use. Rip down the jar and using jdgui see if the class is available.

The issue here is the lib you are using in the module and the one you are referring are different hence the exception.




回答4:


Jar files required :- spring-security 2.0.3 and spring 2.5.5 and echache 1.2.4 ehcache config

 <ehcache>
 <diskStore path="java.io.tmpdir"/>
 <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/>
</ehcache>

Spring config:

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheMa nagerFactoryBean">
<property name="configLocation" value="classpath:ehcache-failsafe.xml" />
</bean>
<bean id="userCacheBackend" class="org.springframework.cache.ehcache.EhCacheFa ctoryBean">
<property name="cacheManager" ref="cacheManager" />
<property name="cacheName" value="userCache" />
</bean>
<bean id="userCache" class="org.springframework.security.providers.dao. cache.EhCacheBasedUserCache">
<property name="cache" ref="userCacheBackend" />
</bean>



回答5:


Use mvn dependency:tree and check your versions of org.springframework:spring-context-support:jaris newer than 3.2.13.RELEASE.

If so that means you need to use the EhCache in version 2.5 and newer. If you cannot, for example because you are using the hibernate-ehcache which requires 2.4.x ehCache then the only way is to use the old spring-context-support:

 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>3.2.13.RELEASE</version><!-- wersja wymagana przez ehcache-core 2.4.8-->
    <exclusions>
      <exclusion>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
      </exclusion>
      <exclusion>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
      </exclusion>
      <exclusion>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
      </exclusion>
      <exclusion>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
      </exclusion>
      <exclusion>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
      </exclusion>
    </exclusions>
  </dependency>

It should work with the newer spring. I've exclude the dependencies to prevent downloading the old versions of them.



来源:https://stackoverflow.com/questions/28167845/spring-ehcache-no-such-method-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!