Org. apache. commons. beanutils. BeanMap simple example, commonsbeanutils
1. org. apache. commons. beanutils. BeanMap; a java bean can be called through the map api,
Several supported Operation interfaces:
- Object get (Object key)
- Object put (Object key, Object value)
- Void putAll (Map t)
- Set entrySet ()
- Collection values ()
- Boolean containsKey (Object key)
- ....
For example:
// Concatenate the student information into a string in the format of stuname | stuage | stuclass
ProtectedString getLine (Student student) {StringBuilder sb =
NewStringBuilder (); BeanMap map =
NewBeanMap (student); appStr (sb, map, "stuname"); appStr (sb, map, "stuage"); appStr (sb, map, "stuclass ");
ReturnSb. toString ();}
// Combine the information of teacher into a string in the format of tname | tage | tclass
ProtectedString getLine (Teacher teacher) {StringBuilder sb =
NewStringBuilder (); BeanMap map =
NewBeanMap (teacher); appStr (sb, map, "tname"); appStr (sb, map, "tage"); appStr (sb, map, "tclass ");
ReturnSb. toString ();} // public method for String concatenation. different objects, such as studengt and teacher, are transmitted to the method through map.
Protected
VoidAppStr (StringBuilder sb, Map <String, Object> map, String str) {Object value = map. get (str); sb. append (newValue ). append ("|");} note: In the above example, although you can directly use sb. append (student. getStuname ()). append ("| "). append (student. getStuage ()). append ("| "). append (student. getStuclass); concatenates strings, but uses BeanMap. I think the reason is that multiple objects (such as student and teacher) Need to concatenate strings, if you want to abstract a public method, you can use BeanMap. In this way, you do not need to upload all objects to appStr. You only need to pass a BeanMap object.