Java basic review: JavaBean and Java internal API

Source: Internet
Author: User

About JavaBean: a class that complies with specific rules (JavaBean is just a specification !)

(1) private Field)

(2) provide an access method for private fields (getter/setter)

(3) Any number of business methods

 

IntroSpector is a default java method for handling Bean class attributes and events. Getter/setter is used to access attributes. Java provides a set of APIs used to access the getter/setter methods of a property. These APIs enable you to ignore this rule (but you 'd better clarify it ), these APIs are stored in the java. in beans, the general method is to get the BeanInfo information of an object through the getBeanInfo method of the Introspector class, and then get the property descriptor through BeanInfo ), you can use this property descriptor to obtain
Getter/setter methods, and then we can call these methods through the reflection mechanism.

 

You can access the JavaBean attribute in either of the following ways:

1) directly call the getter/setter method of bean.

2) access through internal technologies (in the java. beans package)

 

Student Class

Package web. java. introspector; public class Student {private String name; private String sno; public String getName () {return name;} public String getSno () {return sno ;} public void setName (String name) {this. name = name;} public void setSno (String sno) {this. sno = sno;}/** attributes and fields are different: * The field is the variable or constant declared at the top of the class * the property is Xxx after getXxx/setXxx * obviously, the age here is also the property */public int getAge () {return 21 ;}}

Test class: (Junit is used for testing)

Package web. java. introspector; import java. beans. beanInfo; import java. beans. introspector; import java. beans. propertyDescriptor; import java. lang. reflect. method; import org. junit. test; public class Demo1 {@ Testpublic void test1 () throws Exception {Student s1 = new Student ();/** 1. use the PropertyDescriptor class to operate Bean properties */PropertyDescriptor pd = new PropertyDescriptor ("name", Student. class); Method m = pd. getWrit EMethod (); m. invoke (s1, "well"); m = pd. getReadMethod (); String returnValue = (String) m. invoke (s1, null); System. out. println (returnValue);}/** get the BeanInfo of the Bean object through the Introspector class, * then get the property descriptor through BeanInfo ), * The property descriptor can be used to obtain the getter/setter methods corresponding to a property. * these methods are called through the reflection mechanism. * // @ Testpublic void test2 () throws Exception {// BeanInfo indicates all attributes of the Student object. BeanInfo bi = Introspector. getBeanInfo (Student. class); // obtain all the attributes of the Student object. getPropertyDescriptors (); for (PropertyDescriptor pd: PPS) {System. out. println (pd. getName (); // output age class name sno }}}

 

It is worth noting that any JavaBean has a class attribute from the Object class.

Related Article

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.