Summary of JavaBean

Source: Internet
Author: User

I have recently learned some of my summary below JavaBean. If you have any mistakes, I hope you can point them out.

I. Definition:

We can call it a JavaBean class that has set and get methods for attributes. In fact, JavaBean is a Java class. In this Java class, a rule is formed by default-setting and obtaining attributes. On the contrary, the Ava class is a JavaBean, which is wrong, because a Java class does not necessarily have the setting and obtaining methods for attributes (that is, not necessarily the set and get methods ). 2. The concept of inner province is introduced from Javabean: Setting the attribute access permissions that need to be set and obtained in class 1 to private (private) makes the external users invisible, the public (common) set and get methods are used to set and obtain the attribute values. What is the internal operation? It is called introspection. 3. Use introspection to explain the implementation principles of setproperty () and setproperty () methods in JavaBean. In fact, it is also the method of reflection.

Package itcast. test5;

Import java. Beans. beaninfo;
Import java. Beans. introspector;
Import java. Beans. propertydescriptor;
Import java. Lang. Reflect. invocationtargetexception;
Import java. Lang. Reflect. method;

Import javax. crypto. spec. psource;

Import org. Apache. commons. beanutils. beanutils;

/* 5. Write a method, public void setproperty (Object OBJ, string propertyname, object Value ){},
This method can set the value of the property named propertyname In the OBJ object to value. */
// The simple application of introspection is used to understand the implementation of setproperty () and getproperty () methods in JavaBean.
Public class setpropertytest {

/**
* @ Param ARGs
* @ Throws nosuchmethodexception
* @ Throws invocationtargetexception
* @ Throws illegalaccessexception
*/
Public static void main (string [] ARGs) throws exception {
// Todo auto-generated method stub
Point pt1 = new point (3, 5 );
String propertyname = "X ";
Object value = 7;

Getproperty (pt1, propertyname );

Setproperty (pt1, propertyname, value );
// The following are the values of the set and get attributes using the beanutil toolkit.
/* // System. Out. println (beanutils. getproperty (pt1, propertyname ));
Beanutils. setproperty (pt1, propertyname, "9 ");
System. Out. println (pt1.getx ());
// System. Out. println (pt1.getx ());
*/}
// Setproperty () method implementation
Private Static void setproperty (Object object, string propertyname, object Value ){
Try {
// Use the propertydescriptor (attribute operator class) in the Java. Beans package)
Propertydescriptor Pd2 = new propertydescriptor (propertyname, object. getclass ());
// Obtain the Set Method of the Point JavaBean through reflection
Method methodsetx = pd2.getwritemethod (); // getwritemethod () obtain the writable method in the JavaBean, that is, the set method.
Methodsetx. Invoke (object, value); // assign a value to the Method
} Catch (exception e ){
// Todo: handle exception
}
}
// Implement the getproperty () method
Private Static void getproperty (Object object, string propertyname ){

Try {
Propertydescriptor pd1 = new propertydescriptor (propertyname, object. getclass ());
Method methodgetx = pd1.getreadmethod ();
Object retval = methodgetx. Invoke (object );
System. Out. println (retval );
// The analysis below is a complicated method to obtain the attribute values in the JavaBean.
/* Beaninfo = introspector. getbeaninfo (object. getclass ());
// Call the getpropertydescriptors () method through the beaninfo object to obtain all attribute descriptions
Propertydescriptor [] PPS = beaninfo. getpropertydescriptors ();
Object retval = NULL;
The above operation is used to get the description of all attributes in the JavaBean. Next, all attributes are iterated and the attribute names are obtained through the getname () method of each attribute,
Compare with the property name I am looking for. If yes, perform the corresponding operation.
For (propertydescriptor PD: PDS ){
If (PD. getname (). Equals (propertyname )){
Method methodgetx = Pd. getreadmethod ();
Retval = methodgetx. Invoke (object );
Break;
}
}*/
// System. Out. println (retval );
} Catch (exception e ){
// Todo: handle exception
}

}

}

Class Point {
Private int X;
Private int y;
 
Public point (int x, int y ){
Super ();
This. x = X;
This. Y = y;
}

Public int getx (){
Return X;
}

Public void setx (int x ){
This. x = X;
}

Public int Gety (){
Return y;
}

Public void sety (INT y ){
This. Y = y;
}
 
}

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.