1 / 19

Java Beans & Serialization

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

Olivia
Download Presentation

Java Beans & Serialization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Beans & Serialization CS-328 Dick Steflik

  2. 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

  3. 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”

  4. 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

  5. Interaction Levels Methods Bean Properties Event

  6. Bean Framework Bean Bean Bean Bean Network Bean Container Bean Network

  7. 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

  8. Introspection Methods Properties Events Introspector

  9. Reflection • The core APIs used by the JavaBeans Introspector class • java.lang.reflect

  10. 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; } }

  11. 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

  12. 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

  13. 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

  14. 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

  15. Loading and Saving an Object

  16. Saving an Object

  17. Restoring an Object

  18. For more on JavaBeans • JavaBeans Home Page - http://java.sun.com/beans/

More Related