1. The way of introspection developed by Apache
Sun's introspection API is too cumbersome, so the Apache organization has developed a set of simple, Easy-to-use API action Bean properties, combined with a lot of actual development scenarios--beanutils
L Beanutils Tool Kit Common classes:
beanutils
propertyutils
Convertutils.regsiter (Converter convert, Class clazz)
• Custom Converters
Specific case Analysis:
Javabean:person
packageCom.java.Bean;
import java.util.Date;
Publicclass person {
Private String name;
Privateintage;
Private Date birthday;
Public String GetName () {
return name;
}
publicvoid setname (String name) {
this. Name = name;
}
publicint getage () {
return age;
}
publicvoid setage (int age) {
this. Age = age;
}
Public Date Getbirthday () {
return birthday;
}
publicvoid setbirthday (Date birthday) {
this. Birthday = Birthday;
}
Public String Getabc () {
Returnnull;
}
}
Main code:
Package Com.java.Bean;
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;
Importorg.apache.commons.beanutils.locale.converters.DateLocaleConverter;
Import Org.junit.Test;
public class Beanutil {
@Test//Direct test method to debug a program
Public Voidtest1 () throws Illegalaccessexception, InvocationTargetException {
Person P =new person ();
Beanutils.setproperty (P, "name", "John");
System.out.println (P.getname ());
}
@Test
Beanutils tools can automatically convert types to basic data types, but special types such as data cannot be converted
Public Voidtest2 () throws Illegalaccessexception, InvocationTargetException {
Person P =new person ();
An anonymous class is used in a custom converter, and invoking convert is an excuse not to instantiate it directly, but to use its instantiated object, you can only anonymously class the method, or create a class and implement the interface as before
Convertutils.register (Newconverter () {
@Override
Publicobject convert (Class type, Object value) {
if (value==null) {
Returnnull;
}
if (!) ( Valueinstanceof String)) {
Thrownew conversionexception ("Convert only String type ... ");
}
String s= (string) value;
/*if (S.trim (). Equals ("")) {
Returnnull;
}*/
SIMPLEDATEFORMATSDF = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
try {
Dated = Sdf.parse (s);
Returnd;
catch (ParseException e) {
Thrownew conversionexception ("Conversion error ...) ");
}
}
},date.class);
String name = "Dick";
String age = "21";
Stringbirthday = "1992-01-01 02:01:28";
Beanutils.setproperty (P, "name", name);
Beanutils.setproperty (P, ' age ', age);
Beanutils.setproperty (P, "Birthday", birthday);
System.out.println (P.getname () + "..." +p.getage () + "..." +p.getbirthday (). toLocaleString ());
There will be an error: Org.apache.commons.beanutils.ConversionException:DateConverter does not support default String to ' Date ' Conversion.
Then you need to customize a converter
}
Beanutils to the provided conversion class
@Test
Public Voidtest3 () throws Illegalaccessexception, InvocationTargetException {
Convertutils.register (Newdatelocaleconverter (), date.class);
Person P =new person ();
Stringbirthday = "1992-01-10"; Beanutils's own converter has bugs, and when you enter a null value of the date will be wrong, so the actual development of the use of their own definition of the converter.
Beanutils.setproperty (P, "Birthday", birthday);
System.out.println (P.getbirthday ());
In actual development, a string string like the one used above is a form that is filled out by Web server client users
}
}