当前位置: 移动技术网 > IT编程>开发语言>Java > Spring相关BUG

Spring相关BUG

2019年05月10日  | 移动技术网IT编程  | 我要评论

今天从云开发平台上生成的代码报spring相关的错误。

我找到第一处错误,整理如下:

org.springframework.beans.factory.beancreationexception:

error creating bean with name 'kmbookdetailtarget' defined in servletcontext resource [/web-inf/kmssconfig/km/book/spring.xml]: error setting property values;

nested exception is org.springframework.beans.notwritablepropertyexception: invalid property 'kmbookcategoryservice' of bean class [com.landray.kmss.km.book.service.spring.kmbookdetailserviceimp]: bean property 'kmbookcategoryservice' is not writable or has an invalid setter method.

does the parameter type of the setter match the return type of the getter?

首先翻译一遍:

创建定义在这个spring.xml文件里面,名为'kmbookdetailtarget'的bean时出错:设置属性值时出错;

嵌套异常为这个exception:这个bean类的属a性'kmbookcategoryservice'无效:bean属性'kmbookcategoryservice'是不可写或者有一个无效的setter方法。

setter的参数类型是否与getter的返回类型匹配?

然后确定了导致错误的起源在这个spring.xml文件,并且找到'kmbookdetailtarget'这个bean,然后找到这个bean里面的属性'kmbookcategoryservice',如图:

 

bean每注入一个属性,我们都要在这个bean对应的class中定义setter方法,我们找到这个class,如图:

发现没有任何setter方法。其实basedao这个属性应该是在框架中已经setter过了,即父类中定义了,而我们自定义的属性'kmbookcategoryservice'并没有定义,因此才导致错误。

解决办法:

删掉该属性,如图:

clean项目,重新启动,ok!

 

------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------
那么如果我不删除spring配置,该如何做?

应该在对应的类里面添加setter方法,如图:

重新启动,欸~都挺好,也ok!

------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------

但如果,我就是要删除这个spring配置,但是以后我还是想要用这个属性怎么办?

我们可以通过另外一种办法使用:

 

springbeanutil是我们封装的类,针对这个方法的代码如下:

private static applicationcontext applicationcontext = null;

public static object getbean(string beanname) {
  if (applicationcontext == null)
  return null;
  return applicationcontext.getbean(beanname);
}

好不好用测试才行,很不幸,我们框架没有单元测试(我自己也不会搞),于是我从前端写了一个ajax,然后到action层调service,最后在serviceimp调test方法测试。

结果测试可行!

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网