使用内省方式操作JavaBean

内省,英文中称作introspector。主要对javaBean进行操作,JavaBean是一个特殊的Java类,该类中方法名符合特定的规则(其实就是getXXX,setXXX),我们一般是利用get,set方法来推断属性的名称,而不是直接根据属性来获得名称,因为属性都是私有的,而get,set方法都是共有的。推断规则:如果第二个字母为小写,则首字母小写,例如:

  1. getAge—>age
  2. setage—>age

由于自己根据方法名来推断属性名称非常麻烦,因此我们可以通过内省的方式来调用set,get方法,看看下面的例子:

    @Test
    public void test4() {
        Person p1 = new Person();
        Object value = "zhangsan";
        String propertyName = "username";

        try {
            // 给属性设置值
            setProperty(p1, value, propertyName);
            // 获得属性值
            System.out.println(getProperty(p1, propertyName));
        } catch (IllegalAccessException | IllegalArgumentException
                | InvocationTargetException | IntrospectionException e) {
            e.printStackTrace();
        }
    }

    private Object getProperty(Object p1, String propertyName)
            throws IntrospectionException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        PropertyDescriptor pd1 = new PropertyDescriptor(propertyName,
                p1.getClass());
        Method methodGetProperty = pd1.getReadMethod();
        return methodGetProperty.invoke(p1);
    }

    private void setProperty(Object p1, Object value, String propertyName)
            throws IntrospectionException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        PropertyDescriptor pd1 = new PropertyDescriptor(propertyName,
                p1.getClass());
        Method methodSetProperty = pd1.getWriteMethod();
        methodSetProperty.invoke(p1, value);
    }

Person.java

public class Person {

    private String username;
    private String password;
    private int money;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }

}

使用JavaBean中提供的BeanInfo类来操作,这样略微麻烦,但也是一种实现方式:

    private Object getProperty(Object p1, String propertyName)
            throws IntrospectionException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        /*PropertyDescriptor pd1 = new PropertyDescriptor(propertyName,
                p1.getClass());
        Method methodGetProperty = pd1.getReadMethod();
        return methodGetProperty.invoke(p1);*/
        BeanInfo beanInfo = Introspector.getBeanInfo(p1.getClass());
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
        for(PropertyDescriptor pd : pds){
            if(pd.getName().equals(propertyName)){
                Method methodGetProperty = pd.getReadMethod();
                return methodGetProperty.invoke(p1);
            }
        }
        return null;
    }
原文地址:https://www.cnblogs.com/lenve/p/4518007.html