ï»??xml version="1.0" encoding="utf-8" standalone="yes"?>福利网站在线观看,啊v在线视频,国产伦精品一区二区三区免费迷 http://www.aygfsteel.com/lendo/category/20807.htmlzh-cnThu, 05 Apr 2007 03:27:31 GMTThu, 05 Apr 2007 03:27:31 GMT60Spring FrameWorkåQbeans包-属性编辑器(PropertyEditor)http://www.aygfsteel.com/lendo/articles/105589.htmllendolendoWed, 04 Apr 2007 04:55:00 GMThttp://www.aygfsteel.com/lendo/articles/105589.htmlhttp://www.aygfsteel.com/lendo/comments/105589.htmlhttp://www.aygfsteel.com/lendo/articles/105589.html#Feedback0http://www.aygfsteel.com/lendo/comments/commentRss/105589.htmlhttp://www.aygfsteel.com/lendo/services/trackbacks/105589.html         æœ¬æ¥æƒÏxŠŠbeans包放åˆîC¸€½‹‡æ–‡ç« ä¸­æ¥è®°å½•å¾—åQŒä¸˜q‡éšç€å­¦ä¹ (f¨¤n)得深入,发现原来ž®±æ˜¯˜q™ä¸€ä¸ªåŒ…åQŒä¹Ÿæœ‰å¾ˆå¤šå¾—机制需要详¾l†å¾—描述åQŒæ‰€ä»¥æˆ‘˜q˜æ˜¯æŠŠåŽŸæ¥çš„æ–‡æ¡£æ‹†åˆ†æˆå‡ ä¸ªéƒ¨åˆ†ï¼Œ˜q™æ ·æˆ‘以后review自己学习(f¨¤n)的时候也比较清楚ã€?br>     首先应该说明一下什么是属性编辑器åQŒåœ¨Spring的应用中¾lå¸¸è¦é‡åˆ°è¿™¿Uæƒ…况,JavaBean的值是在运行的时候动态地从配¾|®æ–‡ä»¶ä¸­æ³¨å…¥çš„,而配¾|®æ–‡ä»¶ä¸­å±žæ€§çš„值都是String¾cÕdž‹çš„,对应的JavaBean中却有其他很多类型,如Date,Integer,Byte½{‰ï¼Œæ‰€ä»¥å±žæ€§ç¼–辑器的ä“Q务就是在Spring˜q›è¡Œæ³¨å…¥˜q™ä¸ªåŠ¨ä½œçš„æ—¶å€™ï¼Œž®†String¾cÕdž‹çš„æ•°æ®è{åŒ–äØ“(f¨´)JavaBean对应的类型ã€?br>     比如如下˜q™æ®µä»£ç åQŒUserBean是一个标准的JavaBeanåQŒæœ‰Integer idå’ŒString userName两个属性ã€?br>    
     public class TestBeanWrapper extends TestCase {
         public void testPropertyEditor() throws Exception{
             Object obj = Class.forName("com.ws.po.UserBean").newInstance();
             BeanWrapper bw = new BeanWrapperImpl(obj);åQˆAåQ?/˜q™é‡Œž®Þp¦åšæ³¨å†Œå±žæ€§ç¼–辑器的工ä½?br>             bw.setPropertyValue("id", "1");åQˆBåQ?br>             System.out.println(bw.getPropertyValue("id").getClass());åQˆCåQ?br>         }
    
}

     在代码A˜q™é‡ŒåQŒå°±éœ€è¦æ³¨å†Œå±žæ€§ç¼–辑器åQŒä¹Ÿæ˜¯Spring Ioc机制的关键部分ã€?br>     在代码B˜q™é‡ŒåQŒæˆ‘òq¶æ²¡æœ‰ä¼ å…¥new Integer(1)åQŒè€Œæ˜¯ç›´æŽ¥ä¼ å…¥äº†ä¸€ä¸ªå­—½W¦ä¸²“1”ã€?br>     在代码C˜q™é‡ŒåQŒå°±å¾—到了我惌™¦çš„类型,输出¾l“果为:(x¨¬)class java.lang.Integerã€?br>     如果你试囑֜¨B代码˜q™é‡ŒåQŒå°†“1”更换ä¸?#8220;SSSS”字符ä¸ÔŒ¼Œé‚£ä¹ˆä¼?x¨¬)å¾—åˆîC¸€ä¸ªTypeMismatchExceptionåQŒæ½Cºæ— æ³•è{换ã€?/font>

     那么在代码A˜q™é‡Œåˆ°åº•发生了些什么呢(只针对属性编辑器来讲)åQŸä¸‹é¢ä¸€å±‚层地往里面走ã€?br>    ˜q™æ®µä»£ç é‡Œé¢æ²¡æœ‰ä»ÖM½•的有兛_±žæ€§ç¼–辑器的代码,那么ž®±åªæœ‰å¾€BeanWrapperImpl里面查找ã€?br>
     public BeanWrapperImpl(Object object) {
         registerDefaultEditors();//˜q™é‡Œž®±å¼€å§‹æ³¨å†Œé»˜è®¤çš„属性编辑器äº?br>         setWrappedInstance(object);
     }
 

     
