简体   繁体   English

使用通用DAO时出现NoSuchMethodError

[英]NoSuchMethodError while using Generic DAO

I'm trying to use GenericDAO in a project : 我正在尝试在项目中使用GenericDAO:

public class GenericDao<T> {
    ApplicationContext ctx =
            new AnnotationConfigApplicationContext(SpringMongoConfig.class);
    MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");

    public void save(T t) {
        mongoOperation.save(t);
    }

    public void delete(final T t) {
        mongoOperation.remove(t);
    }
}

The child class NoteDAO extends GenericDao and there's no need to override save/delete methods. 子类NoteDAO扩展了GenericDao,无需覆盖保存/删除方法。 But when I'm trying to use them I get: java.lang.NoSuchMethodError: com.example.dao.NoteDAO.save(Lcom/example/model/Note;)V 但是当我尝试使用它们时,我得到: java.lang.NoSuchMethodError: com.example.dao.NoteDAO.save(Lcom/example/model/Note;)V

Here is my NoteDAO, it's empty: 这是我的NoteDAO,它为空:

public class NoteDAO extends GenericDao<Note> {
    public static Logger LOG = Logger.getLogger(NoteDAO.class);
    public static int counter = 0;
}

Why doesn't it work? 为什么不起作用?

解决该问题的一种可能的方法是在NoteDAO中添加一个无参数的构造函数。

I tried to replicate your scenario and it worked for me... See if you have some kind of code: 我试图复制您的方案,它对我有用...请看您是否有某种代码:

package com.x.y;

public class GenericDAO<T> {

   public void save(T t) {
       System.out.println("Generic Save");
   }

   public void delete(final T t) {
      System.out.println("Generic Delete");
   }
}

package com.x.y;

public class NoteDAO extends GenericDAO<Note> {

   public void persist(){
      save(new Note());
   }

}

package com.x.y;

public class Client {
   public static void main(String[] args) {
    NoteDAO nDao = new NoteDAO();
    nDao.save(new Note());   
   }
}

So, when I run Client it prints: "Generic Save". 因此,当我运行Client时,它会打印:“ Generic Save”。

Just trying to help you. 只是想帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM