﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-穿过骨头抚摸你-文章分类-SpringFramework</title><link>http://www.blogjava.net/lendo/category/20807.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 05 Apr 2007 03:27:31 GMT</lastBuildDate><pubDate>Thu, 05 Apr 2007 03:27:31 GMT</pubDate><ttl>60</ttl><item><title>Spring FrameWork－beans包－属性编辑器(PropertyEditor)</title><link>http://www.blogjava.net/lendo/articles/105589.html</link><dc:creator>lendo</dc:creator><author>lendo</author><pubDate>Wed, 04 Apr 2007 04:55:00 GMT</pubDate><guid>http://www.blogjava.net/lendo/articles/105589.html</guid><wfw:comment>http://www.blogjava.net/lendo/comments/105589.html</wfw:comment><comments>http://www.blogjava.net/lendo/articles/105589.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lendo/comments/commentRss/105589.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lendo/services/trackbacks/105589.html</trackback:ping><description><![CDATA[<p><font size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<font face=Verdana>本来想把beans包放到一篇文章中来记录得，不过随着学习得深入，发现原来就是这一个包，也有很多得机制需要详细得描述，所以我还是把原来的文档拆分成几个部分，这样我以后review自己学习的时候也比较清楚。<br>&nbsp;&nbsp;&nbsp;&nbsp; 首先应该说明一下什么是属性编辑器，在Spring的应用中经常要遇到这种情况，JavaBean的值是在运行的时候动态地从配置文件中注入的，而配置文件中属性的值都是String类型的，对应的JavaBean中却有其他很多类型，如Date,Integer,Byte等，所以属性编辑器的任务就是在Spring进行注入这个动作的时候，将String类型的数据转化为JavaBean对应的类型。<br>&nbsp;&nbsp;&nbsp;&nbsp; 比如如下这段代码，UserBean是一个标准的JavaBean，有Integer id和String userName两个属性。<br>&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp; public class TestBeanWrapper extends TestCase {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void testPropertyEditor() throws Exception{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object obj = Class.forName("com.ws.po.UserBean").newInstance();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BeanWrapper bw = new BeanWrapperImpl(obj);（A）//这里就要做注册属性编辑器的工作<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bw.setPropertyValue("id", "1");（B）<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;System.out.println(bw.getPropertyValue("id").getClass());（C）<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp; </font></font><font size=2><font face=Verdana>}<br><br>&nbsp;&nbsp;&nbsp;&nbsp; 在代码A这里，就需要注册属性编辑器，也是Spring Ioc机制的关键部分。<br>&nbsp;&nbsp;&nbsp;&nbsp; 在代码B这里，我并没有传入new Integer(1)，而是直接传入了一个字符串&#8220;1&#8221;。<br>&nbsp;&nbsp;&nbsp;&nbsp; 在代码C这里，就得到了我想要的类型，输出结果为：class java.lang.Integer。<br>&nbsp;&nbsp;&nbsp;&nbsp; 如果你试图在B代码这里，将&#8220;1&#8221;更换为&#8220;SSSS&#8221;字符串，那么会得到一个TypeMismatchException，提示无法转换。</font> </font></p>
<p><font face=Verdana size=2>&nbsp;&nbsp;&nbsp;&nbsp; 那么在代码A这里到底发生了些什么呢(只针对属性编辑器来讲)？下面一层层地往里面走。<br></font><font size=+0><font face=Verdana>&nbsp;&nbsp;&nbsp; <font size=2>这段代码里面没有任何的有关属性编辑器的代码，那么就只有往BeanWrapperImpl里面查找。<br><br>&nbsp;&nbsp;&nbsp;&nbsp; public BeanWrapperImpl(Object object) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; registerDefaultEditors();//这里就开始注册默认的属性编辑器了<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setWrappedInstance(object);<br>&nbsp;&nbsp;&nbsp;&nbsp; }</font></font>&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font face=Verdana size=2>BeanWrapperImpl类继承自AbstractPropertyAccessor抽象类，AbstractPropertyAccessor类又继承自PropertyEditorRegistrySupport类，在PropertyEditorRegistrySupport中就实现了registerDefaultEditors()这个方法，代码如下：<br><br>&nbsp;&nbsp;&nbsp;&nbsp; protected void registerDefaultEditors() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors = new HashMap(32);</font> </p>
<p><font face=Verdana size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Simple editors, without parameterization capabilities.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // The JDK does not contain a default editor for any of these target types.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Class.class, new ClassEditor());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Class[].class, new ClassArrayEditor());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(File.class, new FileEditor());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(InputStream.class, new InputStreamEditor());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Locale.class, new LocaleEditor());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Properties.class, new PropertiesEditor());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(URL.class, new URLEditor());</font> </p>
<p><font face=Verdana size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Register JDK-1.4-specific editors.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (JdkVersion.isAtLeastJava14()) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(URI.class, new URIEditor());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Pattern.class, new PatternEditor());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font> </p>
<p><font face=Verdana size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Default instances of collection editors.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Can be overridden by registering custom instances of those as custom editors.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));</font> </p>
<p><font face=Verdana size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Default editors for primitive arrays.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(char[].class, new CharArrayPropertyEditor());</font> </p>
<p><font face=Verdana size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // The JDK does not contain a default editor for char!<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(char.class, new CharacterEditor(false));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Character.class, new CharacterEditor(true));</font> </p>
<p><font face=Verdana size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true));</font> </p>
<p><font face=Verdana size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // The JDK does not contain default editors for number wrapper types!<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Override JDK primitive number editors with our own CustomNumberEditor.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true));<br>&nbsp;&nbsp;&nbsp; }</font> </p>
<img src ="http://www.blogjava.net/lendo/aggbug/105589.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lendo/" target="_blank">lendo</a> 2007-04-04 12:55 <a href="http://www.blogjava.net/lendo/articles/105589.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>