Professional Documents
Culture Documents
Topics
JavaBean as a component model Core concepts of JavaBeans Properties Event model Introspection Bean persistence Bean persistence in XML
Software Component
Software components are self-contained, reusable software units isual software components
!sin" visual application builder tools, visual software components can be composed into applets, applications, servlets, and composite components #ou perform t$is composition wit$in a "rap$ical user interface, and %ou can immediatel% see t$e results of %our wor&' Capture business lo"ic or state
4
What is a JavaBean?
JavaBeans) is a portable, platform-independent component model written in t$e Java pro"rammin" lan"ua"e *it$ t$e JavaBeans +PI %ou can create reusable, platform-independent components !sin" JavaBeans-compliant application builder tools suc$ as (etBeans or Eclipse, %ou can combine t$ese components into applets, applications, or composite components'
What is a JavaBean?
JavaBean components are &nown as beans' Beans are d%namic in t$at t$e% can be c$an"ed or customi,ed -$rou"$ t$e desi"n mode of a builder tool, %ou use t$e propert% s$eet or bean customi,er to customi,e t$e bean and t$en save .persist/ %our customi,ed beans'
Builder tools discover a bean0s features .t$at is, its properties, met$ods, and events/ b% a process &nown as introspection' Beans support introspection in two wa%s1
B% ad$erin" to specific rules, &nown as desi"n patterns, w$en namin" bean features B% e2plicitl% providin" propert%, met$od, and event information wit$ a related bean information class'
Properties
Properties are t$e appearance and be$avior c$aracteristics of a bean t$at can be c$an"ed at desi"n time Beans e2pose properties so t$e% can be customi,ed at desi"n time Builder tools introspect on a bean to discover its properties and e2pose t$ose properties for manipulation Customi,ation is supported in two wa%s1
Events
Beans use events to communicate wit$ ot$er beans + bean t$at is to receive events .a listener bean/ re"isters wit$ t$e bean t$at fires t$e event .a source bean/ Builder tools can e2amine a bean and determine w$ic$ events t$at bean can fire .send/ and w$ic$ it can $andle .receive/
10
Persistence
Persistence enables beans to save and restore t$eir state +fter c$an"in" a bean0s properties, %ou can save t$e state of t$e bean and restore t$at bean at a later time wit$ t$e propert% c$an"es intact -$e JavaBeans arc$itecture uses Java 3b4ect Seriali,ation to support persistence'
11
JavaBean Method
+ bean0s met$ods are no different from Java met$ods, and can be called from ot$er beans or a scriptin" environment B% default all public met$ods are e2ported
12
E amples of Beans
5!I ."rap$ical user interface/ component (on-visual beans, suc$ as a spellin" c$ec&er +nimation applet Spreads$eet application
13
Button Beans
Slider Bean
14
Properties
15
Properties
+ bean propert% is a named attribute of a bean t$at can affect its be$avior or appearance E2amples of bean properties include color, label, font, font si,e, and displa% si,e'
16
T#pes of Properties
Simple 6 + bean propert% wit$ a sin"le value w$ose c$an"es are independent of c$an"es in an% ot$er propert%' Inde2ed 6 + bean propert% t$at supports a ran"e of values instead of a sin"le value' Bound 6 + bean propert% for w$ic$ a c$an"e to t$e propert% results in a notification bein" sent to some ot$er bean' Constrained 6 + bean propert% for w$ic$ a c$an"e to t$e propert% results in validation b% anot$er bean' -$e ot$er bean ma% re4ect t$e c$an"e if it is not appropriate' 17
Event Model
18
Based t$e Java 7'7 event model +n ob4ect interested in receivin" events is an event listener 6 sometimes called event receiver +n ob4ect t$at "enerates .fire/ events is called an event source 6 sometimes called event sender Event listeners re"ister t$eir interest of receivin" events to t$e event source
Event source provides t$e met$ods for event listeners to call for re"istration
-$e event source maintains a list of listeners and invo&e t$em w$en an event occurs
19
Event listeners are re"istered to t$e event source t$rou"$ t$e met$ods provided b% t$e event source
addXXXListener removeXXXListener
20
Create %our own custom event class, named XXXEvent or use an e2istin" event class -$ere are e2istin" event class .i'e' +ctionEvent/ *rite XXXListener interface and provide implementation class of it -$ere are built-in listerner interfaces .i'e' +ctionListener/ +dd an addXXXListener and removeXXXListener met$ods, w$ere XXX stands for t$e name of t$e event -$ese met$ods are used b% event listeners for re"istration -$ere are built-in event source classes 21
;e"ister event listener to t$e event source t$rou"$ addXXXListener./ met$od of t$e event source
22
23
24
*e are "oin" to use ActionListener interface w$ic$ is alread% provided in J<= *e are "oin" to write ButtonHandler class w$ic$ implements ActionListener interface
25
*e are "oin" to use Button class w$ic$ is event source class and is alread% provided in J<= Button class alread% $as t$e followin" met$ods
add+ctionListener remove+ctionListener
27
28
frame+add2 -utton6 Border&a#out+DE$T' 31 frame+add2 area6 Border&a#out+CEDTE$ 31 frame+pacF231 frame+setGefaultCloseEperation2 WindowConstants+GISPESEHEDHC&ES frame+set&ocation$elativeTo2 null 31 frame+setIisi-le2 true 31 5
29
Event source invo&es event $andlin" met$od of all Event $andlers .event listener/ re"istered to it
30
Introspection
31
What is Introspection?
Introspection is t$e automatic process of anal%,in" a bean0s desi"n patterns to reveal t$e bean0s properties, events, and met$ods
-$is process controls t$e publis$in" and discover% of bean operations and properties
B% default, introspection is supported b% reflection, w$ere %ou name met$ods wit$ certain namin" patterns, li&e set?"etPropert%./ and add?removeListener./
32
?eatureGescriptor
33
Simple propert%
public void setPropert%(ame.Propert%-%pe value/@ public Propert%-%pe "etPropert%(ame./@ public void setPropert%(ame.boolean value/@ public boolean isPropert%(ame./@ public void setPropert%(ame.int inde2, Propert%-%pe value/@ public Propert%-%pe "etPropert%(ame.int inde2/@ public void setPropert%(ame.Propert%-%peAB value/@ public Propert%-%peAB "etPropert%(ame./@
34
Boolean propert%
Inde2ed propert%
Multicast events
public void addEventListener-%pe.EventListener-%pe l/@ public void removeEventListener-%pe.EventListener-%pe l/@ public void addEventListener-%pe.EventListener-%pe l/ t$rows -ooMan%ListenersE2ception@ public void removeEventListener-%pe.EventListener-%pe l/@ public met$ods
!nicast events
Met$ods
35
Bean Persistence
36
Bean Persistence
-$rou"$ ob4ect seriali,ation 3b4ect seriali,ation means convertin" an ob4ect into a data stream and writin" it to stora"e' +n% applet, application, or tool t$at uses t$at bean can t$en CreconstituteC it b% deseriali,ation' -$e ob4ect is t$en restored to its ori"inal state Dor e2ample, a Java application can seriali,e a Drame window on a Microsoft *indows mac$ine, t$e seriali,ed file can be sent wit$ e-mail to a Solaris mac$ine, and t$en a Java application can restore t$e Drame window to t$e e2act state w$ic$ e2isted on t$e Microsoft *indows mac$ine' 37
JM&Encoder Class
Enable beans to be saved in XML format -$e XMLEncoder class is assi"ned to write output files for te2tual representation of Seriali,able ob4ects
JM&Encoder encoder 4 new JM&Encoder2 new BufferedEutputStream2 new ?ileEutputStream2 7Beanarchive+ ml7 3 3 31 encoder+writeE-Cect2 o-Cect 31 encoder+close231
39
JM&Gecoder Class
XML<ecoder class reads an XML document t$at was created wit$ XMLEncoder1
JM&Gecoder decoder 4 new JM&Gecoder2 new BufferedInputStream2 new ?ileInputStream2 7Beanarchive+ ml7 3 3 31 E-Cect o-Cect 4 decoder+readE-Cect231 decoder+close231
40
E ample* SimpleBean
import 4ava'awt'Color@ import 4ava'beans'XML<ecoder@ import 4ava2'swin"'JLabel@ import 4ava'io'Seriali,able@ public class SimpleBean e2tends JLabel implements Seriali,able E public SimpleBean./ E set-e2t. C>ello worldFC /@ set3paGue. true /@ setBac&"round. Color';E< /@ setDore"round. Color'#ELL3* /@ set ertical+li"nment. CE(-E; /@ set>ori,ontal+li"nment. CE(-E; /@ H H
41
JavaBeans
43