Java Reference
In-Depth Information
String getName() accessormethodanda void setName(String
name) mutator method.
• Instances of the class must be serializable.
Thefirstconventionallowsapplicationsandframeworkstoeasilyinstantiateabean,
thesecondconventionletsthemautomaticallyinspectandupdatebeanstate,andthe
third convention allows them to reliably store bean state to and restore bean state
from a persistent store (such as a file).
JavaBeans was created so that visual editors could present palettes of Swing com-
ponents(e.g., JList and JButton )thatdeveloperswouldaccesstoquicklycreate
graphical user interfaces. However, JavaBeans is applicable to any kind of
component-oriented editor.
JavaBeansisalsousefulwith activation frameworks thatdeterminethetypeofanar-
bitrarypieceofdata,encapsulateaccesstothedata,discovertheavailableoperations
for the data, and instantiate the appropriate bean to perform those operations.
Forexample,ifaJava-basedbrowserobtainedaJPEGimage,theJavaBeansActiv-
ationFrameworkwouldenablethebrowsertoidentifythatstreamofdataasaJPEG
image.Fromthattype,thebrowsercouldlocateandinstantiateanobjectformanip-
ulating or viewing that image.
FormoreinformationonJavaBeans,checkoutthe“JavaBeansTrail”inOracle'son-
line Java Tutorial ( http://download.oracle.com/javase/tutorial/
javabeans/TOC.html ) .
After creating the necessary output stream to library.ser and instantiating
XMLEncoder via Java 7's try-with-resources statement (to ensure proper resource
cleanup whether or not an exception is thrown), serialize() invokes XMLEn-
coder 's void writeObject(Object o) methodwith library asthismeth-
od'sargumentsothattheentiremapwillbeserialized.The deserialize() method
creates the necessary input stream to library.ser , instantiates XMLDecoder , in-
vokes this class's XMLDecoder 's Object readObject() method, and returns
the deserialized object returned from this method after casting it to Map<String,
Book> .
Lastly, Listing 11-11 declares a main() method that publishes this web service
on path /library of port 9902 of the local host, by executing End-
point.publish("http://localhost:9902/library",
new
Library()); .
Forcompleteness, Listing11-12 presentsthe Book class,whosebeansstoreinform-
ation about individual books.
Search WWH ::




Custom Search