190 likes | 454 Views
Java Beans & Serialization. CS-328 Dick Steflik. Java Beans. Java based component technology originally developed to provide java with a component technology like vbx/ocx of VB originally used for user interface widgets currently used for server side componets Requirements
E N D
Java Beans & Serialization CS-328 Dick Steflik
Java Beans • Java based component technology • originally developed to provide java with a component technology like vbx/ocx of VB • originally used for user interface widgets • currently used for server side componets • Requirements • Bean naming conventions • must be serializable, to support persistence
Bean Naming Conventions • “setters” • set or modify a bean property value • must start with “set” • “getters” • retrieve property values from a bean • must start with “get”
Bean Properties • Simple • represents a single property and can be defined with a pair of get/set methods • Indexed • represents an array of properties • Bound • a bound property notifies other objects when its value changes • generates a PropertyChange event with property name, old value and new value • Constrained • an object with constrained properties allows other objects to veto a constrained property value change • Constrained property listeners can veto a change by throwing a PropertyVetoException
Interaction Levels Methods Bean Properties Event
Bean Framework Bean Bean Bean Bean Network Bean Container Bean Network
Introspection • the processby which a builder tool finds out which properties, methods, and events a bean supports. • searching for classes and methods that follow certain naming conventions • by querying the BeanInfo of a class • BeanInfo is an interface whose methods allow the interrogation of a beans properties, methods and events
Introspection Methods Properties Events Introspector
Reflection • The core APIs used by the JavaBeans Introspector class • java.lang.reflect
a simple GasTank Bean Class Gastank implements Serializable{ private double capacity; private int percent_full; public void setCapacity(double pounds){ capacity = pounds; } public double getCapacity() { return Capacity; } public void setPercent_full( int p) { percent_full = p; } public int getPercent_full() { return percent_full; } }
Jarring the Bean • Beans are packaged in a java archive (jar) file • the jar utility comes with the JDK • the jar file must be explicitly in your class path • in JDK 1.2.x all AWT and Swing classes are beans
GUI Builders • Because of a bean’s naming convention the GUIs introspector can build a property sheet for each bean automatically • The GUI builder allows you to configure the bean as your app. Needs • Because the bean is serializable the configured properties can be saves by the GUI Builder
Beans Development Kit • Download the latest BDK from SUN • Comes with the Beanbox • allows building of bean applications • provides the jar utility • allows saving of configured beans
Persistance • for persistance a bean must implement the Serializable interface • java.io.Serializable • saving of the bean’s fields is automatic, you need do nothing, everything is done by the JavaBean API • the same applies to retrieving the bean’s persistent values
For more on JavaBeans • JavaBeans Home Page - http://java.sun.com/beans/