Reflective mode in the running state of the Java class to map the various components into the corresponding Java class, you can dynamically get all the properties and dynamic call any method, emphasizing the running state.
The introspection mechanism is implemented by reflection, BeanInfo is used to expose the properties, methods, and events of a bean, and we can manipulate the properties of the JavaBean later.
5.1. Modify the attribute mode by PropertyDescriptor
Package Com.query;import Java.beans.propertydescriptor;import Java.lang.reflect.method;public class BeanInfoUtil { public static void SetProperty (UserInfo userinfo,string userName) throws exception{ PropertyDescriptor propdesc= New PropertyDescriptor (Username,userinfo.class); Method Methodsetusername=propdesc.getwritemethod (); Methodsetusername.invoke (UserInfo, "Wong"); System.out.println ("Set UserName:" +userinfo.getusername ()); } public static void GetProperty (UserInfo userinfo,string userName) throws exception{ PropertyDescriptor Prodescriptor =new PropertyDescriptor (username,userinfo.class); Method Methodgetusername=prodescriptor.getreadmethod (); Object Objusername=methodgetusername.invoke (userInfo); System.out.println ("Get UserName:" +objusername.tostring ());} }
5.2. Modifying properties through the Introspector class
Package Com.query;import Java.beans.beaninfo;import Java.beans.introspector;import java.beans.PropertyDescriptor; Import Java.lang.reflect.method;public class BeanInfoUtil2 {public static void Setpropertybyintrospector (UserInfo Userinfo,string userName) throws Exception {BeanInfo BeanInfo = Introspector.getbeaninfo (Userinfo.class); propertydescriptor[] prodescrtptors = Beaninfo.getpropertydescriptors (); if (prodescrtptors! = null && Prodescrtptors.length > 0) {for (PropertyDescriptor propdesc:prodescrtptors) {if (Propdesc.getname (). Equals ( UserName) {Method methodsetusername = Propdesc.getwritemethod (); Methodsetusername.invoke (UserInfo, "Alan"); System.out.println ("Set UserName:" + userinfo.getusername ()); public static void Getpropertybyintrospector (UserInfo userinfo,string userName) throws Exception {BeanInfo BeanInfo = Int Rospector.getbeaninfo (Userinfo.class); propertydescriptor[] prodescrtptors = Beaninfo.getpropertydescriptors (); if (prodescrtptors! = null &&amP Prodescrtptors.length > 0) {for (PropertyDescriptor propdesc:prodescrtptors) {if (Propdesc.getname (). Equals ( UserName)) {Method methodgetusername = Propdesc.getreadmethod (); Object objusername = Methodgetusername.invoke ( UserInfo); System.out.println ("Get userName:" + objusername.tostring ()); break;}}}}
Note, if the data type is incorrect, the error will be given when the JavaBean property is modified. For example Beaninfoutil.setproperty (UserInfo, "age"); the error is that the Age property is an int data type, and the value assigned to the Age property by default in the SetProperty method is a string type. Therefore, the argument type mismatch parameter type mismatch error message will be burst.
To address these issues, Apache has developed a simple, easy-to-use API to manipulate the Bean's Properties--beanutils toolkit.
Beanutils Toolkit: Download: http://commons.apache.org/beanutils/Note: A logging package is also required when applying http://commons.apache.org/logging/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.