32_use BeanUtils toolkit to operate JavaBean and beanutilsjavabean

Source: Internet
Author: User

32_use BeanUtils toolkit to operate JavaBean and beanutilsjavabean

 

Some open-source warriors are not satisfied with the IntroSpector In the JavaBean API to operate beans because they require a lot of attribute setting values and obtaining values,

Write out the common BeanUtils tool to further simplify java bean operations and provide free download on the apache website.

 

Beanutils Toolkit

  • Demonstrate how to add a jar package with eclipse. First, introduce the beanutils package, and then introduce the logging package after an error occurs.
   
 
 
1 commons-beanutils-1.9.2-bin.zip http://u2l.info/3VN80n
2 commons-beanutils-1.9.2-src.zip http://u2l.info/3o99D3
3 commons-logging-1.1.3-bin.zip http://u2l.info/2D1d0m
4 commons-logging-1.1.3-src.zip http://u2l.info/nKLKp

 

Java. lang. NoClassDefFoundError: org/apache/commons/logging/LogFactory the jar package for logging is missing

This log package is used by many frameworks.

  • Based on the preceding example, use the BeanUtils class to get the previously set attribute and then set it to a new value.
    • The result returned by the get attribute is a string, and the set attribute can accept any type of objects. A string is usually used.

This is very suitable for the set object of the string passed by the browser.

  • Use the PropertyUtils class to first get the previously set attribute and then set it as a new value.
    • The result returned by the get attribute is the original type of the property, and the set attribute only accepts the original type of the property.
 
   

java bean

package com.itcast.day1;import java.util.Date;public class ReflectPoint {    private Date birthday=new Date();        private int x;    public int y;    public ReflectPoint(int x, int y) {        super();        this.x = x;        this.y = y;    }    @Override    public int hashCode() {        final int prime = 31;        int result = 1;        result = prime * result + x;        result = prime * result + y;        return result;    }    @Override    public boolean equals(Object obj) {        if (this == obj)            return true;        if (obj == null)            return false;        if (getClass() != obj.getClass())            return false;        ReflectPoint other = (ReflectPoint) obj;        if (x != other.x)            return false;        if (y != other.y)            return false;        return true;    }    public int getX() {        return x;    }    public void setX(int x) {        this.x = x;    }    public int getY() {        return y;    }    public void setY(int y) {        this.y = y;    }    public Date getBirthday() {        return birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }    @Override    public String toString() {        return "ReflectPoint [birthday=" + birthday + ", x=" + x + ", y=" + y                + "]";    }    }

  

Test class:

Package com. itcast. day2; 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 java. util. hashMap; import java. util. map; import org. apache. commons. beanutils. beanUtils; import org. apache. commons. beanutils. propertyUtils; import com. itcast. day1.ReflectPoint; public class IntroSpectorTest {public static void main (String [] args) throws Exception {ReflectPoint rf1 = new ReflectPoint (3, 4); Object value = 6; System. out. println (BeanUtils. getProperty (rf1, "x "). getClass (). getName (); // x is an int, but the value set by beanutils is java. lang. string System. out. println (rf1.getX (); BeanUtils. setProperty (rf1, "x", 1); // BeanUtils of the original type.SetProperty(Rf1, "x", "2"); // supports the String type setting value System. out. println (rf1.getX (); BeanUtils. setProperty (rf1, "birthday. time "," 111 "); // supports attribute cascade operation System. out. println (BeanUtils.GetProperty(Rf1, "birthday. time"). getClass (). getName (); // java. lang. String 111 Map mm = BeanUtils.Describe(Rf1); // convert javabean to map System. out. println (rf1); // ReflectPoint [birthday = Thu Jan 01 08:00:00 GMT + 08:00 1970, x = 2, y = 4] Map map = new HashMap (); map. put ("x", 1); map. put ("y", 1); BeanUtils.Populate(Rf1, map); // converts a map to a javabean System. out. println (rf1); // ReflectPoint [birthday = Thu Jan 01 08:00:00 GMT + 08:00 1970, x = 1, y = 1] // PropertyUtils. setProperty (rf1, "x", "9"); // An error occurred while running! Because PropertyUtils only supports the original type, this is not as powerful as BeanUtils! PropertyUtils. setProperty (rf1, "x", 9); System. out. println (rf1.getX (); // 9 }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.