[Learn Java for beginners] Use beanUtils to control javabean and beanutilsjavabean
Use BeanUtils to set/read attribute values and Auto conversion supported by default:
@ Test // use BeanUtils to set/read attribute values and automatically convert public void test1 () throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {Person p = new Person (); // use BeanUtils to set the attribute value BeanUtils. setProperty (p, "username", ""); // use BeanUtils to read the attribute value System. out. println (BeanUtils. getProperty (p, "username"); // different types can still be converted automatically. BeanUtils supports conversion of eight basic types by default. BeanUtils. setProperty (p, "age", "123"); System. out. println (p. getAge ());}
Register an existing converter to automatically convert complex types:
@ Test // register an existing converter to automatically convert complex types to public void test3 () throws IllegalAccessException, InvocationTargetException {Person p = new Person (); String birthday = "; // register the time converter ConvertUtils provided by Apache. register (new DateLocaleConverter (), Date. class); BeanUtils. setProperty (p, "birthday", birthday); System. out. println (p. getBirthday ());}
The existing Apache time converter cannot filter empty strings. If the string to be converted is empty, an exception is thrown. However, the real business is very complicated, apache cannot provide us with all types of conversion methods. When necessary, we can register the converters we need to complete the business needs.
Register your own converter completion time conversion:
@ Test // register your own converter completion time to convert public void test2 () throws IllegalAccessException, InvocationTargetException {Person p = new Person (); String birthday = "; // For the date value that can be assigned to the bean attribute, we register the date converter ConvertUtils for benUtils. register (new Converter () {@ SuppressWarnings ({"unchecked", "rawtypes"}) public Object convert (Class type, Object value) {if (value = null) {return null;} if (! (Value instanceof String) {throw new ConversionException ("only String type conversion supported");} String str = (String) value; if (str. trim (). equals ("") {return null;} SimpleDateFormat dateformate = new SimpleDateFormat ("yyyy-MM-dd"); try {return dateformate. parse (str);} catch (ParseException e) {throw new RuntimeException (e) ;}}, Date. class); BeanUtils. setProperty (p, "birthday", birthday); System. out. println (p. getBirthday ());}
Directly use the map object filling class:
@ Test // directly use the map object filling class public void test4 () throws Exception {HashMap <String, String> map = new HashMap <String, String> (); map. put ("username", "Li Si"); map. put ("password", "lisi"); map. put ("age", "26"); map. put ("birthday", "1990-05-05"); ConvertUtils. register (new DateLocaleConverter (), Date. class); Person p = new Person (); BeanUtils. populate (p, map); System. out. println (p. getUsername (); System. out. println (p. getPassword (); System. out. println (p. getAge (); System. out. println (p. getBirthday ());}