BeanWrapperImpl¾cȝ‘ô承自AbstractPropertyAccessor抽象¾c»ï¼ŒAbstractPropertyAccessor¾cÕdˆ¾l§æ‰¿è‡ªPropertyEditorRegistrySupport¾c»ï¼Œåœ¨PropertyEditorRegistrySupport中就实现了registerDefaultEditors()˜q™ä¸ªæ–ÒŽ(gu¨©)³•åQŒä»£ç å¦‚下:(x¨¬)

     protected void registerDefaultEditors() {
         this.defaultEditors = new HashMap(32);

         // Simple editors, without parameterization capabilities.
         // The JDK does not contain a default editor for any of these target types.
         this.defaultEditors.put(Class.class, new ClassEditor());
         this.defaultEditors.put(Class[].class, new ClassArrayEditor());
         this.defaultEditors.put(File.class, new FileEditor());
         this.defaultEditors.put(InputStream.class, new InputStreamEditor());
         this.defaultEditors.put(Locale.class, new LocaleEditor());
         this.defaultEditors.put(Properties.class, new PropertiesEditor());
         this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor());
         this.defaultEditors.put(URL.class, new URLEditor());

         // Register JDK-1.4-specific editors.
         if (JdkVersion.isAtLeastJava14()) {
             this.defaultEditors.put(URI.class, new URIEditor());
             this.defaultEditors.put(Pattern.class, new PatternEditor());
         }

         // Default instances of collection editors.
         // Can be overridden by registering custom instances of those as custom editors.
         this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class));
         this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class));
         this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class));
         this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class));
         this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));

         // Default editors for primitive arrays.
         this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor());
         this.defaultEditors.put(char[].class, new CharArrayPropertyEditor());

         // The JDK does not contain a default editor for char!
         this.defaultEditors.put(char.class, new CharacterEditor(false));
         this.defaultEditors.put(Character.class, new CharacterEditor(true));

         // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor.
         this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false));
         this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true));

         // The JDK does not contain default editors for number wrapper types!
         // Override JDK primitive number editors with our own CustomNumberEditor.
         this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false));
         this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true));
         this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false));
         this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true));
         this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false));
         this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true));
         this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false));
         this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true));
         this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false));
         this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true));
         this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false));
         this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true));
         this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true));
         this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true));
    }



]]>
Ö÷Õ¾Ö©Öë³ØÄ£°å£º ÐÁ¼¯ÊÐ| ¼Ó²éÏØ| ÁÙ¹ðÏØ| ÅæÏØ| Ðãɽ| ÄôÀ­Ä¾ÏØ| ºÓÄÏÊ¡| ¾üÊÂ| ëøÖÝÊÐ| Íß·¿µêÊÐ| ÔÆÑôÏØ| Áúº£ÊÐ| Îè¸ÖÊÐ| Ã×ȪÊÐ| ¶«¹âÏØ| ½ú½­ÊÐ| ³ØÖÝÊÐ| ¼ª°²ÊÐ| ½¨ºþÏØ| ºìÔ­ÏØ| ¶«°¢ÏØ| ʯÇþÏØ| ±£¿µÏØ| ¹«°²ÏØ| ½ðÉ½Çø| ·½³ÇÏØ| òÔºÓÊÐ| É£Ö²ÏØ| Ðãɽ| ÑÎÔ´ÏØ| ÕØ¶«ÊÐ| Ñô³ÇÏØ| ¾ÞÒ°ÏØ| ÃçÀõÊÐ| äųØÏØ| Á¹³ÇÏØ| ¼ÎÐËÊÐ| ¤´¨ÏØ| ¾ÅÁúÏØ| ³Â°Í¶û»¢Æì| иÉÏØ|