introduction to java beans from anders børjesson
DESCRIPTION
Introduction to JavaBeans3 Characteristics Visibility –Some JavaBeans are visual Buttons, TextFields, etc. –Some JavaBeans are “invisible” Model components (worker classes) Size (granularity) –Some JavaBeans are small (in terms of features) Buttons, TextFields, etc. –Some JavaBeans are big (in terms of features) SMTP bean, entire applicationsTRANSCRIPT
![Page 1: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/1.jpg)
Introduction to Java Beans
From Anders Børjesson
![Page 2: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/2.jpg)
Introduction to JavaBeans 2
JavaBeans components
• JavaBeans are the components in the Java environment– COM / ActiveX are components in the
Microsoft environment• Many classes in J2SE are JavaBeans
– Like the Java Swing classes
![Page 3: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/3.jpg)
Introduction to JavaBeans 3
Characteristics
• Visibility– Some JavaBeans are visual
• Buttons, TextFields, etc.
– Some JavaBeans are “invisible”• Model components (worker classes)
• Size (granularity)– Some JavaBeans are small (in terms of features)
• Buttons, TextFields, etc.
– Some JavaBeans are big (in terms of features)• SMTP bean, entire applications
![Page 4: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/4.jpg)
Introduction to JavaBeans 4
Requirements for JavaBeans classes
• Must have a no-arg (no parameters) constructor– More constructors are allowed– Remember: If you add a constructor with parameters
then the default no-arg constructor is gone.• Must implements java.io.Serializable• Data should not be public
– Data should be accesses via get and set methods• Should be thread safe
– Use synchronized carefully
![Page 5: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/5.jpg)
Introduction to JavaBeans 5
Non-requirements for JavaBeans
• An applet must extend java.lang.Applet• A servlet must extend javax.servlet.Servlet• A thread must implement
java.lang.Runnable• Beans does not need to extend or
implement anything special– Except for java.io.Serializable
• Which is only a marker interface
![Page 6: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/6.jpg)
Introduction to JavaBeans 6
JavaBeans terms
• Properties– To be read or set by other objects
• Get and set methods
• Methods– To be called by other objects
• Ordinary Java methods
• Events– Sent to other objects when something happens to the
bean object• Like a property with a new value
![Page 7: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/7.jpg)
Introduction to JavaBeans 7
Access levels
• 3 access levels– Read-only
• Make only a get method– Write-only
• Make only a set method– Quite rare
– Read / write• Make get and set methods• Most common
![Page 8: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/8.jpg)
Introduction to JavaBeans 8
Different kinds of properties
• Different kinds of properties– Simple or indexed (array) properties– Bound properties
• Event after a new value is set on the property• To update other components (e.g. visual
components)– Constrained properties
• Event before a new value is set on the property• To check if the new value is legal according to the
current business rules
![Page 9: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/9.jpg)
Introduction to JavaBeans 9
Simple properties• Represents a single value • A simple property has
– 1 name– 1 type
• Primitive data type or• Class based data type
• Example: size– Name: size Type: int– public void setSize(int size)– public int getSize()– AccountSimple.java
![Page 10: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/10.jpg)
Introduction to JavaBeans 10
Example bean: JButton
• Some properties of javax.swing.JButton– Text: setText(String), getText()– Font: setFont(Font), getFont()– Icon: setIcon(Icon), getIcon()– Enabled: setEnabled(boolean), isEnabled()
• Properties of type boolean has special naming conventions
– setXx, isXx
![Page 11: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/11.jpg)
Introduction to JavaBeans 11
Programming JavaBeans
• JavaBeans can be programmed using– Ordinary text editors
• TextPad, even Notepad– Builder tools
• NetBeans, Eclipse, JBuilder, etc.• Builder tools often have special features that
makes programming JavaBeans easier.
![Page 12: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/12.jpg)
Introduction to JavaBeans 12
Indexed properties
• Represents an array of values• Example: Property: address Type: Address
– Public void setAddress(int index, Address addr)– Public Address getAddress(int index)
– Bulk operations:• Public void setAddresses(Address[] addr)• Public Address[] getAddresses()
– AccountIndexedOwner.java
![Page 13: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/13.jpg)
Introduction to JavaBeans 13
Bound properties
• Sends an event to another object after the property is assigned a new value.– The other object is sometimes called:
Observer, listener, etc.• The other object may react on the event
– Update its own state• Content of an event
– propertyName, oldValue, newValue, source
![Page 14: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/14.jpg)
Introduction to JavaBeans 14
Examples
• AccountBound.java• ScreenLogListener.java• AccountBoundApp.java
![Page 15: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/15.jpg)
Introduction to JavaBeans 15
Observer pattern
Account
+addPropertyChangeListener(l : PropertyChangeListener)+removePropertyChangeListener(l : PropertyChangeListener)
#pcs : PropertyChangeSupport
«Interface»PropertyChangeListener
+propertyChange(evt : PropertyChangeEvent)0..n
0..n
MyPropertyChangeListener
+propertyChange(evt : PropertyChangeEvent)
c
![Page 16: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/16.jpg)
Introduction to JavaBeans 16
Sequence diagram: Firing a property change event
setBalance
firePropertyChange
:Account :PropertyChangeSupport :PropertyChangeListener
propertyChange
:PropertyChangeListener
propertyChange
![Page 17: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/17.jpg)
Introduction to JavaBeans 17
Sequence diagram: Adding a property change listener to a bean
:PropertyChangeListener :Account :PropertyChangeSupport
addPropertyChangeListener(this)
addPropertyChangeListener(...)
![Page 18: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/18.jpg)
Introduction to JavaBeans 18
Classes and interfaces
• Package java.beans– Class PropertyChangeEvent
• extends EventObject– Interface PropetyChangeListener
• Extends EventListener– Class PropertyChangeSupport
• Helps you to make bound properties
![Page 19: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/19.jpg)
Introduction to JavaBeans 19
PropertyChangeListener
• Interface PropertyChangeListener extends EventListener– EventListener is a marker interface
• No methods to implement
• PropertyChangeListener methods– void propertyChange(PropertyChangeEvent evt)
![Page 20: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/20.jpg)
Introduction to JavaBeans 20
PropertyChangeEvent• After changing the value of a property
– firePropertyChange(propertyChangeName, oldValue, newValue)
• Listener called– propertyChange(PropertyChangeEvent ev)
• PropertyChangeEvent methods:– String getPropertyName()– Object getOldValue()– Object getNewValue()– Object getSource()
![Page 21: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/21.jpg)
Introduction to JavaBeans 21
PropertyChangeSupport• Helps you to make bound properties• Constructor
– PropertyChangeSupport(Object sourceBean) • Some methods
– addPropertyChangeListener(PropertyChangeListener listener) – removePropertyChangeListener(PropertyChangeListener listener) – addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) – removePropertyChangeListener(String propertyName,
PropertyChangeListener listener) – firePropertyChange(String propertyName, Object oldValue,
Object newValue) – firePropertyChange(String propertyName, int oldValue,
int newValue)
![Page 22: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/22.jpg)
Introduction to JavaBeans 22
Event loops
• Problem– 2 beans send propertyChangeEvents to each other
• Example: User interface objects events a model object, and vice versa
– The 2 beans might event each other forever.• Solution
– Check if the new value is really a new value• i.e. newValue != oldValue• PropertyChangeSupport.firePropertyChange() does the
checking
![Page 23: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/23.jpg)
Introduction to JavaBeans 23
Example bean: JButton
• Javax.swing.JButton is a bean– Does not use ProperChangeEvent, etc.– addActionListener(ActionListener l)
• Listens for button presses– addChangeListener(ChangeListener l)
• Listens for changes in the button’s state– Size, font, text, etc.
![Page 24: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/24.jpg)
Introduction to JavaBeans 24
Constrained properties• A bean object asks one (or more) observers if
an update of a property is legal– Observer may throw PropertyVetoException– Example: account.setBalance(-100)– A property is often bound and constrained
• Examples– AccountConstrained.java– MinimumBalanceChecker.java– MaximumWithdrawChecker.java– AccountConstrainedTest.java– AccountFrame.java + AccountGuiApp.java
![Page 25: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/25.jpg)
Introduction to JavaBeans 25
Dynamic vs. static characteristics on properties
• Static characteristic– Follows the object for its life time.
• Dynamic characteristic– Changes over time during the objects life time– Characteristics may be (de)aggregated using
constrained properties.– Example: AccountAdminFrame.java
![Page 26: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/26.jpg)
Introduction to JavaBeans 26
Low coupling
• The event mechanism in JavaBeans provides low coupling– The bean object doesn’t know much about the
listeners• Except that listeners must implement
PropertyChangeListener or PropertyVetoListener– The listeners doesn’t know much about the
bean• It’s just an Object
– Which we sometimes have to typecast to the Bean class (which is very bad)
![Page 27: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/27.jpg)
Introduction to JavaBeans 27
Using JavaBeans in builder tools
• JavaBeans can be used in a builder tool like– NetBeans– Eclipse– Borland JBuilder– etc.
• The bean must be “installed” in the builder– The builder usually helps you to during
installation
![Page 28: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/28.jpg)
Introduction to JavaBeans 28
Introspection• The builder tool unzip’s the JAR file to get the
class files.• The builder tool “learns” about the beans’
properties, methods, and events using the introspection API– Package java.beans
• Class Introspector• Interface BeanInfo• Interface PropertyDescriptor• Interface MethodDescriptor• Interface EventSetDescriptor
– Example: beanbank/introspection/IntrospectionApp.java
![Page 29: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/29.jpg)
Introduction to JavaBeans 29
Using JavaBeans
• JavaBeans can be used as ordinary classes in– Java applications, applets, servlets– JSP (JavaServer Pages)
• special tags to manipulated JavaBeans– <jsp:useBean id="cart" class="shop.cart.ShoppingCart"
scope="session" />– <jsp:getProperty name="cart" property="numberOfItems" />– <jsp:setProperty name="cart" property="... " value="... " />
![Page 30: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/30.jpg)
Introduction to JavaBeans 30
JavaBeans vs. Enterprise JavaBeans
• JavaBeans – Are relatively simple– Part of Java Standard Edition (J2SE)– Can be used in an ordinary Java application / applet
• Enterprise JavaBeans– More advanced– Part of Java Enterprise Edition (J2EE)– Middle tier: Between (web)application and database.– Can only be used in an enterprise Java application
• Requires an J2EE server to run the application
![Page 31: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/31.jpg)
Introduction to JavaBeans 31
Job types using JavaBeansand other types of components
• Designing components• Programming components• Documenting components
– Technical writer• Design using components• Programming using components• Marketing / selling components• Testing components
– Correctness, performance, security [Trojan horses, etc.]
![Page 32: Introduction to Java Beans From Anders Børjesson](https://reader036.vdocuments.us/reader036/viewer/2022062302/5a4d1b177f8b9ab059991def/html5/thumbnails/32.jpg)
Introduction to JavaBeans 32
References
• Niemeyer & Knudsen Learning Java, 3rd edition, O’Reilly 2005 – 22. JavaBeans, page
751-786