使用Java的Introspector类操作JavaBean的属性

Introspector类是Java提供的操作JavaBean的属性的工具类


Introspector类操作JavaBean属性的示例:

JavaBean:Person

package com.test.introspector;

/**
 * Person
 * @author xuhu
 *
 */
public class Person
{
    //id
    private int id; 
    //姓名
    private String name;
    //年龄
    private int age;
    
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id = id;
    }
    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;
    }
}


IntrospectorTest:

package com.test.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;

/**
 * 内省测试类
 * @author xuhu
 *
 */
public class IntrospectorTest
{
    private Person p = new Person();
    
    /**
     * 获取Person类所有属性
     * @throws IntrospectionException 
     */
    @Test
    public void test1() throws IntrospectionException
    {
        BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
        //输出Person类所有属性
        for(PropertyDescriptor pd : pds)
        {
            System.out.println(pd.getName());
        }
    }
    
    /**
     * Person类属性赋值 方法1
     * @throws IntrospectionException 
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
     * @throws Exception 
     */
    @Test
    public void test2() throws Exception
    {
        BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
        //创建Person对象
        for(PropertyDescriptor pd : pds)
        {
            if("name".equals(pd.getName()))
            {
                Method method = pd.getWriteMethod();
                method.invoke(p, "测试用户");
            }
        }
        
        System.out.println(p.getName());
    }
    
    /**
     * Person类属性赋值 方法2
     * @throws Exception
     */
    @Test
    public void test3() throws Exception
    {
        PropertyDescriptor pd = new PropertyDescriptor("name", Person.class);
        Method method = pd.getWriteMethod();
        method.invoke(p, "测试用户2");
        
        System.out.println(p.getName());
    }
    
    
    /**
     * Person类属性读取方法
     * @throws Exception
     */
    @Test
    public void test4() throws Exception
    {
        PropertyDescriptor pd = new PropertyDescriptor("name", Person.class);
        Method method = pd.getWriteMethod();
        method.invoke(p, "测试用户2");
        
        method = pd.getReadMethod();
        String str = String.valueOf(method.invoke(p, null));
        
        System.out.println(str + "#");
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值