The explanation of JavaBean from introspection

Source: Internet
Author: User
Tags naming convention

introspector--internal inspection, learn more details--Introspection---JavaBean

First, JavaBean
    • JavaBean is a special Java class that is used primarily to pass data information, a method in a Java class in which the primary user accesses a private field, and the method name conforms to a certain naming convention.
    • If you want to pass more than one piece of information between two modules, you can encapsulate that information in a single javabean, which is often referred to as a value object (value, JavaBean, or VO).

This information is stored in classes using private fields, and if you read or set the values of these fields, this needs to be accessed by some appropriate means, what do you think the names of these methods are called?

The attribute stone of JavaBean is determined by the setter and getter methods in it, not according to the member variables. If the method name is SetID, the Chinese meaning is the set ID, which

Variable, with a tube? If the method name is GetID, the Chinese meaning is to get the ID, as to which variable you take from, use the tube? Remove the set prefix, the remainder is the property name, if the remainder of the second

If the letter is lowercase, the first letter of the remaining part is changed to small.

      • The property name of SetId ()->id
      • The property name of Islast ()->last
      • The property name of SetCPU ()->cpu
      • The property name of Getups ()->ups

In summary, when a class is used as a JavaBean, the JavaBean attribute is inferred from the method name, and it does not see the member variables inside the Java class at all.

    • A class that conforms to the JavaBean feature can be used as a normal class, but it is necessary to use it as a javabean to bring some additional benefits to understand and apply javabean!

The benefits are as follows:

      • In Java EE development, it is often used to javabean. Many environments require the operation according to JavaBean mode, others are so used and required to do so, where there is no room for selection!
      • The JDK provides some APIs for JavaBean operations, and this set of APIs becomes introspective. Get it if you want to go through the Getx method to access the private x, how to do it, there is a certain difficulty it?

It is easier to manipulate javabean with an introspective set of APIs than in a normal class.

Ii. Comprehensive cases of introspection
    • Demonstrates the setter and getter methods for automatically generating Reflectpoint with Eclipse.
    • Directly new a PropertyDescriptor object to let you understand the value of the JavaBean API, first read the JavaBean property with a piece of code, and then set the JavaBean properties in a piece of code.
 PackageCom.itcast.day2;ImportJava.beans.PropertyDescriptor;ImportJava.lang.reflect.Method;ImportCom.itcast.day1.ReflectPoint; Public classIntrospectortest { Public Static voidMain (string[] args)throwsexception{reflectpoint Rf1=NewReflectpoint (3,4); String PropertyName= "X"; //If you do not use the JavaBean API, you need to do this manually step by step through reflection, with the steps: 01. " X "-->02." X "-->03." GetX "-->04.methodgetx-->//ReadPropertyDescriptor pd=NewPropertyDescriptor (PropertyName, Rf1.getclass ()); Method Methodgetx=pd.getreadmethod ();//using the JavaBean API so far, 01, 02, 03, 04 have been completed.Object retval=Methodgetx.invoke (RF1);                                System.out.println (RetVal); //WriteObject value=6; PropertyDescriptor PD2=NewPropertyDescriptor (PropertyName, Rf1.getclass ()); Method Methodsetx=pd.getwritemethod ();//using the JavaBean API so far, 01, 02, 03, 04 have been completed.Methodsetx.invoke (Rf1,value);    System.out.println (Rf1.getx ()); }}
    • Demonstrates using eclipse to extract the journal code for reading properties and setting properties into methods, respectively:
      • As long as you call this method and pass an object, property name, and setting value to the method, it will be able to perform the property modification function.
      • The best way to get BeanInfo is to use "obj.getclass ()" Instead of "class name", which is a more generic procedure.

    • The X property of a Reflectpoint object is found and set by traversing all the properties of the BeanInfo. In the program to see a class as JavaBean, is called the Introspector.getbeaninfo method, the resulting BeanInfo object encapsulates this class as JavaBean look at the results of information .
Getting properties is worth two ways
 Public StaticObject GetProperty (Object Rf1, String PropertyName)throwsintrospectionexception, Illegalaccessexception, invocationtargetexception {//①now the way--simple, good! O (∩_∩) o~~        /*PropertyDescriptor pd=new PropertyDescriptor (PropertyName, Rf1.getclass ());        Method Methodgetx=pd.getreadmethod (); Object Retval=methodgetx.invoke (RF1);*/                //② The earliest way--traverse find, more troublesome  ̄へ ̄Object retval=NULL; BeanInfo BeanInfo=Introspector.getbeaninfo (Rf1.getclass ()); Propertydescriptor[] PDS=beaninfo.getpropertydescriptors ();  for(PropertyDescriptor Pd:pds) {if(Pd.getname (). Equals (PropertyName)) {Method Methodgetx=Pd.getreadmethod (); RetVal=Methodgetx.invoke (RF1);  Break; }        }        returnRetVal; }

The explanation of JavaBean from introspection

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.