CacheBuilder中的NoSuchMethodError(checkState)

时间:2016-10-28 12:21:28

标签: java caching java-ee guava

我创建了一个Guava(一个包含有用东西的库,在几乎任何Java项目中都非常方便,包括用于不可变集合的库,函数式编程,I / O等等)基于CacheBuilder的缓存

 LoadingCache<String, Byte[]> companyDevicesCache = 
             CacheBuilder.newBuilder()
                .maximumSize(1000)                     // maximum 100 records can be cached
                .expireAfterAccess(24, TimeUnit.HOURS) // cache will expire after 30 minutes of access
                .build(new CacheLoader<String, Byte[]>(){ // build the cacheloader

                   @Override
                   public Byte[] load(String companyId) throws Exception {
                      //make the expensive call
                      return getFromServletContext (companyId);
                   }


                });

我也试过

CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES)
         .maximumSize(1000)
         .build(new CacheLoader<String, List<String>>() {
             @Override
             public List<String> load(String queryKey) throws Exception {
                 return null;
             }
         });

以及

CacheLoader loader = new CacheLoader()
            {
                public String load(String key) throws Exception
                {
                    return null;
                }

                @Override
                public Object load(Object arg0) throws Exception {
                    // TODO Auto-generated method stub
                    return null;
                }
            };

         LoadingCache<String, String> persons = CacheBuilder.newBuilder() 
                 .initialCapacity(30) 
                 .maximumSize(40) 
                 .recordStats() 
                 .build(loader);

final LoadingCache<String, Optional<Product>> cache =


               CacheBuilder.newBuilder()
                   .expireAfterWrite(1, TimeUnit.DAYS)
                   .refreshAfterWrite(1, TimeUnit.SECONDS)
                   .build( new CacheLoader<String, Optional<Product>>() {
                         @Override
                         public Optional<Product> load( String productId ) throws Exception {
                             return null;
                         }
                   }
             );

但是当我初始化CacheBuilder时,我收到了这个错误:

]] Root cause of ServletException.
java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;J)V
        at com.google.common.cache.CacheBuilder.maximumSize(CacheBuilder.java:409)

这是图书馆的版本:guava-20.0-rc1.jar

这里包含的耳朵

enter image description here

我还试图替换这个guava-19.0.jar的库,但后来我得到了

]] Root cause of ServletException.
java.lang.IncompatibleClassChangeError: Found interface com.google.common.base.Equivalence, but class was expected
        at com.google.common.cache.LocalCache$Strength$1.defaultEquivalence(LocalCache.java:393)
        at com.google.common.cache.CacheBuilder.getKeyEquivalence(CacheBuilder.java:297)
        at com.google.common.cache.LocalCache.<init>(LocalCache.java:246)
        at com.google.common.cache.LocalCache$LocalLoadingCache.<init>(LocalCache.java:4868)
        at com.google.common.cache.CacheBuilder.build(CacheBuilder.java:786)

1 个答案:

答案 0 :(得分:6)

看起来你在类路径上有com.google.common.base.Preconditions类的多个版本(可能来自一个旧的google-collections jar?),或者是一个损坏的jar。

找不到的method overloadadded in Guava 20.0

public static void checkState(boolean b, @Nullable String errorMessageTemplate, long p1)

基于NoSuchMethodError

中的签名
checkState(ZLjava/lang/String;J)V
           ||                 | |
           ||                 | returns void
           ||                 |
           ||                 long
           |String
           |
           boolean

您可以在致电CacheBuilder.maximumSize()之前添加以下代码,找到您的课程定义来自哪里:

System.out.println(com.google.common.base.Preconditions.class
        .getProtectionDomain()
        .getCodeSource()
        .getLocation()
        .toExternalForm());

您将获得jar的URL。

我从Maven Central下载了guava-20.0-rc1.jar,提取了内容,并使用Preconditions.class检查了javap的内容,方法肯定在那里:

$ javap Preconditions.class
Compiled from "Preconditions.java"
public final class com.google.common.base.Preconditions {
  // ...
  public static void checkState(boolean, java.lang.String, long);
  // ...
}

在您的ant片段中,有多个通配符包含,例如:

<include name="**/spring/*.jar"/>

可能还有一个额外版本的Guava,特别是考虑到切换到guava-19.0时遇到的其他错误:Equivalence是一个interface直到9.0,并在{{3对于10.0的2011