简单演示样例:
package com.asdfLeftHand.test; import java.beans.BeanDescriptor; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.MethodDescriptor; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; class Person { private String name; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class IntrospectorTest { /** * @param args * @throws IntrospectionException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Person person = new Person(); person.setAge(22); person.setName("小强"); BeanInfo beanInfo = Introspector.getBeanInfo(person.getClass()); //BeanInfo beanInfo = Introspector.getBeanInfo(Person.class); System.out.println("--------BeanDescriptor--------"); BeanDescriptor beanDesc = beanInfo.getBeanDescriptor(); Class cls = beanDesc.getBeanClass(); System.out.println(cls.getName()); System.out.println("--------MethodDescriptor-------"); MethodDescriptor[] methodDescs = beanInfo.getMethodDescriptors(); for (int i = 0; i < methodDescs.length; i++) { Method method = methodDescs[i].getMethod(); System.out.println(method.getName()); } System.out.println("--------PropertyDescriptor------"); PropertyDescriptor[] propDescs = beanInfo.getPropertyDescriptors(); for(int i = 0; i < propDescs.length; i++) { Method methodR = propDescs[i].getReadMethod(); if (methodR != null) { System.out.println("读方法:"+methodR.getName()); Object o= methodR.invoke(person); System.out.println(methodR.getName()+":"+o); } Method methodW = propDescs[i].getWriteMethod(); if (methodW != null) { System.out.println("写方法:"+methodW.getName()); if(methodW.getName().equals("setName")){ methodW.invoke(person,"小王"); System.out.println("调用"+methodW.getName()+"方法后的值为:"+person.getName());//此处为了方便就直接用person.getName()方法了 } } } } }
执行结果:
--------BeanDescriptor--------
com.asdfLeftHand.test.Person
--------MethodDescriptor-------
hashCode
wait
setAge
notifyAll
equals
wait
wait
toString
setName
getAge
notify
getClass
getName
--------PropertyDescriptor------
读方法:getAge
getAge:22
写方法:setAge
读方法:getClass
getClass:class com.asdfLeftHand.test.Person
读方法:getName
getName:小强
写方法:setName
调用setName方法后的值为:小王
一个简单应用:利用内省简化一系列类似的方法为一个通用的方法。
部分代码:
用到common BeanUtils包。
/** * 有一张图片Image表,存有何种对象相应的图像(如 用户头像),用hql语句查处相应的图片集合, * 各种对象字段有差别可是查询方法相似,就写一个通用的方法(相对通用) * 得到某个对象集合的图片。 * key:guid+id,value:address+fileName * @param list * @param type * @return */ public Map<String,String> getImagesMap(List<?> list,int imageType) { Map<String, String> imagesMap = new HashMap<String, String>(); List<?> entityList = list; for(int i=0;i<entityList.size();i++){ Object entity = entityList.get(i); String id = ""; String guid = ""; try { BeanInfo bi = Introspector.getBeanInfo(entity.getClass(), Object.class); PropertyDescriptor[] props = bi.getPropertyDescriptors(); for (int i2 = 0; i2 < props.length; i2++) { String str = props[i2].getName(); if(str.equals("guid")){ guid = BeanUtils.getProperty(entity, str); }else if(str.endsWith("ID")){ id = BeanUtils.getProperty(entity, str); } } } catch (Exception e) { e.printStackTrace(); } String hql2 = "from Image where guid='"+guid+"' and FKID='"+id+"' and type="+imageType"; List<Image> list2 = imageDao.query(hql2); //,,, } return imagesMap; }
这样全部须要图片的地方就仅仅须要调用这一个方法了。