javabeans (3)
TRANSCRIPT
-
8/8/2019 javabeans (3)
1/19
Java Beans & Serialization
CS-328
Dick Steflik
-
8/8/2019 javabeans (3)
2/19
Java Beans
Java based component technology
originally developed to provide java with acomponent technology like vbx/ocx of VB
originally used for user interface widgets
currently used forserverside componets
Requirements
Bean naming conventions
must be serializable, to support persistence
-
8/8/2019 javabeans (3)
3/19
Bean Naming Conventions
setters
set or modify a bean property valuemust start with set
getters
retrieve property values from a bean
must start with get
-
8/8/2019 javabeans (3)
4/19
Bean Properties Simple
representsasingle property and can be defined with a pair of get/set
methods
Indexed
representsan array of properties
Bound
a bound property notifies other objects when itsvalue changes
generatesa PropertyChange event with property name, old value and new
value
Constrained
an object with constrained propertiesallows other objects to veto a
constrained property value change
Constrained property listeners can veto a change by throwing a
PropertyVetoException
-
8/8/2019 javabeans (3)
5/19
Interaction Levels
Properties
Bean
Methods
Event
-
8/8/2019 javabeans (3)
6/19
Bean Framework
Network
Network
Bean
Bean
Bean Bean
Bean
Container Bean
-
8/8/2019 javabeans (3)
7/19
Introspection
the processby which a builder tool finds out
which properties, methods, and eventsa
bean supports.
searching for classesand methods that follow
certain naming conventions
by querying the BeanInfo ofa class BeanInfo isan interface whose methodsallow the
interrogation ofa beans properties, methodsand
events
-
8/8/2019 javabeans (3)
8/19
I
ntrospection
Introspector
Methods
Properties
Events
-
8/8/2019 javabeans (3)
9/19
R
eflection
The core APIs used by the JavaBeans
Introspector class
java.lang.reflect
-
8/8/2019 javabeans (3)
10/19
a simple GasTank BeanClass 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; }
}
-
8/8/2019 javabeans (3)
11/19
Jarring the Bean
Beansare packaged in a javaarchive (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 AWTand Swing classes
are beans
-
8/8/2019 javabeans (3)
12/19
GUI
Builders
Because ofa beans naming convention the
GUIs introspector can build a property sheet
for each bean automatically
The GUI builderallows you to configure
the bean as yourapp.Needs
Because the bean isserializable the
configured properties can be saves by the
GUI Builder
-
8/8/2019 javabeans (3)
13/19
-
8/8/2019 javabeans (3)
14/19
Persistance
for persistance a bean must implement the
Serializable interface
java.io.Serializable
saving of the beans fields isautomatic, you
need do nothing, everything is done by the
JavaBean API
the same applies to retrieving the beans
persistent values
-
8/8/2019 javabeans (3)
15/19
Loading and Saving an Object
-
8/8/2019 javabeans (3)
16/19
Saving an Objectpublic void SaveState(){
FileOutputStream fos = null;ObjectOutputStream oos = null;
try{fos = new
FileOutputStream(State.ser);oos = new
ObjectOutputStream(fos);oos.writeObject(currentState);}
catch(IOException e){System.out.println(e.toString;}
}
-
8/8/2019 javabeans (3)
17/19
R
estoring an Objectpublic void loadState(){FileInputStream fis = null;ObjectInputStream ois = null
try{fis = newFileInputStream(State.ser);
ois = new ObjectInputStream(fis);State currentState =
(State)ois.readObject();}
catch(ClassNotFoundException e){Systen.out.println(e.toString);}
-
8/8/2019 javabeans (3)
18/19
public void SaveState(){FileOutputStream fos = null;
ObjectOutputStream oos = null;
try{
fos = new
FileOutputStream(State.ser);oos = new
ObjectOutputStream(fos);
oos.writeObject(currentState);
}catch(IOException e){
System.out.println(e.toString;}}
-
8/8/2019 javabeans (3)
19/19
For more on JavaBeans
JavaBeans Home Page -
http://java.sun.com/beans/