內省—beanutils工具包 操作javabean屬性

內省—beanutils工具包 操作javabean屬性 比 內省Introspector類操作javabean屬性更加方便。內省—beanutils工具包 是Apache組織開發了一套用於操作JavaBean的API,Introspector是sun公司自己開發的一套用於操作JavaBean的API。
1. 新建工程,建包cn.itcast.beanutils,在包下建javabean類Student,代碼如下:

package cn.itcast.beanutils;

    import java.util.Date;

    public class Student {

        private String name;
        private String password;
        private String email;
        private int age;        
        private Date birthday;


        public Date getBirthday() {
            return birthday;
        }

        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        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;
        }

}
  1. 在cn.itcast.beanutils下建Demo類操作Student類的各屬性,代碼如下:
    需注意導入commons-beanutils-1.9.2.jar和commons-logging-1.0.4.jar包
package cn.itcast.beanutils;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

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.converters.DateConverter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;

public class Demo {
    /*內省—beanutils工具包 
    Apache組織開發了一套用於操作JavaBean的API*/

    @Test
    public void test1() throws Exception, InvocationTargetException{
        Student stu=new Student();
        String name="maomao";
        String password="123";
        String email="[email protected]";
        int age=23;
        //1. BeanUtils.setProperty設置Student stu的各屬性
        BeanUtils.setProperty(stu, "name", name);
        BeanUtils.setProperty(stu, "password", password);
        BeanUtils.setProperty(stu, "email", email);
        BeanUtils.setProperty(stu, "age",age);

        //2.BeanUtils.getProperty獲取Student stu的各屬性
        String Name=BeanUtils.getProperty(stu, "name");
        String Password=BeanUtils.getProperty(stu, "password");
        String Email=BeanUtils.getProperty(stu, "email");
        String Age=BeanUtils.getProperty(stu, "age");
        System.out.println("name="+Name+", password="+password+", email="+Email+", age="+Age);
    }

    @Test
    public void test2() throws Exception, InvocationTargetException{
        Student stu=new Student();
        //當數據類型間不能直接轉換時,需要調用轉換器實現數據類型的轉換
        String birthday="1999-09-09";
        //因爲字符串不能轉換成Date類型,所以此處需要註冊一個轉換器.下面代碼表示遇到Date類型的數據時調用DateConverter()轉換器
        ConvertUtils.register(new DateLocaleConverter(), Date.class);
        BeanUtils.setProperty(stu, "birthday",birthday);
        System.out.println(BeanUtils.getProperty(stu, "birthday"));
    }

    @Test
    public void test3() throws Exception, InvocationTargetException{
        Student stu=new Student();
        //當數據類型間不能直接轉換時,需要調用轉換器實現數據類型的轉換,自己編寫轉換器
        String birthday="1999-09-09";
        //下面代碼表示自己編寫一個實現字符串到日期類型的轉換器
        //註冊轉換器,    //Converter是接口類型,不能直接new 接口,new接口時必須實現接口的抽象方法
        ConvertUtils.register(new Converter(){
            public Object convert(Class type, Object value) { 
                if(value==null){
                    return null;
                }
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date date = null;
                try {
                    date = format.parse((String)value);
                } catch (ParseException e) {
                    throw new ConversionException(e);
                }
                return date;
            }       
        }, Date.class);
        BeanUtils.setProperty(stu, "birthday",birthday);
        System.out.println(BeanUtils.getProperty(stu, "birthday"));
    }

}
發佈了34 篇原創文章 · 獲贊 6 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章