Java內省技術(Introspector)
阿新 • • 發佈:2018-12-11
Java內省技術
一、利用內省API操作JavaBean的屬性
import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; /** * 利用內省API操作JavaBean的屬性 * @author WangYifeng * * 2018年9月22日下午10:15:32 */ public class Demo1 { public static void main(String[] args) throws Exception { getProperty(); System.out.println("-------------華麗的分割線------------"); getProperty1(); System.out.println("-------------華麗的分割線------------"); getProperty2(); } //利用內省獲取JavaBean的全部屬性 public static void getProperty() throws Exception { BeanInfo beanInfo = Introspector.getBeanInfo(Person.class); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for(PropertyDescriptor pd : propertyDescriptors) { System.out.print(pd.getName()+","); } System.out.println(); } //利用內省獲取JavaBean的自身屬性,不包含從父類繼承的屬性 public static void getProperty1() throws Exception { BeanInfo beanInfo = Introspector.getBeanInfo(Person.class, Object.class); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for(PropertyDescriptor pd : propertyDescriptors) { System.out.print(pd.getName()+","); } System.out.println(); } //操作JavaBean的指定屬性 public static void getProperty2() throws Exception { Person p = new Person(); //建立JavaBean指定屬性的屬性描述器PropertyDescriptor PropertyDescriptor pd = new PropertyDescriptor("name", Person.class); //根據屬性描述器獲取屬性的型別 System.out.println(pd.getPropertyType()); //根據屬性描述器獲取屬性的讀方法讀取屬性的值,即getter方法 Method readMethod = pd.getReadMethod(); System.out.println(readMethod.invoke(p, null)); //根據屬性描述器獲取屬性的寫方法修改屬性的值,即setter方法 Method writeMethod = pd.getWriteMethod(); writeMethod.invoke(p, "李四"); System.out.println(readMethod.invoke(p, null)); } }
執行結果: 二、利用BeanUtils操作JavaBean的屬性 下載第三方開源庫的jar包,匯入當前project中
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConversionException; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.Converter; /** * 利用BeanUtils操作JavaBean的屬性 * @author WangYifeng * * 2018年9月22日下午10:15:32 */ public class Demo2 { public static void main(String[] args) throws Exception { useBeanUtils(); useBeanUtils1(); } //利用beanUtils操作JavaBean的指定屬性 public static void useBeanUtils() throws Exception { Person p = new Person(); //操作屬性的值 BeanUtils.setProperty(p, "name", "王五"); //只能操作8中基本資料型別 //獲取屬性的值 System.out.println(BeanUtils.getProperty(p, "name")); } //利用beanUtils操作JavaBean的指定屬性 public static void useBeanUtils1() throws Exception { Person p = new Person(); //為了將日期賦到birthday屬性上,我們需要給BeanUtils註冊一個日期轉換器 ConvertUtils.register(new Converter() { @Override public <T> T convert(Class<T> arg0, Object arg1) { if(arg1==null) { return null; } if(arg1.getClass() != String.class) { throw new ConversionException("只支援String型別轉換。"); } String str = (String) arg1; if(str.trim().equals("")) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { return (T) sdf.parse(str); } catch (ParseException e) { throw new RuntimeException(e); } } }, Date.class); //操作屬性的值 BeanUtils.setProperty(p, "birthday", "1995-11-23"); //只能操作8中基本資料型別 //獲取屬性的值 // String property = BeanUtils.getProperty(p, "birthday"); Date birthday = p.getBirthday(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:SS"); System.out.println(sdf.format(birthday)); } }
注意:BeanUtils的setProperty()方法只支援對基本資料型別的操作,如需要操作複雜型別,則需要為BeanUtils註冊一個型別轉換器ConvertUtils.register(); 執行結果: