码迷,mamicode.com
首页 > 编程语言 > 详细

spring 笔记1: mvn 中Controller方法的参数不能是嵌套类(内部类)。

时间:2016-05-31 18:39:57      阅读:571      评论:0      收藏:0      [点我收藏+]

标签:

最近做spring开发,个人认为,Controller和客户端js通讯时传递的参数类 只使用某几个方法,为了减少对其他功能的影响,想把参数类定义为Controller类的

嵌套类(内部类)。但是实践发现不行。

系统会报错:

Servlet.service() for servlet [kingkoo] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.itl.wms.mvc.systemsetting.POQCSettingController$QueryParameter]: No default constructor found; nested exception is java.lang.NoSuchMethodException: ***************Controller$嵌套类.<init>()] with root cause
java.lang.NoSuchMethodException: com.itl.wms.mvc.systemsetting.Controller$嵌套类.<init>()

 

提示嵌套类没有空参数的构造函数。虽然我已定义类的空构造函数。

因为spring查找参数类型的构造函数时,指明要找一个空参数的构造函数:

return instantiateClass(clazz.getDeclaredConstructor());  //org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)
但是,java里面,嵌套类是没有空参数的构造函数的:
public class App {

public class Nest
{
public Nest(){}

}

public static void main(String[] args) throws Exception {

NestClassConstrunct();
}

private static void NestClassConstrunct()
{
java.lang.Class c= Nest.class;
Constructor<?>[] cc= c.getConstructors();
Parameter[] p;
for (int i = 0; i < cc.length; i++) {
System.out.println(cc[i].toString());
p=cc[i].getParameters();
for (int j = 0; j < p.length; j++) {
System.out.println(p[j].getName()+":"+p[j].getParameterizedType().toString());
}
}
}

输出:

public App$Nest(App)
arg0:class App

 

 

so,只能把参数类移动到一个指定的包里面了。

请高手指正。

 

spring 笔记1: mvn 中Controller方法的参数不能是嵌套类(内部类)。

标签:

原文地址:http://www.cnblogs.com/fgq841103/p/spring.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!