JavaBean和内省

JavaBean

 

Bean的属性,字段对外提供了get或set方法那这个字段叫做属性,一个bean的属性不是由字段决定的,而是由get或set方法决定,只要有相对应的get或set方法就会有相对应的属性

 

内省

import java.beans.BeanInfo;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.Method;

 

import org.junit.Test;

 

public class Demo1 {

    @Test

    public void test1() throws Exception {

       BeanInfo info = Introspector.getBeanInfo(Person.class,Object.class);

       PropertyDescriptor[] pds = info.getPropertyDescriptors();

       for (PropertyDescriptor ps : pds) {

           System.out.println(ps.getName());

       }

    }

   

    //操纵bean的指定属性:age

    @Test

    public void test2() throws Exception{

       Person p=new Person();

       PropertyDescriptor pd=new PropertyDescriptor("age",Person.class);

       //得到属性的写方法,为属性赋值

       Method method=pd.getWriteMethod();

       method.invoke(p, 45);

      

       //获取属性的值

       method=pd.getReadMethod();

       System.out.println(method.invoke(p, null));

    }

    @Test

    public void test3() throws Exception{

       Person p=new Person();

        PropertyDescriptor pd=new PropertyDescriptor("age",Person.class);

       System.out.println(pd.getPropertyType());

    }

}

 

 

 

 

 

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

 

import org.apache.commons.beanutils.BeanUtils;

import org.apache.commons.beanutils.ConversionException;

import org.apache.commons.beanutils.ConvertUtils;

import org.apache.commons.beanutils.Converter;

import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

import org.junit.Test;

 

public class Demo1 {

    @Test

    public void test1() throws Exception{

       Person p=new Person();

       BeanUtils.setProperty(p," name", "nannan");

      

       System.out.println(BeanUtils.getProperty(p, "name"));

    }

   

   

   

   

   

    @Test

    public void test2() throws Exception, Exception{

       String birthday="1990-06-01";

       Person p=new Person();

       //为了让日期赋值到birthday属性上,我们给beanUtils注册一个日期转换器

       ConvertUtils.register(new Converter(){

 

           @Override

           public Object convert(Class type, Object value) {

              if(value==null){

                  return null;

              }

              if(!(value instanceof String)){

                  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) {

                  throw new RuntimeException(e);//异常链不能断

              }

           }

 

       }, Date.class);

       //Date date=p.getBirthday();

       BeanUtils.setProperty(p, "birthday", birthday);

       Date date=p.getBirthday();

      

       System.out.println(date.toLocaleString());

    }

   

    @Test

    public void test3() throws Exception{

       String birthday="1990-06-01";

       ConvertUtils.register(new DateLocaleConverter(), Date.class);

      

       Person p=new Person();

       BeanUtils.setProperty(p, "birthday", birthday);

       System.out.println(p.getBirthday().toLocaleString());

      

    }

   

    @Test

    public void test4() throws Exception{

       Map map=new HashMap();

        map.put("name", "aaa");

       map.put("password","123" );

       map.put("birthday","1990-06-01");

      

       ConvertUtils.register(new DateLocaleConverter(), Date.class);

       Person bean=new Person();

       BeanUtils.populate(bean, map);//用map集合中的值填充bean的属性

      

       System.out.println(bean.getBirthday().toLocaleString());

       System.out.println(bean.getName());

       System.out.println(bean.getPassword());

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值