[ Team LiB ] 10.3 How Beans Work
[ Team LiB ] 10.3 How Beans Work All the JSP/bean functionality is built on the ability of one Java class to discover and invoke methods on another class at runtime. The mechanism that supports this, introspection, has been built into Java since version 1.1. This extremely powerful capability is missing from many other object-oriented languages, in which everything must be known in advance; once a program is built, it may have to be changed significantly to extend it with new functionality. Introspection is possible because a lot of information about method names and signatures is stored in .class files, and certain methods can access and organize this information. An easy way to see the kinds of information that introspection provides is to use the javap utility, which is included in the JDK (Java Development Kit). Javap is run from the command line and is invoked with the name of a class. If it is given SimpleBean from Listing 10.1, Javap will generate the following output: Compiled from SimpleBean.javapublic synchronized class SimpleBean extends java.lang.Object /* ACC_SUPER bit set */ { public int getAge(); public void setAge(int); public java.lang.String getName(); public void setName(java.lang.String); public SimpleBean(); } Although javap does not use introspection to generate this output, the principle is the same. The utility is able to pull out the names of all the methods and the type of their arguments and to return values. From this, a person or a program could infer that there is a property called name that is a string, and so on. Introspection also provides a mechanism to create a new instance of an object once its class has been loaded. This mechanism will construct this instance by looking for a constructor that takes no arguments, which is why the programmer must provide one. As a convenience, if a class contains no constructors at all, Java will automatically provide one that takes no arguments and doesn’t do anything. However, it is always better to make such things explicit. The classes related to introspection are all in the java.beans and java.lang. reflectpackages, and the whole process starts with the java.beans. Introspector class. The use of these classes is beyond the scope of this blog, but readers are encouraged to peruse the JDK documentation to see how all this is accomplished. [ Team LiB ] Page 187
Note: If you are looking for reliable and quality webspace company to host and run your servlet application check professional servlet hosting services