BeanUtils framework Learning
package com.liang.Instopector;import java.io.Serializable;import java.util.Date;public class Student implements Serializable {private String name="liang";private int age=21;private Date birthday;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}}
Package com. liang. instopector; import java. beans. beanInfo; import java. beans. introspector; import java. beans. propertyDescriptor; import java. lang. reflect. invocationTargetException; import java. lang. reflect. method; import java. text. dateFormat; import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; import org. apache. commons. beanutils. beanUtils; import org. apache. commons. beanutils. convertUtils; import org. apache. commons. beanutils. converter; import org. apache. commons. beanutils. locale. converters. dateLocaleConverter; import org. junit. test; public class Demo {// @ Testpublic void test1 () throws Exception {// get the specified beanInfo object BeanInfo beanInfo = Introspector. getBeanInfo (Student. class); // get the attribute description array PropertyDescriptor [] PPS = beanInfo. getPropertyDescriptors (); // traversal: Because the class inherits the Object class, there is a getClass () method in the Object class, so the number will be one more System than you write. out. println (pps. length); for (PropertyDescriptor pd: pds) {System. out. print (pd. getName () + "");} System. out. println (); // obtain the specified PropertyDescriptorPropertyDescriptor pd = new PropertyDescriptor ("name", Student. class); // obtain the get Method m = pd. getReadMethod (); Student s = new Student (); String str = (String) m. invoke (s, null); System. out. println (str); // obtain the set Method m1 = pd. getWriteMethod (); m1.invoke (s, "aaa"); System. out. println (s. getName ();} // @ Testpublic void test2 () throws Exception {Student s = new Student (); // call the get method: getProperty returns a String, because all data is displayed in String age = BeanUtils. getProperty (s, "age"); System. out. println (age); // call the set Method: // BeanUtils can be automatically converted to a type, but only for the basic type: int type is required, and okBeanUtils is provided. setProperty (s, "name", "aaa"); BeanUtils. setProperty (s, "age", "123"); System. out. println ("name:" + s. getName (); System. out. println ("age:" + s. getAge ());} // attribute settings for non-basic types // user input is String // String <-----> mutual conversion between other types // The result displayed by the user is String @ Testpublic void test3 () throws Exception {Student s = new Student (); // register the type converter for BeanUtils: Custom converter ConvertUtils. register (new Converter () {DateFormat df = new SimpleDateFormat ("yyyy-MM-dd"); // type: Target type // value: the currently passed public Object convert (Class type, Object value) {if (value instanceof String) {// String to DateString d = (String) value; java. util. date date = null; try {date = df. parse (d);} catch (ParseException e) {e. printStackTrace ();} return date;} else {// Date is converted to String date = df. format (value); return date;} // if (type. equals (Date. class) {/// string to Date //} else {// Date to string //}, Date. class); BeanUtils. setProperty (s, "birthday", "2013-5-12"); System. out. println (s. getBirthday ();} // use the Date converter written in BeanUtils @ Testpublic void test4 () throws Exception {Student s = new Student (); ConvertUtils. register (new DateLocaleConverter (), Date. class); BeanUtils. setProperty (s, "birthday", "2014-10-3"); System. out. println (s. getBirthday ());}}