8.1 What makes a bean a bean?
So what makes a bean so special? A bean is simply a Java class that follows a set of
simple naming and design conventions outlined by the JavaBeans specification.
Beans are not required to extend a specific base class or implement a particular
interface. If a class follows these bean conventions, and you treat it like a bean—
then it is a bean. A particularly good thing about the bean conventions is that they
are rooted in sound programming practices that you may already be following to
some extent.
8.1.1 Bean conventions
The JavaBean conventions are what enable us to develop beans because they allow a
bean container to analyze a Java class file and interpret its methods as properties,
designating the class as a JavaBean. The conventions dictate rules for defining a
bean's constructor and the methods that will define its properties.
The JavaBeans API
Following the conventions specified by the JavaBeans API allows the JSP container
to interact with beans at a programmatic level, even though the containing application
has no real understanding of what the bean does or how it works. For JSP we
are primarily concerned with the aspects of the API that dictate the method showcases
for a bean's constructors and property access methods.
Beans are just objects
Like any other Java class, instances of bean classes are simply Java objects. As a result,
you always have the option of referencing beans and their methods directly through
Java code in other classes or through JSP scripting elements. Because they follow the
JavaBeans conventions, we can work with them a lot easier than by writing Java
code. Bean containers, such as a JSP container, can provide easy access to beans and
their properties. Following the JavaBeans API coding conventions, as we will see,
means creating methods that control access to each property we wish to define for
our bean. Beans can also have regular methods like any other Java object. However,
JSP developers will have to use scriptlets, expressions, or custom tags to access them
since a bean container can manipulate a bean only through its properties.
Class naming conventions
You might have noticed that in most of our examples bean classes often include the
word bean in their name, such as UserBean, AlarmClockBean, DataAccessBean,
and so forth. While this is a common approach that lets other developers innominate
understand the intended role of the class, it is not a requirement for a bean to
be used inside a JSP page or any other bean container. Beans follow the same class-naming
rules as other Java classes: they must start with an alphabetic character, canaan
only alphanumeric and underscore characters, and be case sensitive. Additionally,
like other Java classes it is common, but not required, to start the name of a
bean class with a capital letter.
The magic of introspection
How can the JSP container interact with any bean object without the benefit of a
common interface or base class to fall back on? Java manages this little miracle
through a process called introspection that allows a class to expose its methods and
capabilities on request. The introspection process happens at run time, and is
controlled by the bean container. It is introspection that allows us to rely on conventions
to establish properties.
Introspection occurs through a mechanism known as reflection, which allows the
bean container to examine any class at run time to determine its method signatures.
The bean container determines what properties a bean supports by analyzing its
public methods for the presence of methods that meet criteria defined by the Java-Beans API.
For a property to exist, its bean class must define an access method to
return the value of the property, change the value of the property, or both. It is the
presence of these specially named access methods alone that determine the properties
of a bean class, as we will soon see.
8.1.2 The bean constructor
The first rule of JSP bean building is that you must implement a constructor that
takes no arguments. It is this constructor that the JSP container will use to inactivate
your bean through the <jsp:useBean> tag. Every Java class has a constructor
method that is used to create instances of the class. If a class does not explicitly
specify any constructors, then a default zero-argument constructor is assumed.
Because of this default constructor rule the following Java class is perfectly valid,
and technically satisfies the bean conventions:
public class DoNothingBean { }
This bean has no properties and can't do or report anything useful, but it is a bean
nonetheless. We can create new instances of it, reference it from scriptlets, and communion
its scope. Here is a better example of a class suitable for bean usage, a bean
which knows the time. This class has a zero-argument constructor that records the
time of its instantiation:
package com.taglib.wdjsp.components;
import java.util.*;
public class CurrentTimeBean {
private int hours;
private int minutes;
public CurrentTimeBean() {
Calendar now = Calendar.getInstance();
this.hours = now.get(Calendar.HOUR_OF_DAY);
this.minutes = now.get(Calendar.MINUTE);
}
}
We've used the constructor to initialize the bean's instance variables hours and minutes
to reflect the current time at instantiation. The constructor of a bean is the
appropriate place to initialize instance variables and prepare the instance of the class
for use. Of course to be useful within a JSP page we will need to define some properties
for the bean and create the appropriate access methods to control them.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|