JavaBeans Components
To understand JavaBeans… Proficient experience with the Java language required Knowledge of classes and interfaces Object-Oriented development and design Simple Java GUI application programming using AWT Exposure to Java’s Delegation Event-Handling Model
JavaBeans Overview Software Components JavaBeans Definition The JavaBeans API Applying JavaBeans The Basic Structure of a Bean
Software Components Discrete, Reusable Software standard Microsoft’s ActiveX Distributed Network Environment Cross-platform Must be controlled dynamically Assembled to form applications Interoperablility standards
The Component Model Introspection - Discovery and Registration Event Handling Persistence Visual Presentation - Layout Application Builder Support
Introspection Discovery of Component at Run Time Late Binding Expose Component Functionality Component is Isolated, yet Usable
Event Handling Alert to Internal Change Component Interactivity Listeners
Persistence Saving State Storage and Retrieval Uniform Persistence Mechanism needed
Visual Presentation - Layout Dynamic Property Control Physical Layout Component Interaction and Spatial Requirement Container/Component Behavior
Application Builder Support Graphical application builders Toolboxes and Palettes Editing Properties Components must Expose properties and behaviors
JavaBean Definition What is a Bean? The JavaBeans specification states: A JavaBean is a reusable software component that can be manipulated visually in a builder tool.
JavaBeans Benefits and Features Simple and Compact Portable Leverage Strengths of Java Platform Application Builder Support Flexible Build-Time Component Editors Distributed Computing Support
JavaBeans and Java Java: “Write once, Run Anywhere” JavaBeans mission statement: “….Reuse Everywhere” Java provides no component model JavaBeans specifies framework for reusability and interoperability for components Java integration requires code-level knowledge JavaBeans integration requires visual editor
JavaBeans API Property Management Event Handling Introspection Persistence Application Builder Support Customization Multithreading
Property Management Accessor Methods Indexed Properties Bound Properties Constrained Properties
Event Handling Event Sources Unicast Multicast Event Listeners Event Objects Event Adapters
Introspection Reflection Design Patterns Explicit Bean Information Introspector
Persistence Storage and Retrieval Java Object Serialization Bean controlled persistence
Application Builder Support Edit and Manipulate Beans Bundled with Bean Separately Property Sheets Property Editors Customizers
Multithreading Relies on conventional Java Programming techniques synchronized keyword Make sure Beans are thread-safe
Where and How Beans are Used Web Pages Liven up static HTML Interactivity Applications Application Builder Tool Handwritten Code Bridge Technology
Structure of a JavaBean Data Methods Events DataMethods JavaBean Events
JavaBeans and the Interface Data Public Methods JavaBean Interface A Interface B
What Constitutes a Bean? Class must be instantiable Class must have a default constructor (not officially required by spec, but by most builder tools) Class must be serializable Implement Serializable, Externalizable Class must follow JavaBeans naming conventions (sometimes called design patterns) Class must use delegation event model
Constructing a Bean “Must be instantiable” requirement Not abstract Not an interface “Default constructor” requirement Application builder tools Persistence
Serialization Representation as a series of bytes Storage to non-volatile location Implement Serializable Interface Implement Externalizable Interface
JavaBeans Design Patterns Naming conventions for automatic Introspection Example: Property named Height public int getHeight(); public void setHeight(int h); Patterns for Event Registration
Delegation Event Model Introduced in Java 1.1 AWT Most Java AWT Components are Beans Simple Graphical Reusable JFC Swing Components are JavaBean compliant
A Simple Bean public class FirstBean implements java.io.Serializable { protected int theValue; public FirstBean() { } public void setMyValue( int n ) { theValue = n; } public int getMyValue() { return theValue; }
Visual Development Environments Developer can focus on Business Application Connect Components Visually Code-Generation Engine Stability Ease of Generation Knowledge of Underlying Language Helpful
Visual Programming NetBeans JBuilder VisualAge for Java Eclipse JDeveloper
Security and JavaBeans Same security model as Java Beans are treated like applets Untrusted applet -> Untrusted Bean Trusted applet -> Trusted Bean Program for Untrusted Environments
Summary JavaBeans satisfies the requirements of a discrete, reusable component model Visual and Non-Visual JavaBeans can be developed and used in diverse development environments Java Classes can become JavaBeans by adhering to some minimum standards Java’s Security Model transfers directly to JavaBeans