32_ using the Beanutils toolkit operation JavaBean

Source: Internet
Author: User

Because of the many requirements for setting values and getting values for attributes, there are some open source warriors who are not satisfied with Introspector in the JavaBean API to manipulate beans.

A generic Beanutils tool has been written to further simplify the operation of Java beans, and open source is available for free download on the Apache website.

Beanutils Tool Kit

    • Demonstrate how eclipse joins the jar package by introducing the Beanutils package first, and then introducing the logging package when the program runs out of error.
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 Missing logging jar Package

This log package, many frameworks are in use.

    • On the basis of the previous introspection example, the Beanutils class first get the previously set property, and set it to a new value.
      • The Get property returns the result as a string, and the Set property can accept any type of object, typically using a string.

This is ideal for strings that the browser passes over to set the object.

    • Use the Propertyutils class to get the previously set property, and set it to a new value.
      • The Get property returns the result for the property's original type, and the Set property only accepts the property's original type.

Java Bean

 PackageCom.itcast.day1;Importjava.util.Date; Public classReflectpoint {PrivateDate birthday=NewDate (); Private intx;  Public inty;  PublicReflectpoint (intXinty) {Super();  This. x =x;  This. y =y; } @Override Public inthashcode () {Final intPrime = 31; intresult = 1; Result= Prime * result +x; Result= Prime * result +y; returnresult; } @Override Public Booleanequals (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 intGetX () {returnx; }     Public voidSetX (intx) { This. x =x; }     Public intGetY () {returny; }     Public voidSety (inty) { This. y =y; }     PublicDate Getbirthday () {returnbirthday; }     Public voidsetbirthday (Date birthday) { This. Birthday =birthday; } @Override PublicString toString () {return"Reflectpoint [birthday=" + Birthday + ", x=" + x + ", y=" +y+ "]"; }    }

Test class:

 PackageCom.itcast.day2;ImportJava.beans.BeanInfo;Importjava.beans.IntrospectionException;ImportJava.beans.Introspector;ImportJava.beans.PropertyDescriptor;Importjava.lang.reflect.InvocationTargetException;ImportJava.lang.reflect.Method;ImportJava.util.HashMap;ImportJava.util.Map;Importorg.apache.commons.beanutils.BeanUtils;Importorg.apache.commons.beanutils.PropertyUtils;ImportCom.itcast.day1.ReflectPoint; Public classIntrospectortest { Public Static voidMain (string[] args)throwsexception{reflectpoint Rf1=NewReflectpoint (3,4); Object value=6; System.out.println (Beanutils.getproperty (Rf1,"X"). GetClass (). GetName ());//x is int, but is java.lang.String when setting value with BeanutilsSystem.out.println (Rf1.getx ()); Beanutils.setproperty (Rf1,"X", 1);//original Type set valueBeanutils.SetProperty(Rf1, "X", "2");//support String Type set valueSystem.out.println (Rf1.getx ()); Beanutils.setproperty (Rf1,"Birthday.time", "111");//support for attribute cascade operationsSystem.out.println (beanutils.GetProperty(Rf1, "Birthday.time"). GetClass (). GetName ());//java.lang.String 111Map mm=beanutils.describe(RF1);//JavaBean turn into mapSystem.out.println (RF1);//Reflectpoint [Birthday=thu Jan 08:00:00 gmt+08:00 1970, x=2, Y=4]map Map=NewHashMap (); Map.put ("X", 1); Map.put ("Y", 1); Beanutils. Populate (Rf1, map);//convert map to JavaBeanSystem.out.println (RF1);//Reflectpoint [Birthday=thu Jan 08:00:00 gmt+08:00 1970, X=1, Y=1]//Propertyutils.setproperty (Rf1, "X", "9");//Error Running! Because propertyutils only support the original type, this is not beanutils powerful! Propertyutils.setproperty (Rf1, "X", 9); System.out.println (Rf1.getx ());//9    }}

32_ using the Beanutils toolkit operation JavaBean

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.