First, the use of beanutils
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.
BEANUTILSS Steps to use
1. Guide Pack Commons-logging.jar
2. Guide Pack Commons-beanutils-1.9.2.jar
Example Description:
@Test Public voidTestbeanutils ()throwsInvocationTargetException, illegalaccessexception {person P=NewPerson (); String ID= "110"; String name= "Zhang San"; String Salary= "10000"; String Birthday= "2010-10-19"; Beanutils.setproperty (P,"id", id); Beanutils.setproperty (P,"Name", name); Beanutils.setproperty (P,"Salary", salary); Convertutils.register (NewConverter () {@Override Public<T> T Convert (class<t>type, Object value) {T T=NULL; Try{SimpleDateFormat format=NewSimpleDateFormat ("Yyyy-mm-dd"); T=(T) format.parse ((String) value); } Catch(Exception e) {e.printstacktrace (); } returnT; }}, Date.class); Beanutils.setproperty (P,"Birthday", birthday); SYSTEM.OUT.PRINTLN (P); }
Second, path problems in Java
In Java programs, it is not appropriate to use absolute or relative paths in general, because the location of the Java program's jar is indeterminate, and the current path is not deterministic when executing the Java program. Typically in Java programs we put resources in classpath and then use the Classpath path to find resources.
Classpath Path: The current path of the classpath is used.
Example Description:
public class Demo {
public static void Main (string[] args) throws IOException {
Class clazz = new Demo (). GetClass ();
Prop.properties file in src root directory
InputStream is = Clazz.getresourceasstream ("/prop.properties");
Properties Properties = new properties ();
Properties.load (IS);
String name = Properties.getproperty ("username");
String pwd = properties.getproperty ("password");
System.out.println ("Username:" + name + ", Password:" + pwd);
}
}
Use of beanutils, path problems in Java