内省(Introspector)

内省(Introspector) 是Java 语言对 JavaBean 类属性、事件的一种缺省处理方法

目的:主要用于传递数据信息,这种类中的方法主要用于访问私有的字段(且方法名符合某种命名规则)

package day02.introspector;

public class Person {
    public String name;
    public int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getHigh() {
        return high;
    }
    public void setHigh(String high) {
        this.high = high;
    }
    public String high;
    public int getAb() {
        return age;
    }
}

//使用内省操作bean的属性

package day02.introspector;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.junit.Test;
//使用内省操作bean的属性 
public class Demo1 {
    //得到bean的所有属性
    @Test
    public void Test() throws IntrospectionException{
        BeanInfo info= Introspector.getBeanInfo(Person.class,Object.class);//拿到bean自己的属性
        PropertyDescriptor[] psd=info.getPropertyDescriptors();//得到属性描述器
        for(PropertyDescriptor pd:psd){
            System.out.println(pd.getName());
        }
    }
    
    //操作bean的属性:age
    @Test
    public void Test2() throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        Person p=new Person();
        PropertyDescriptor pd=new PropertyDescriptor("age",Person.class);
        //得到属性的写方法,为属性赋值
        Method method=pd.getWriteMethod();
        method.invoke(p,23);
        //获得属性值
        method=pd.getReadMethod();
        System.out.println(method.invoke(p, null));
        // System.out.println(p.getAge());
    }
    

}

以上代码可以运行后体验下

//高级点的操作:bean的操作属性类型
    @Test
    public void Test3 () throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        Person p=new Person();
        PropertyDescriptor pd=new PropertyDescriptor("age",Person.class);
        System.out.println(pd.getPropertyType());
    }
    

get set数据

注册日期转换器

@Test
    public void test2() throws IllegalAccessException, InvocationTargetException {
        String name = "aaaa";
        String high = "232";
        String age = "34";
        String birthday = "1995-12-10";
//        String today="1996-12-10";
//        ConvertUtils.register(new DateLocaleConverter(), Date.class);//方法之二:有bug,不要用!
        Person p = new Person();
        // 为了让日期赋到bean的birthday的属性上。我们给beanuntils注册一个日期转换器
        ConvertUtils.register(new Converter() {
            @Override
            public Object convert(Class arg0, Object value) {
                if (value == null) {
                    return null;
                }
                if (!(value instanceof String)) {
                    System.out.println("no");
                    throw new ConversionException("只转String");
                }
                String str = (String) value;
                if (str.trim().equals("")) {
                    return null;
                }
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return df.parse(str);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    throw new RuntimeException(e);// 异常链不能断
                }
            }
        }, Date.class);
        BeanUtils.setProperty(p, "name", name);
        BeanUtils.setProperty(p, "high", high);// 只支持8种基本类型自动转型
        BeanUtils.setProperty(p, "age", age);// 只支持8种基本类型自动转型
        BeanUtils.setProperty(p, "birthday", birthday);//通过转化器成功

        System.out.println(p.getName());
        System.out.println(p.getHigh());
        System.out.println(p.getAge());
        System.out.println(p.getBirthday());
//        Date date=p.getToday();
//        System.out.println(date.toLocaleString());
    }

用map

//用map来添数据
    @Test
    public void test5() throws IllegalAccessException, InvocationTargetException{
        Map map=new HashMap();
        map.put("name", "aaaa");
        map.put("high", "123");
        map.put("age", "23");
        map.put("birthday", "1995-12-10");
        ConvertUtils.register(new DateLocaleConverter(), Date.class);
        Person p=new Person();
        BeanUtils.populate(p, map);
        System.out.println(p.getName());
        System.out.println(p.getHigh());
        System.out.println(p.getAge());
        System.out.println(p.getBirthday());    
    }
既往不恋,未来不迎,当下不杂
原文地址:https://www.cnblogs.com/SnowingYXY/p/6043661.html