锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
public class TestBeanWrapper extends TestCase {
public void testPropertyEditor() throws Exception{
Object obj = Class.forName("com.ws.po.UserBean").newInstance();
BeanWrapper bw = new BeanWrapperImpl(obj);錛圓錛?/榪欓噷灝辮鍋氭敞鍐屽睘鎬х紪杈戝櫒鐨勫伐浣?br> bw.setPropertyValue("id", "1");錛圔錛?br> System.out.println(bw.getPropertyValue("id").getClass());錛圕錛?br> }
}
鍦ㄤ唬鐮丄榪欓噷錛屽氨闇瑕佹敞鍐屽睘鎬х紪杈戝櫒錛屼篃鏄疭pring Ioc鏈哄埗鐨勫叧閿儴鍒嗐?br> 鍦ㄤ唬鐮丅榪欓噷錛屾垜騫舵病鏈変紶鍏ew Integer(1)錛岃屾槸鐩存帴浼犲叆浜嗕竴涓瓧絎︿覆“1”銆?br> 鍦ㄤ唬鐮丆榪欓噷錛屽氨寰楀埌浜嗘垜鎯寵鐨勭被鍨嬶紝杈撳嚭緇撴灉涓猴細(xì)class java.lang.Integer銆?br> 濡傛灉浣犺瘯鍥懼湪B浠g爜榪欓噷錛屽皢“1”鏇存崲涓?#8220;SSSS”瀛楃涓詫紝閭d箞浼?xì)寰楀堫C竴涓猅ypeMismatchException錛屾彁紺烘棤娉曡漿鎹€?/font>
閭d箞鍦ㄤ唬鐮丄榪欓噷鍒板簳鍙戠敓浜嗕簺浠涔堝憿(鍙拡瀵瑰睘鎬х紪杈戝櫒鏉ヨ)錛熶笅闈竴灞傚眰鍦板線閲岄潰璧般?br> 榪欐浠g爜閲岄潰娌℃湁浠諱綍鐨勬湁鍏沖睘鎬х紪杈戝櫒鐨勪唬鐮侊紝閭d箞灝卞彧鏈夊線BeanWrapperImpl閲岄潰鏌ユ壘銆?br>
public BeanWrapperImpl(Object object) {
registerDefaultEditors();//榪欓噷灝卞紑濮嬫敞鍐岄粯璁ょ殑灞炴х紪杈戝櫒浜?br> setWrappedInstance(object);
}
BeanWrapperImpl綾葷戶鎵胯嚜AbstractPropertyAccessor鎶借薄綾伙紝AbstractPropertyAccessor綾誨張緇ф壙鑷狿ropertyEditorRegistrySupport綾伙紝鍦≒ropertyEditorRegistrySupport涓氨瀹炵幇浜唕egisterDefaultEditors()榪欎釜鏂規(guī)硶錛屼唬鐮佸涓嬶細(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));
}