Org.springframework.beans. A demo of Beanutils . It is graceful to copy the value of the parent field to the subclass
The output of the following example (subclass using the ToString method of the parent class, a bit of meaning):
true person{name= ' People '}
Importorg.springframework.beans.BeanUtils; Public classTest { Public Static voidMain (string[] args) { person person=NewPerson ("people"); Male Male=NewMale (); Beanutils. copyproperties (person, male); System.out.println (Male.class. isinstance (male)); SYSTEM.OUT.PRINTLN (male); }}classPerson {PrivateString name; PublicPerson () {} PublicPerson (String name) { This. Name =name; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"person{" + "name=" + name + ' \ ' + '} '; }}classMaleextendsPerson {PrivateString necklace; PublicMale () {} PublicMale (String name) {Super(name); } PublicString Getnecklace () {returnnecklace; } Public voidsetnecklace (String necklace) { This. Necklace =necklace; }}
Org.springframework.beans.BeanUtils
Source Code Analysis:
The use of reflection is:
/*** Copy The property values of the given source beans into the target bean. * <p>note:the source and Target classes do not has to match or even is derived * from all other, as long as T He properties match. Any bean properties, the * source Bean exposes but the target bean does not would silently be ignored. * <p>this is just a convenience method. For the complex transfer needs, * Consider using a full beanwrapper. * @paramsource The source Bean *@paramtarget the target bean *@throwsBeansexception If the copying failed *@seeBeanwrapper*/ Public Static voidCopyproperties (object source, object target)throwsbeansexception {copyproperties (source, Target,NULL, (string[])NULL); }
/*** Copy The property values of the given source beans into the given target bean. * <p>note:the source and Target classes do not has to match or even is derived * from all other, as long as T He properties match. Any bean properties, the * source Bean exposes but the target bean does not would silently be ignored. * @paramsource The source Bean *@paramtarget the target bean *@parameditable the class (or interface) to restrict property setting to *@paramignoreproperties array of names to ignore *@throwsBeansexception If the copying failed *@seeBeanwrapper*/ Private Static voidCopyproperties (object source, object target, class<?>editable, String ... ignoreproperties)throwsbeansexception {assert.notnull (source,"Source must not being null"); Assert.notnull (Target,"Target must not being null"); Class<?> actualeditable =Target.getclass (); if(Editable! =NULL) { if(!editable.isinstance (target)) { Throw NewIllegalArgumentException ("Target class [" + Target.getclass (). GetName () + "] not assignable to Edit Able class ["+ Editable.getname () +"] "); } actualeditable = editable;//If editable is assigned, ignore the field property in Target } Propertydescriptor[] targetpds =getpropertydescriptors (actualeditable); List<String>ignorelist= (Ignoreproperties! =NULL? Arrays.aslist (ignoreproperties):NULL); for(propertydescriptor targetpd: targetpds) {Method Writemethod=Targetpd.getwritemethod (); if(Writemethod! =NULL&& (Ignorelist = =NULL|| ! Ignorelist.contains (Targetpd.getname ()))) {PropertyDescriptor SOURCEPD=Getpropertydescriptor (Source.getclass (), Targetpd.getname ()); if(SOURCEPD! =NULL) {Method Readmethod=Sourcepd.getreadmethod (); if(Readmethod! =NULL&&classutils.isassignable (Writemethod.getparametertypes () [0], Readmethod.getreturntype ())) {Try { if(!Modifier.ispublic (Readmethod.getdeclaringclass (). getmodifiers ())) {readmethod.setaccessible (true); } Object Value=Readmethod.invoke (source); if(!Modifier.ispublic (Writemethod.getdeclaringclass (). getmodifiers ())) {writemethod.setaccessible (true); } writemethod.invoke (target, value); } Catch(Throwable ex) {Throw NewFatalbeanexception ("Could Not copy property '" + targetpd.getname () + "' from Source to target", ex); } } } } } }
Org.springframework.beans.BeanUtils