找不到默认构造函数;嵌套异常是使用Spring MVC的java.lang.NoSuchMethodException? [英] No default constructor found; nested exception is java.lang.NoSuchMethodException with Spring MVC?
问题描述
我正在使用Spring MVC控制器项目。下面是我的控制器,我有一个声明的构造函数,我专门用于测试目的。
I am working with Spring MVC controller project. Below is my Controller and I have a constructor declared which I am specifically using for testing purpose.
@Controller
public class TestController {
private static KeeperClient testClient = null;
static {
// some code here
}
/**
* Added specifically for unit testing purpose.
*
* @param testClient
*/
public TestController(KeeperClient testClient) {
TestController.testClient = testClient;
}
// some method here
}
每当我启动服务器时,我都会遇到异常 -
Whenever I am starting the server, I am getting below exception -
No default constructor found; nested exception is java.lang.NoSuchMethodException:
但如果我删除 TestController
构造函数然后它没有任何问题正常工作。我在这里做错了什么?
But if I remove TestController
constructor then it works fine without any problem. What wrong I am doing here?
但是如果我添加这个默认构造函数然后它开始工作正常 -
But if I add this default constructor then it starts working fine -
public TestController() {
}
推荐答案
如果要创建自己的构造函数,则必须定义no-args或默认构造函数。
You must have to define no-args or default constructor if you are creating your own constructor.
您可以阅读为什么默认或不需要参数构造函数。
You can read why default or no argument constructor is required.
why-default-or-no-argument-constructor-java-class.html
这篇关于找不到默认构造函数;嵌套异常是使用Spring MVC的java.lang.NoSuchMethodException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!