Java Framework Beanutils Exercises

Source: Internet
Author: User

Introspective-----> A perverted reflex
Beanutils The main problem: encapsulating the object's attribute data into an object. Making the data read from a file easier to assign to objects;

Benefits of Beanutils:
1. Beanutils when setting a property value, Beanutils automatically helps me convert the data type if the property is the base data type.
2. Beanutils when setting a property value, the underlying is also dependent on the Get or set method setting and getting the value of the property.
3. Beanutils set the property value, if the property is set to another reference type data, then you must register a type converter.

Code Exercise:

EMP class:

 Packagecom.java.base;Importjava.sql.Date; Public classEMP {Private intID; PrivateString name; Private Doublesalary; PrivateDate birthday;  PublicDate Getbirthday () {returnbirthday; }     Public voidsetbirthday (Date birthday) { This. Birthday =birthday; }     PublicEMP (intID, String name,Doublesalary) {        Super();  This. ID =ID;  This. Name =name;  This. Salary =salary; }         Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public Doublegetsalary () {returnsalary; }     Public voidSetsalary (Doublesalary) {         This. Salary =salary; }     PublicEmp () {} @Override PublicString toString () {return"No.:" + This. ID + "Name:" + This. Name + "Salary:" + This. Salary + "Date of birth:" + This. Birthday; }    }

Beanutils Code:

 PackageCom.java.fram;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.lang.reflect.Constructor;Importcom.java.base.Emp;ImportCom.java.base.Person;ImportJava.lang.reflect.Field;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importorg.apache.commons.beanutils.BeanUtils;Importorg.apache.commons.beanutils.ConvertUtils;ImportOrg.apache.commons.beanutils.Converter;/*Introspection-----> A Perverted reflection beanutils the main problem: encapsulating the object's attribute data into an object. Benefits of Beanutils: 1.      Beanutils when setting a property value, Beanutils automatically helps me convert the data type if the property is the base data type.      2. Beanutils when setting a property value, the underlying is also dependent on the Get or set method setting and getting the value of the property.       3. Beanutils set the property value, if the property is set to another reference type data, then you must register a type converter. */ Public classBeanutilsexercise { Public Static voidMain (string[] args)throwsException {//WriteFile ();//Person p = (person) getinstance ();//System.out.println (p);String id = "001"; String name= "Zhang San"; String Salary= "1000"; String Birthday= "2013-12-10"; //Registering a type converterConvertutils.register (NewConverter () {@Override PublicObject Convert (Class type, Object value) {Date Date=NULL; Try{SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd"); Date=(Date) dateformat.parse ((String) value); }Catch(Exception e) {e.printstacktrace (); }                returndate; }}, Date.class); EMP e=NewEmp (); Beanutils.setproperty (E,"id", id); Beanutils.setproperty (E,"Name", name); Beanutils.setproperty (E,"Salary", salary); Beanutils.setproperty (E,"Birthday", birthday);    System.out.println (e); }           Public Static voidWriteFile ()throwsexception{bufferedwriter BufferedWriter=NewBufferedWriter (NewFileWriter ("Obj.txt")); Bufferedwriter.write ("Com.java.base.Person");         Bufferedwriter.newline (); Bufferedwriter.write ("Id=1");         Bufferedwriter.newline (); Bufferedwriter.write ("Name= Harry");         Bufferedwriter.newline (); Bufferedwriter.write ("Age=25");         Bufferedwriter.newline ();              Bufferedwriter.close (); }      Public StaticObject getinstance ()throwsexception{WriteFile (); BufferedReader BufferedReader=NewBufferedReader (NewFileReader ("Obj.txt")); String ClassName=Bufferedreader.readline (); Class Clazz=Class.forName (className); Constructor Constructor= Clazz.getconstructor (NULL);//get the construction method;Object person = constructor.newinstance (NULL); //get an instance by constructing a methodString line =NULL;  while(line = Bufferedreader.readline ())! =NULL) {string[] datas= Line.split ("="); Field field= Clazz.getdeclaredfield (datas[0]); if(Field.gettype () = =int.class{field.set (person, integer.parseint (datas[1])); }             Else{field.set (person, datas[1]); }         }         returnPerson ; }}

Java Framework Beanutils Exercises

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.