JavaBeans(TM) Specification Version 1.01, July 24, 1997, Graham Hamilton,
editor, Sun Microsystems
Using the Beans Development Kit 1.0: A Tutorial, Alden DeSoto, September 1997,
Sun Microsystems
Enterprise JavaBeans(TM) Version 1.0, Vlada Matena & Mark Harper, March 21,
1998 8:54 am, Sun Microsystems
http://java.sun.com/beans is a useful Web page for information about
JavaBeans(TM)
Texts
Developing Java Beans, Englander, O'Reilly, 1997
JavaBeans(TM) Developer's Reference, Dan Brookshier, New Rider, 1997
Creating JavaBeans(TM): Components for Distributed Applications, Mark
Watson, Morgan Kaufmann, 1998
"A JavaBean is a reusable software component that can be manipulated visually
in a builder tool."
Beans may or may not have a GUI component
A builder tool may:
- operate entirely visually
-
- enable users to conveniently write Java classes that interact with and
control a set of beans
-
- may provide a simple scripting language to allow easy high-level scripting
of a set of beans
The BeanBox, part of the Beans Development Kit (BDK), is a sample container for
testing and connecting beans
The BDK is on rohan (/opt/BDK) and available at http:java.sun.com.beans
What is a software Component?
"Components are self-contained elements of software that can be controlled
dynamically and assembled to form applications."
Features that distinguish a JavaBean
introspection
- Allows a builder tool to analyze how a bean works
customization
- The appearance and behavior of a bean can be customized
events
- Used to connect beans together
properties
- For customization and for programmatic use
persistence
- Allow the customized state to be saved and reloaded later
Beans v. Class Libraries
"Not all useful software modules should necessarily turn into beans"
"Beans are appropriate for software components that can be visually manipulated
and customized to achieve some effect."
"Class libraries are an appropriate way of providing functionality that is
useful to programmers, but which doesn't benefit from visual manipulation."
"The JDBC database access API is a class library rather
than as a bean"
"JDBC is essentially a programmatic API and not something that can be directly
presented for visual manipulation."
"You might write a "select" bean that at customization time helped a user to
compose a select statement, and then when the application is run uses JDBC to
run the select statement and display the results."
Security Issues
Java Beans use the standard Java security model
Java Beans & Distributed Computing
Java Bean components is that they run in the same virtual machine as the
container or application that contains it
Java Beans can access remote machines/object/beans via:
- Standard socket based client/server
-
- Java RMI
- "We expect that the use of Java RMI will be a very common way for writing
Java Beans within networked systems."
- Java IDL
- JDK 1.2 contains an implementation of Java IDL, which implements the CORBA
distributed object model
-
- JDBC
- Allows Java code, hence Java Beans, access to SQL
- databases
Multi-Threading
Java Beans should assume that they are being run in a multi-threaded
environment
Each bean must deal with threads properly
Internationalization
Visible beans should use the internationalization of strings feature of JDK 1.1
when they display text
Invisible beans may also display text in a builder tool, so should also
internationalize that text
Alternate type views of a bean
In the current Bean architecture a bean is a single object
In the future a bean can consist of a set of cooperating objects
Use static methods of java.beans.Beans to test beans for their class and to
cast beans to different types
java.beans.BeansMethods (All static)
getInstanceOf(Object, Class)
- From a given bean, obtain an object representing a specified type view of
that source object.
instantiate(ClassLoader, String)
- Instantiate a bean
isDesignTime()
- Test if we are in design-mode
isGuiAvailable()
isInstanceOf(Object, Class)
- Check if a bean can be viewed as a given target type
setDesignTime(boolean)
- Used to indicate whether of not we are running in an application builder
environment
setGuiAvailable(boolean)
- Used to indicate whether of not we are running in an environment where GUI
interaction is available
JavaBeans Interfaces, Classes & ExceptionsInterfaces
BeanInfo | PropertyEditor |
Customizer | VetoableChangeListener |
PropertyChangeListener | Visibility |
Classes
BeanDescriptor | PropertyChangeEvent |
Beans | PropertyChangeSupport |
EventSetDescriptor | PropertyDescriptor |
FeatureDescriptor | PropertyEditorManager |
IndexedPropertyDescriptor | PropertyEditorSupport |
Introspector | SimpleBeanInfo |
MethodDescriptor | VetoableChangeSupport |
ParameterDescriptor |
Exceptions
IntrospectionException
PropertyVetoException
While JavaBeans does have its own API much of JavaBean architecture consists of
standard Java API
Events provide a mechanism for plugging bean components together application
builder
Example - No Events
class Clock extends Thread
{
long durationBetweenTicks;
SimDay calender;
public Clock( long millisecondsPerTimeUnit, SimDay aDay )
{
calender = aDay;
durationBetweenTicks = millisecondsPerTimeUnit;
setPriority( Thread.MAX_PRIORITY );
}
public void run()
{
try
{
while (true)
{
calender.advanceHour();
sleep( durationBetweenTicks );
}
}
catch ( InterruptedException simulationOver )
{ return;}
}
}
class SimDay
{
int day = 0;
int hour = 0;
Clock myClock;
public SimDay()
{
myClock = new Clock( 20, this );
}
public void start()
{
myClock.start();
}
public void advanceHour()
{
if (hour == 23)
{
hour = 0;
day++;
}
else
hour++;
}
public String toString()
{
return "Day: " + day + " Hour: " + hour;
}
}
public class Test
{
public static void main( String args[] ) throws Exception
{
SimDay test = new SimDay();
System.out.println( test);
test.start();
for ( int k = 0; k <10; k++ )
{
System.out.println( test);
Thread.currentThread().sleep(100);
}
}
}
Using Events Notification
public class TimeAdvancedEvent extends java.util.EventObject
{
public TimeAdvancedEvent( Clock source)
{
super( source );
}
}
interface TimeAdvancedListener extends java.util.EventListener
{
void timeAdvanced( TimeAdvancedEvent timeEvent );
}
class Clock extends Thread
{
long durationBetweenTicks;
Vector listeners = new Vector();
public Clock( long millisecondsPerTimeUnit )
{
durationBetweenTicks = millisecondsPerTimeUnit;
setPriority( Thread.MAX_PRIORITY );
this.start();
}
public synchronized void addTimeAdvancedListener(
TimeAdvancedListener listener )
{
listeners.addElement( listener );
}
public synchronized void removeTimeAdvancedListener(
TimeAdvancedListener listener )
{
listeners.removeElement( listener );
}
protected void notifyListeners()
{
TimeAdvancedEvent time = new TimeAdvancedEvent(this);
for ( int k = 0; k < listeners.size(); k++)
{
TimeAdvancedListener aListener =
(TimeAdvancedListener) listeners.elementAt( k );
aListener.timeAdvanced( time );
}
}
public void run()
{
try
{
while (true)
{
notifyListeners();
sleep( durationBetweenTicks );
}
}
catch ( InterruptedException simulationOver )
{ return;}
}
}
class SimDay implements TimeAdvancedListener
{
int day = 0;
int hour = 0;
public void timeAdvanced( TimeAdvancedEvent timeEvent )
{
advanceHour();
}
public void start(Clock myClock)
{
myClock.addTimeAdvancedListener( this );
}
public void advanceHour()
{
if (hour == 23)
{
hour = 0;
day++;
}
else
hour++;
}
public String toString()
{
return "Day: " + day + " Hour: " + hour;
}
}
public class Test
{
public static void main( String args[] ) throws Exception
{
SimDay test = new SimDay();
Clock aClock = new Clock( 50 );
System.out.println( test);
test.start( aClock );
for ( int k = 0; k <10; k++ )
{
System.out.println( test);
Thread.currentThread().sleep(100);
}
}
}
visitors since 23-Apr-98