Spring 调试历险记

来源:互联网 发布:采石之战 知乎 编辑:程序博客网 时间:2024/05/16 18:16

 

Spring  调试历险记


 

前言:

    

     今天最大的收获就是对代码的报错,不再那么的恐惧了,自己更具有信心一步一步去分析。就事论事,面对当下:有问题,就问题论事、分析,解决问题----这就是通俗的说:代码亲和力的进步的吧!

 

    最近敲了一个spring的demo,对于时间的转换,一直都是没有出来。自己很是苦恼,最后是请了sister Angelina 来帮个忙,人家有经验,有代码量,就是不一样呀!

 

     在InjectionTest.java中进行单元测试时,一直就是报错,自己没有找出来,总的来说还是对报错有一定的恐惧,现在的耐心还不是特别的足,还需要沉淀,这段时间也是非常的感觉sister Angelina,常常叫她来帮我调试代码的错误,总是责无旁贷,让我很是感激,thanks for her!

 

 

问题原型:

       单元测试爆红,测试又是没有通过:

       

 



报错情况:

 

   org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'customEditors' defined in class path resource[applicationContext-editor.xml]: Cannot create inner bean'com.bjpowernode.spring.UtilDatePeropertyEditor#5c0369c4' while setting beanproperty 'customEditors' with key [java.util.Date]; nested exception isorg.springframework.beans.factory.BeanCreationException: Error creating beanwith name 'com.bjpowernode.spring.UtilDatePeropertyEditor#5c0369c4' defined inclass path resource [applicationContext-editor.xml]: Error setting propertyvalues; nested exception is org.springframework.beans.NotWritablePropertyException:Invalid property 'pattern' of bean class[com.bjpowernode.spring.UtilDatePeropertyEditor]: Bean property 'pattern' isnot writable or has an invalid setter method. Does the parameter type of thesetter match the return type of the getter?

 

 

   Causedby: org.springframework.beans.factory.BeanCreationException: Error creatingbean with name 'com.bjpowernode.spring.UtilDatePeropertyEditor#5c0369c4'defined in class path resource [applicationContext-editor.xml]: Error settingproperty values; nested exception isorg.springframework.beans.NotWritablePropertyException: Invalid property'pattern' of bean class [com.bjpowernode.spring.UtilDatePeropertyEditor]: Beanproperty 'pattern' is not writable or has an invalid setter method. Does the parametertype of the setter match the return type of the getter?

 

   Causedby: org.springframework.beans.NotWritablePropertyException: Invalid property'pattern' of bean class [com.bjpowernode.spring.UtilDatePeropertyEditor]: Beanproperty 'pattern' is not writable or has an invalid setter method. Does theparameter type of the setter match the return type of the getter?

         atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:751)

         atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:608)

         atorg.springframework.beans.AbstractPropertyAccessor.setPropertyValue(AbstractPropertyAccessor.java:49)

         atorg.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAcce

 

 

 

     看到这样的情况,自己是非常的恐惧的,总是感觉自己不会,没有信心去面对,但是只要耐心的去看,其实也不是那么的恐惧。之前看了也调试了不少错误,这次真的弄了好长时间,还没有弄出来,就请sister Angelina 来帮我,站在巨人她的肩膀上。她带着我一句一句,一步一步的分享,这次最重要的就是叫我怎么分享,怎么一句一句的去理清逻辑,去分享,找错再去纠错误等等。

 


解决问题:

    其实细心的看看上面,先宏观的看,就是两个Caused by ,所以很有可能就是两个错误,在细细的去看和分析就行呀:


       一个是Errorcreating bean with name 'customEditors' defined in class path resource[applicationContext-editor.xml]: Cannot create inner bean'com.bjpowernode.spring.UtilDatePeropertyEditor#5c0369c4' while setting beanproperty 'customEditors' with key [java.util.Date];


       另一个就是:org.springframework.beans.NotWritablePropertyException:Invalid property 'pattern' of bean class[com.bjpowernode.spring.UtilDatePeropertyEditor]: Bean property 'pattern' isnot writable or has an invalid setter method. Does the parameter type of thesetter match the return type of the getter?

就事论事,这么一看,其实就是非常的有意思了,将问题查分开来,柳暗花明又一村!




下面是代码实践例子:

       



 

实体:Bean1.java

package com.bjpowernode.spring; import java.util.Date;import java.util.List;import java.util.Map;import java.util.Set; public class Bean1 {          privateString strValue;         privateint intValue;         privateList listValue;         privateSet setValue;   private String[] arrayValue;   private Map mapValue;   private Date dateValue;                   publicDate getDateValue() {                   returndateValue;         }         publicvoid setDateValue(Date dateValue) {                   this.dateValue= dateValue;         }         publicString getStrValue() {                   returnstrValue;         }         publicvoid setStrValue(String strValue) {                   this.strValue= strValue;         }         publicint getIntValue() {                   returnintValue;         }         publicvoid setIntValue(int intValue) {                   this.intValue= intValue;         }         publicList getListValue() {                   returnlistValue;         }         publicvoid setListValue(List listValue) {                   this.listValue= listValue;         }         publicSet getSetValue() {                   returnsetValue;         }         publicvoid setSetValue(Set setValue) {                   this.setValue= setValue;         }         publicString[] getArrayValue() {                   returnarrayValue;         }         publicvoid setArrayValue(String[] arrayValue) {                   this.arrayValue= arrayValue;         }         publicMap getMapValue() {                   returnmapValue;         }         publicvoid setMapValue(Map mapValue) {                   this.mapValue= mapValue;         }      } 


