美高梅开户-美高梅官方网站开户

您的位置:美高梅开户 > 美高梅开户 > 美高梅开户:Apache Commons Beanutils教程一(访问B

美高梅开户:Apache Commons Beanutils教程一(访问B

发布时间:2019-08-12 22:38编辑:美高梅开户浏览(63)

    Apache Commons Beanutils教程一(访问Bean属性),beanutilsbean

    拜访基本数据类型的Bean属性

    简述:

      那类属性指的是Integer, Double, Float, boolean等,,,, 注意这里还包罗String,其实像HashMap,ArrayList, 等个性都能够安装,只可是Map里面包车型大巴键值对、List索引处的值不可能透过那八个API访谈,必要选拔极其的API来拍卖,接下去将会介绍;

    访问API:

    • PropertyUtils.getSimpleProperty(Object, String)
    • PropertyUtils.setSimpleProperty(Object, String, Object)

    调用示例:

    /*
     * File Name: Main.java
     * Description: 
     * Author: PiChen
     * Create Date: 2017年5月29日
     */
    package apache.commons.beanutils.example.propertyaccess;
    
    import java.lang.reflect.InvocationTargetException;
    
    import org.apache.commons.beanutils.PropertyUtils;
    
    import apache.commons.beanutils.example.pojo.Employee;
    
    /**
     * 
     * @author PiChen
     * @version 2017年5月29日
     */
    
    public class BasicPropertyAccess
    {
    
        /**
         * 
         * 
         * @param args
         * @throws NoSuchMethodException
         * @throws InvocationTargetException
         * @throws IllegalAccessException
         */
    
        public static void main(String[] args)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
        {
            Employee employee = new Employee();
            String firstName = (String) PropertyUtils.getSimpleProperty(employee, "firstName");
            String lastName = (String) PropertyUtils.getSimpleProperty(employee, "lastName");
    
            firstName = firstName == null ? "Pi" : "";
            lastName = lastName == null ? "Chen" : "";
    
            PropertyUtils.setSimpleProperty(employee, "firstName", firstName);
            PropertyUtils.setSimpleProperty(employee, "lastName", lastName);
    
            System.out.println(employee.getFullName());
        }
    
    }
    

    做客基本数据类型的Bean属性

    简述:

      那类属性指的是Integer, Double, Float, boolean等,,,, 注意这里还包涵String,其实像HashMap,ArrayList, 等天性都得以安装,只可是Map里面包车型地铁键值对、List索引处的值不能够透过那七个API访谈,须要选择极其的API来拍卖,接下去将会介绍;

    访问API:

    • PropertyUtils.getSimpleProperty(Object, String)
    • PropertyUtils.setSimpleProperty(Object, String, Object)

    调用示例:

    /*
     * File Name: Main.java
     * Description: 
     * Author: PiChen
     * Create Date: 2017年5月29日
     */
    package apache.commons.beanutils.example.propertyaccess;
    
    import java.lang.reflect.InvocationTargetException;
    
    import org.apache.commons.beanutils.PropertyUtils;
    
    import apache.commons.beanutils.example.pojo.Employee;
    
    /**
     * 
     * @author PiChen
     * @version 2017年5月29日
     */
    
    public class BasicPropertyAccess
    {
    
        /**
         * 
         * 
         * @param args
         * @throws NoSuchMethodException
         * @throws InvocationTargetException
         * @throws IllegalAccessException
         */
    
        public static void main(String[] args)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
        {
            Employee employee = new Employee();
            String firstName = (String) PropertyUtils.getSimpleProperty(employee, "firstName");
            String lastName = (String) PropertyUtils.getSimpleProperty(employee, "lastName");
    
            firstName = firstName == null ? "Pi" : "";
            lastName = lastName == null ? "Chen" : "";
    
            PropertyUtils.setSimpleProperty(employee, "firstName", firstName);
            PropertyUtils.setSimpleProperty(employee, "lastName", lastName);
    
            System.out.println(employee.getFullName());
        }
    
    }
    

    Java Bean

    在介绍apache commons beanutils在此之前,很有供给先领会下javabean。

    apache commons beanutils便是基于JavaBeans的设计命名标准来促成的,如下是三个粗略的javabean示例:

    /*
     * File Name: Employee.java
     * Description: 
     * Author: PiChen
     * Create Date: 2017年5月29日
     */
    package apache.commons.beanutils.example.pojo;
    
    import java.util.Date;
    
    /**
     * 
     * @author PiChen
     * @version 2017年5月29日
     */
    
    public class Employee
    {
        private String firstName;
        private String lastName;
        private Date hireDate;
        private boolean isManager;/**
         * @return the firstName
         */
        public String getFirstName()
        {
            return firstName;
        }
    
        /**
         * @param firstName the firstName to set
         */
        public void setFirstName(String firstName)
        {
            this.firstName = firstName;
        }
    
        /**
         * @return the lastName
         */
        public String getLastName()
        {
            return lastName;
        }
    
        /**
         * @param lastName the lastName to set
         */
        public void setLastName(String lastName)
        {
            this.lastName = lastName;
        }
    
        /**
         * @return the hireDate
         */
        public Date getHireDate()
        {
            return hireDate;
        }
    
        /**
         * @param hireDate the hireDate to set
         */
        public void setHireDate(Date hireDate)
        {
            this.hireDate = hireDate;
        }
    
        /**
         * @return the isManager
         */
        public boolean isManager()
        {
            return isManager;
        }
    
        /**
         * @param isManager the isManager to set
         */
        public void setManager(boolean isManager)
        {
            this.isManager = isManager;
        }
    
        /**
         * @return the fullName
         */
        public String getFullName()
        {
            return firstName   " "   lastName;
        }
    
    
    }
    

    参照他事他说加以考察资料

    commons.apache.org/proper/commons-beanutils/javadocs/v1.9.3/apidocs/org/apache/commons/beanutils/package-summary.html

    源码链接:

    CommonsBeanutils教程一(访谈Bean属性),beanutilsbean BeanUtils简要描述 beanutils,以偏概全,是java bean的三个工具类,能够帮助我们有益的读...

    javabean一般有以下多少个特点:

    1、类必须是public访谈权限,且供给有贰个public的无参构造方法,之所以那样重大是方便人民群众利用Java的反射动态成立对象实例:

    Class beanClass = Class.forName(className);
    Object beanInstance = beanClass.newInstance();
    

    2、由于javabean的构造方法是无参的,所以大家的bean的一举一动配置(即设置bean的属性值,方法对应行为,属性对应数据)不能够在构造方法完毕,替代它的是由此一雨后冬笋的set方法来安装属性值,通过setter方法,大家能够变动javabean显示出来的表现和中间数据,这里的setter方法会按自然的预约来命名,如setHireDate、setName。。。

    3、读取和安装bean属性值的命名约定,即getter方法和setter方法,不过这里须求极度注意boolean类型的预订,如下示例:

        private String firstName;
        private String lastName;
        private Date hireDate;
        private boolean isManager;
        public String getFirstName();
        public void setFirstName(String firstName);
        public String getLastName();
        public void setLastName(String lastName);
        public Date getHireDate();
        public void setHireDate(Date hireDate);
        public boolean isManager();
        public void setManager(boolean manager);
    

    4、并非必须为各种属性提供setter和getter方法,大家能够只定义贰天性能的getter方法而不定义setter方法,那样的本性一般是只读属性;

    javabean一般有以下几个特点:

    1、类必须是public访谈权限,且需求有多个public的无参构造方法,之所以这么主若是低价使用Java的反射动态创立对象实例:

    Class beanClass = Class.forName(className);
    Object beanInstance = beanClass.newInstance();
    

    2、由于javabean的构造方法是无参的,所以大家的bean的展现配置(即设置bean的属性值,方法对应行为,属性对应数据)不能够在构造方法实现,替代它的是经过一多种的set方法来安装属性值,通过setter方法,大家得以变动javabean展现出来的作为和内部数据,这里的setter方法会按自然的预约来定名,如setHireDate、setName。。。

    3、读取和装置bean属性值的命名约定,即getter方法和setter方法,但是这里须要特别注意boolean类型的约定,如下示例:

        private String firstName;
        private String lastName;
        private Date hireDate;
        private boolean isManager;
        public String getFirstName();
        public void setFirstName(String firstName);
        public String getLastName();
        public void setLastName(String lastName);
        public Date getHireDate();
        public void setHireDate(Date hireDate);
        public boolean isManager();
        public void setManager(boolean manager);
    

    4、并不是必须为每一个属性提供setter和getter方法,大家能够只定义两日性质的getter方法而不定义setter方法,那样的习性一般是只读属性;

    BeanUtils简要描述

    beanutils,望文生义,是java bean的二个工具类,可以支持大家有利的读取(get)和装置(set)bean属性值、动态定义和做客bean属性;

    紧密的话,会发觉其实JDK已经提供了多个java.beans包,同样能够实现以上作用,只然则使用起来相比费心,所以诞生了apache commons beanutils;

    看源码就知道,其实apache commons beanutils正是凭仗jdk的java.beans包完结的。

    访谈索引类型的Bean属性

    简述:

      可索引的性质,如ArrayList, 数组等,能够经过下标索引来访谈Bean属性的值, 同理可安装value;

    访问API

    • PropertyUtils.getIndexedProperty(Object, String)
    • PropertyUtils.getIndexedProperty(Object, String, int)
    • PropertyUtils.setIndexedProperty(Object, String, Object)
    • PropertyUtils.setIndexedProperty(Object, String, int, Object)

    调用示例

    Bean:

    美高梅开户 1package apache.commons.beanutils.example.pojo; import java.util.List; public class IndexedBean { private List<Employee> employeeList; private Integer[] intArr; /** * @return the employeeList */ public List<Employee> getEmployeeList() { return employeeList; } /** * @param employeeList the employeeList to set */ public void setEmployeeList(List<Employee> employeeList) { this.employeeList = employeeList; } /** * @return the intArr */ public Integer[] getIntArr() { return intArr; } /** * @param intArr the intArr to set */ public void setIntArr(Integer[] intArr) { this.intArr = intArr; } } View Code

    调用example:

    /*
     * File Name: Main.java
     * Description: 
     * Author: PiChen
     * Create Date: 2017年5月29日
     */
    package apache.commons.beanutils.example.propertyaccess;
    
    import java.lang.reflect.InvocationTargetException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.commons.beanutils.PropertyUtils;
    
    import apache.commons.beanutils.example.pojo.Employee;
    import apache.commons.beanutils.example.pojo.IndexedBean;
    
    /**
     * 
     * @author PiChen
     * @version 2017年5月29日
     */
    
    public class IndexedPropertiesAccess
    {
    
        /**
         * 
         * 
         * @param args
         * @throws NoSuchMethodException
         * @throws InvocationTargetException
         * @throws IllegalAccessException
         */
    
        public static void main(String[] args)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
        {
            // 初始工作
            IndexedBean indexedBean = new IndexedBean();
            List<Employee> employeeList = new ArrayList<Employee>();
            Employee e1 = new Employee();
            e1.setLastName("Chen");
            Employee e2 = new Employee();
            e2.setLastName("Wang");
            employeeList.add(e1);
            employeeList.add(e2);
            indexedBean.setEmployeeList(employeeList);
            indexedBean.setIntArr(new Integer[]{ 0, 1, 2 });
    
            // API测试
            int index0 = 0;
            String name0 = "employeeList["   index0   "]";
            Employee employee0 = (Employee) PropertyUtils.getIndexedProperty(indexedBean, name0);
            System.out.println(employee0.getLastName());
    
            int index1 = 1;
            String name1 = "employeeList["   index1   "]";
            Employee employee1 = (Employee) PropertyUtils.getIndexedProperty(indexedBean, name1);
            System.out.println(employee1.getLastName());
    
            Employee employee00 = (Employee) PropertyUtils.getIndexedProperty(indexedBean,"employeeList", 0);
            Employee employee11 = (Employee) PropertyUtils.getIndexedProperty(indexedBean,"employeeList", 1);
            System.out.println(employee00.getLastName());
            System.out.println(employee11.getLastName());
    
            Integer i = (Integer) PropertyUtils.getIndexedProperty(indexedBean,"intArr", 1);
            System.out.println(i);
        }
    
    }
    

    做客Map映射类型的Bean属性

    简述:

      常见的HashMap,TreeMap等,能够经过key来访谈Bean属性值,同理可安装value;

    访问API:

    • PropertyUtils.getMappedProperty(Object, String)
    • PropertyUtils.getMappedProperty(Object, String, String)
    • PropertyUtils.setMappedProperty(Object, String, Object)
    • PropertyUtils.setMappedProperty(Object, String, String, Object)

    调用示例:

    map bean:

    美高梅开户 2美高梅开户 3

    /*
     * File Name: MappedBean.java
     * Description: 
     * Author: PiChen
     * Create Date: 2017年5月29日
     */
    package apache.commons.beanutils.example.pojo;
    
    import java.util.Map;
    
    /**
     * 
     * @author    PiChen
     * @version   2017年5月29日
     */
    
    public class MappedBean
    {
        private Map<String, Object> mapProperty;
    
        /**
         * @return the mapProperty
         */
        public Map<String, Object> getMapProperty()
        {
            return mapProperty;
        }
    
        /**
         * @param mapProperty the mapProperty to set
         */
        public void setMapProperty(Map<String, Object> mapProperty)
        {
            this.mapProperty = mapProperty;
        }
    
    }
    

    View Code

    使用example:

    /*
     * File Name: MapPropertyAccess.java
     * Description: 
     * Author: PiChen
     * Create Date: 2017年5月29日
     */
    package apache.commons.beanutils.example.propertyaccess;
    
    import java.lang.reflect.InvocationTargetException;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.apache.commons.beanutils.PropertyUtils;
    import apache.commons.beanutils.example.pojo.MappedBean;
    
    /**
     * 
     * @author    PiChen
     * @version   2017年5月29日
     */
    
    public class MapPropertyAccess
    {
    
        public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
        {
            MappedBean employee = new MappedBean();
            Map<String, Object> map = new HashMap<String, Object>();
            //employee.setMapProperty(map);
            PropertyUtils.setSimpleProperty(employee, "mapProperty", map);
    
            PropertyUtils.setMappedProperty(employee, "mapProperty", "testKey1", "testValue1");
            PropertyUtils.setMappedProperty(employee, "mapProperty(testKey2)", "testValue2");
    
            System.out.println(employee.getMapProperty().get("testKey1"));
            System.out.println(employee.getMapProperty().get("testKey2"));
    
        }
    }
    

    本文由美高梅开户发布于美高梅开户,转载请注明出处:美高梅开户:Apache Commons Beanutils教程一(访问B

    关键词: 美高梅开户

上一篇:美高梅开户:每天一道Java题

下一篇:没有了