I. JavaBean
1. Javabean is a special Java class that is mainly used to transmit data information. The methods in this Java class are mainly used to access private fields and the method name complies with certain naming rules.
If you want to transmit multiple information between two modules, You can encapsulate the information into a JavaBean. The instance object of this JavaBean is usually called a value object (VO ). This information is stored using private fields in the class. If you want to read or set the values of these fields, you need to use some corresponding methods to access them. What do you think are the best names of these methods? The attributes of JavaBean are determined based on the setter and getter methods, rather than the member variables. If the method name is setid, the Chinese meaning is Set ID. As for the variable to which you store the method, do you want to use it? If the method is named GETID, the Chinese meaning is to get the ID. Which variable do you use? Remove the set prefix. The remaining part is the attribute name. If the second letter of the remaining part is in lower case, change the first letter of the remaining part to a smaller one.
- Setid () attribute name ----> ID
- Attribute name of islast () ----> last
- What is the property name of setcpu? ----> CPU
- What is the property name of getups? ----> UPS
In short, when a class is used as a JavaBean, the attributes of the JavaBean are inferred based on the method name, and it cannot see the member variables inside the Java class.
2. A JavaBean-compliant class can be used as a normal class, but using it as a JavaBean certainly requires some additional benefits so that we can understand and apply JavaBean! Benefits:
- Javabean is often used in Java EE development. Many environments require operations in the form of JavaBean. If everyone else uses and requires such operations, you have no choice!
- JDK provides some APIs for operations on JavaBean, which is called introspection. If you want to use the getx method to access private X, how can this problem be solved? It is more convenient to use this API to operate JavaBean than to use a common class.
2. propertydescriptor beaninfo beanutils propertyutils for operations on JavaBean, see the code:
Import java. util. date; public class person {private string name; private int age; private date Birthday = new date (); // create an object here! Public date getbirthday () {return birthday;} public void setbirthday (date birthday) {This. birthday = birthday;} public person (string name, int age) {super (); this. name = Name; this. age = age;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public int getage () {return age;} public void setage (INT age) {This. age = age ;}@ overridepublic string tostring () {return this. name + "" + this. age + "" + this. birthday ;}}
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 Org. apache. commons. beanutils. beanutils; import Org. apache. commons. beanutils. propertyutils; public class introspectortest {/*** JavaBean */public static void main (string [] RGS) throws exception {person P = new person ("Zhang San", 23); string propertyname = "name"; // 1. the following uses propertydescriptor to operate javabeangetproperty (p, propertyname); object val = "zhangkai"; setproperty (p, propertyname, Val); // 2. use beaninfo to operate javabeangetpropertybybeaninfo (p, propertyname); // 3. the following uses beanutils to operate JavaBean. Beanutils. setproperty (p, propertyname, ""); // This is the property chain, and birthday is of the date type. The settime (long time) method in date is beanutils. setproperty (P, "birthday. time "," 11111 "); system. out. println (beanutils. getproperty (P, "birthday. time "); // In the beanutils operation, the returned results of the string type are also of the string type system. out. println (beanutils. getproperty (P, "birthday. time "). getclass (). getname (); // 4. propertyutils to operate javabeanpropertyutils. setproperty (P, "name", "Lily"); propertyutils. setproperty (P, "Age", 30); // The input value is intsystem. out. println (propertyutils. getproperty (P, "Age "). getclass (). getname (); // The returned value is integerpropertyutils. setproperty (P, "birthday. time ", 10000); system. out. println (p);} public static void getpropertybybeaninfo (person P, string propertyname) throws introspectionexception, illegalaccessexception, invocationtargetexception {beaninfo = introspector. getbeaninfo (P. getclass (); propertydescriptor [] PDS = beaninfo. getpropertydescriptors (); // retrieve the array to find the specified property for (propertydescriptor PD: PDS) {If (PD. getname (). equals (propertyname) {system. out. println (PD. getreadmethod (). invoke (p); break ;}}// set the attribute method public static void setproperty (person P, string propertyname, object Val) throws introspectionexception, illegalaccessexception, invocationtargetexception {propertydescriptor pd1 = new propertydescriptor (propertyname, P. getclass (); Method methodset = pd1.getwritemethod (); methodset. invoke (p, Val); // call the set method to set the name value to "Zhang Kai" system. out. println (p);} // obtain the property method Private Static void getproperty (person P, string propertyname) throws introspectionexception, illegalaccessexception, invocationtargetexception {// obtain the attribute description of the JavaBean. propertydescriptor Pd = new propertydescriptor (propertyname, P. getclass (); // obtain method methodget = PD. getreadmethod (); system. out. println (methodget. invoke (p); // call method }}
3. Other iterative labels with Struts cannot iterate over the attributes of enumeration elements, but the iterative labels with jstl can. When beanutils is used to obtain the attributes of member objects of enumeration classes with abstract methods, an error occurs. You must use the inner and brute force reflection methods to obtain the attributes. The main reason is that the abstract subclass of enumeration classes is not of the Public type.
public static void main(String[] args) {// TODO Auto-generated method stub/*System.out.println(PropertyUtils.getProperty(Sex.NONE, "title"));*/Object bean = Sex.NONE;BeanInfo beanInfo = null;try {beanInfo = Introspector.getBeanInfo(bean.getClass());} catch (Exception e1) {// TODO Auto-generated catch blocke1.printStackTrace();}PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();for(PropertyDescriptor property:properties){if(property.getName().equals("title")){Method method = property.getReadMethod();method.setAccessible(true);Object retVal;try {retVal = method.invoke(bean, null); System.out.println(retVal);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}