java.lang.NoSuchMethodException 与构造函数 newInstance

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12404963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:46:01  来源:igfitidea点击:

java.lang.NoSuchMethodException with Constructor newInstance

javawebfront-controller

提问by MartinElvar

I am currently working on some web dev project in Java, i have implemented a frontcontroller, which job is to instantiate new controllers, depending on the path.

我目前正在用 Java 开发一些 Web 开发项目,我已经实现了一个前端控制器,它的工作是根据路径实例化新的控制器。

So when the user is running ?q=user/login ex. the front controller should instatiate the UserController, that i am trying to do with this piece of code.

所以当用户运行 ?q=user/login ex 时。前端控制器应该设置 UserController,我正在尝试使用这段代码。

    String q = request.getParameter("q");

    try {
        String[] page = q.split("/");
        // Make first char upper, to match class name conventions.
        page[0] = (page[0].substring(0, 1).toUpperCase() + page[0].substring(1).toLowerCase()).trim();

      Class contDes = Class.forName("dk.elvar.rocks." + page[0]+ "Controller");
      Constructor co = contDes.getConstructor();
      co.newInstance(request, response, page);

This results in a

这导致一个

java.lang.NoSuchMethodException: dk.elvar.rocks.UserController.<init>()
at java.lang.Class.getConstructor0(Class.java:2706)
at java.lang.Class.getConstructor(Class.java:1657)
at dk.elvar.rocks.FrontController.doGet(FrontController.java:35)

I've tryed to look it up at google, and bugs as, declaring a constructor in loaded object, make the class public, is already there.

我试图在谷歌上查找它,并且错误已经存在,在加载的对象中声明一个构造函数,使类公开。

UserController:

用户控制器:

public class UserController extends HttpServlet  {

private final String USERNAME = "Martin";
private final String PASSWORD = "David";

    private static final long serialVersionUID = 1L;
HttpServletRequest request;  
HttpServletResponse response;

public UserController(HttpServletRequest request, HttpServletResponse response, String[] action)  {
    this.request = request;
    this.response = response;

    if(action[1].equalsIgnoreCase("login")) {
        this.renderLoginAction();
    }
    if(action[1].equalsIgnoreCase("val-login")) {
        this.validateLoginAction();
    }
}

回答by assylias

You probably get this exception because that class does not have a default constructor. You can get a constructor with parameters by passing them to the getConstructormethod:

您可能会收到此异常,因为该类没有默认构造函数。您可以通过将参数传递给getConstructor方法来获取带参数的构造函数:

Constructor co = contDes.getConstructor(
                HttpServletRequest.class, 
                HttpServletResponse.class, 
                String[].class);
co.newInstance(request, response, page);

回答by Nilay Shah

Generally its not recommended to use other then default constructor, as web.xml or struts-config.xml uses to instantiate the servlet using reflection.

通常不建议使用其他默认构造函数,因为 web.xml 或 struts-config.xml 使用反射来实例化 servlet。

If you want to get instance of your class other than default constructor,

如果你想获得你的类的实例而不是默认构造函数,

Class userController = Class.forName("<packages.>UserController");
Constructor userControllerConstructor = userController.class.getConstructor(HttpServletRequest.class, HttpServletResponse.class, String[].class);
UserController userControllerObj = (UserController)constructor.newInstance(request,response, action);