Java Programming/JavaBeans/Introduction to JavaBeans
[edit] Definition
Reusability comes at the core of any modern computer language's framework. It is often desirable to use components you previously built in recurring environments. In Rapid Application Development, these prove more helpful as you can drag them off a list of components and use it elsewhere in your project. Such level of reusability is added into the Java Programming language with the help of the JavaBeans architecture.
JavaBeans are the mainstream Java component model, introduced in 1996 by Sun Microsystems. JavaBeans are defined as follows:
"A JavaBean is a reusable software component that can be manipulated visually in a builder tool."
Together with the component model, Sun released a simple visual composition tool, the BeanBox. It is mostly intended for experimenting with Beans rather than than offering a professional IDE. For real world applications, one should better deploy on one of the Java IDEs like Visual Age or JBuilder that support the visual composition of JavaBeans.
As we will see, JavaBeans do not essentially differ from standard Java classes, which makes the component model quite easy to use. What sets a JavaBean apart from normal Java classes are a set of features and conventions adopted for facilitating reuse:
- Presence of a no argument constructor;
- Support for persistence;
- Properties manipulated by getter and setter methods;
- Support for introspection;
- Events as the mechanism of communication between beans;
- Support for customization via property editors.
[edit] Persistence
The requirements for an object to be a bean is to define a public parameterless constructor, so that beans can be instantiated by builder tools in an uncomplicated way (In the Point bean, the parameterless constructor is given implicitly). Secondly, one of the interfaces java.io.Serializable
or code java.io.Externalizable
need to be implemented. The interfaces do not prescribe the implementation of any methods, but are an approval, that the bean may be saved in persistent storage as in a file or database. In doing so, the bean can be restored after an application was shut down or transferred across networks. The ability to store state of a component in persistent storage is called persistence. Java offers a standard serialization mechanism, which makes it very easy to serialize objects. Alternatively, a component can be stored in a customized manner (e.g. in xml format) by implementing the code Externalizable
interface.
[edit] Properties
The properties of a bean are all private fields that are accessible and modifiable by public methods. These getter and setter methods should be marked as such by following a simple naming convention: for some property named, say, xxxx
there should be a getXxx
which returns the property value and a setXxx
which sets the property.
[edit] Introspection
The intention behind this naming convention is that it helps builder tools to find out how a bean works and what capabilities it has. As we have seen, a component has to provide information about its services, which is called introspection. Java Beans provides two mechanisms for introspection: reflection and the BeanInfo
class. BeanInfo
gives complex information about a Bean component, but has to be implemented explicitly.
[edit] Events
JavaBeans interact with each other by means of events. Events are notifications, a component can give to other components, that something interesting has happened. An example for an event might be a mouse click on a button or the closing of a window. Beans can be source and target of events. To be informed about an event, a bean has to register at another Bean as a listener.
The Java event model realizes the observer design pattern with the effect that the inter-component coupling is reduced. Method calls require tight coupling, as caller and receiver need to know each other at compile time, while with events all communication happens solely via interfaces.
A special kind of event are PropertyChangeEvents
. They are used to restrict some properties to take only specific values, for example for a month integer values between 1 and 12. Every time, such a bound property is modified, notifications to all registered PropertyChangeListeners will be send.
[edit] Customization
Customization is done via Property Editors. A property editor is a tool for customizing at design time a particular property type. Property editors are activated from so-called property sheets, which display all properties of a bean. If a property is selected for customization, the property sheet finds out the type of the property, displays the appropriate property editor with the property's current value.
[edit] Additional Remarks
A big strength of the JavaBean component model is that it is designed for simplicity. Developing JavaBeans is very simple, because a lot of behaviour (like the platform independence or packaging mechanism) is supported in the Java Programming Language by default. However, one can optionally equip beans with additional objects like BeanInfos or custom PropertyEditors to use the component model in a more flexible way. A second facility is that Sun designed the whole swing GUI library according to the JavaBeans component model. Thereby Swing components can easily be composed in visual builder tools.
However, JavaBeans do not realize all features of a component model. A drawback is that JavaBeans are restricted to the Java programming language, while an important goal of components is the independence of an implementation language.
[edit] Recommended readings
- Learning JavaTM, Niemeyer, P. and Knudsen, J., 3rd Edition, 2005, O'Reilly: Sebastopol, CA. pp.751-786
[edit] Preface
[edit] Getting started
- Installing Java on Your Computer
- Compiling programs
- Running Java programs
- Developing the first Java program
- Understanding a Java program
[edit] Language fundamentals
- Variables
- Arrays
- Basic arithmetic
- Mathematical functions
- Large numbers
- Random numbers
- Flow control
- Methods
- Statements
- Types
- String
- Classes, Objects and Types
- Syntax
- Keywords
- Packages
- Nested classes
- Access modifiers
- Data and variables
- Generics
- Java Security
[edit] Classes and objects
- Defining classes
- Creating objects
- Interfaces
- Using static members
- Destroying objects
- Overloading methods and constructors
- Class loading
[edit] Collections
[edit] Exceptions
- Throwing and catching exceptions
- Stack trace
- Checked exceptions
- Unchecked exceptions
- Preventing NullPointerException
- Chained exceptions(Nesting Exceptions)
[edit] Annotations
[edit] Designing user interfaces
[edit] Swing programming
- First Example: Frames and Labels
- Events and Buttons
- A Responsive Application
- Layouts
- Building a calculator
[edit] Advanced topics
- Byte code
- Concurrent programming
- Networking
- Database programming
- Reflection
- JavaBeans
- Design patterns
- Libraries, extensions and frameworks
- 3D programming
- Threads
- Remote Method Invocation (RMI) and Internet Inter-Orb Protocol (IIOP)
- Profiling
- Monitoring