UtilDatePropertyEditor.java

package com.bjpowernode.spring; import java.beans.PropertyEditorSupport;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date; /** * 属性编辑器:在application.xml中配置的是字符串, * 这个UtilDatePerorpertyEditor就是将字符串转换javaUtilDate *@author Daniel * */public class UtilDatePropertyEditor extendsPropertyEditorSupport {          @Override         publicvoid setAsText(String text) throws IllegalArgumentException {                   System.out.println("----UtilDatePropertyEditor.setAsText()-----"+ text );                   try{                            Datedate=new SimpleDateFormat("yyyy-MM-dd").parse(text);                            this.setValue(date);                            }catch (ParseException e) {                                     //TODO Auto-generated catch block                                     e.printStackTrace();                                     thrownew IllegalArgumentException(text);                            }         } } 


配置文件:

  本来是一个配置文件的,但是有时一个配置文件东西还是比较多的,所以可以将配置文件也分成两个,让分析和编码起来更加的高效。


applicationContext.xml 和applicationContext-editor.xml

<?xml version="1.0"encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xmlns:aop="http://www.springframework.org/schema/aop"           xmlns:tx="http://www.springframework.org/schema/tx"           xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd          http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  <bean  id="bean1"class="com.bjpowernode.spring.Bean1">   <property name="strValue" value="Hello_Spring"/>   <!--两种方式注入数字:方法1<propertyname="intValue" value="123"/> -->               <!-- 方法2 -->               <property name="intValue">                 <value>123</value>               </property>   <property name="listValue">               <list>                      <value >list1</value>                      <value >list2</value>               </list>     </property>   <property name="setValue">           <set>              <value>set1</value>              <value>set2</value>           </set>   </property>   <property name="arrayValue">           <list>              <value>array1</value>              <value>array2</value>           </list>   </property>   <property name="mapValue">           <map>             <entry key="k1"value="v1"></entry>             <entry key="k2"value="v2"></entry>           </map>   </property>     <property name="dateValue" value="2017-2-27"/>          </bean> </beans>


 

applicationContext-editor.xml

<?xml version="1.0"encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xmlns:aop="http://www.springframework.org/schema/aop"           xmlns:tx="http://www.springframework.org/schema/tx"           xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd          http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="customEditors"class="org.springframework.beans.factory.config.CustomEditorConfigurer"><property name="customEditors">       <map>           <entry key="java.util.Date">              <bean class="com.bjpowernode.spring.UtilDatePeropertyEditor">                                        <!-- <property name="pattern" value="yyyy年MM月dd日"/> -->              </bean>                        </entry>       </map></property></bean> <!-- <bean id="utilDatePropertyEditor"class="com.bjpowernode.spring.UtilDatePropertyEditor"> </bean> --></beans> 


InjectionTest.java

package test; importorg.springframework.beans.factory.BeanFactory;importorg.springframework.context.support.ClassPathXmlApplicationContext; import com.bjpowernode.spring.Bean1; import junit.framework.TestCase;   public class InjectionTest extends TestCase{          privateBeanFactory factory;          @Override         protectedvoid setUp() throws Exception {                   //TODO Auto-generated method stub            String[] configLocation=new String[]{"applicationContext.xml","applicationContext-editor.xml"};                  // factory=newClassPathXmlApplicationContext("applicationContext.xml");            factory=newClassPathXmlApplicationContext(configLocation);         }          /*protectedvoid tearDown() throws Exception {                   //TODO Auto-generated method stub                           }*/           publicvoid testInjection1()         {      //简单的new 一下,属性是不会注入的,必须从容器中拿出来            //Bean bean=new Bean();普通的new 不会被注入                                                        Bean1bean1=(Bean1)factory.getBean("bean1");                   System.out.println("bean.strValue=" + bean1.getStrValue());                   System.out.println("bean.intValue=" + bean1.getIntValue());                   System.out.println("bean.listValue=" + bean1.getListValue());                   System.out.println("bean.setValue=" + bean1.getSetValue());                   System.out.println("bean.arrayValue=" + bean1.getArrayValue());                   System.out.println("bean.mapValue=" + bean1.getMapValue());                   System.out.println("bean.dateValue=" + bean1.getDateValue());                           }} 



小结:

      今天最大的收获就是对代码的报错,不再那么的恐惧了,自己更具有信心一步一步去分析。就事论事,面对当下:有问题,就问题论事、分析,解决问题----这就是通俗的说:代码亲和力的进步的吧!

 

    感激sister 何红霞今天晚上来引导我如何一步一步,耐心的调错,非常的开心,非常的好,以后我更要学会自己多找错和调错,接下来进行总结和继续往后面走。



 

0 0