Architecture & DesignJavaBeans Tutorial: What Are JavaBeans?

JavaBeans Tutorial: What Are JavaBeans?

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

JavaBeans are basically POJOs (Plain Old Java Object), defined according to the norms based on the software component model. Like any physical component of a system, a software component is also a partially independent and replaceable part of a system that fulfills a clear function in the context of a well-defined architecture. Bean component architecture follows a similar pattern. Beans are important because JavaBeans helps in building a complex system from the parts of software components. So, when we talk of JavaBeans, we are basically talking about the architecture that adheres to the software component model standard and how JavaBeans are integrated and incorporated to become a part of the whole subsystem.

Component Granularity

Analogically, think of a computer. It is a complex system composed of many individual components, such as memory devices, IO subsystem, processor, and so forth, or you may consider components as inductors, capacitors, resistors and so on. These components are partially independent because they are complete by their architectural design yet may not function in isolation. But, when integrated with compatible components, we get a clear cut functionality. Putting them together makes them whole. Characteristically, they can be reused without the slightest change. However, tangible components have compatibility issues with other components of the subsystem if one wants to replace/update it later. The same is true for software components, but they are inexpensive and less abrupt than their hardware counterpart. So, the primary motivation of building software components is twofold:

  • To achieve maximum re-usability of a component.
  • To be able to easily integrate or disintegrate as part of a larger system according to the requirement.

Component Granularity with JavaBeans

According to the JavaBeans Specification, “A Java Bean is a reusable software component that can be manipulated visually in a builder tool.” So, following the footprint of the component architecture model, JavaBeans are reusable. And, on the basis of their built structure and user requirement, JavaBeans can be built to function in two modes:

  • That which is built as a component to integrate with a larger application. That means this is a dependent bean that cannot work in isolation. The user may use some kind of a tool to integrate and customize to plug in as a part of an application. For example, a Swing or AWT button is a JavaBeans that follows this type of pattern.
  • The JavaBeans application. That means, these types of JavaBeans are built to function like an application itself, which may be integrated into a compound relationship with an user application. For example, a report viewer bean may be embedded in a Web page or to an PDF reader.

Introspection

Introspection is the core concept of JavaBean technology and the basic building block of the Java Bean API. It is a mechanism of analyzing a bean and providing the necessary information about what it actually does, its capabilities. Design tools obtain required information about the bean through introspection. The functionalities of a bean are reflected through its properties. There are two ways that a bean developer can inform its user about the properties, events, and methods it contains.

  • By a naming convention that enables the introspection process to infer information about the bean.
  • By extending the BeanInfo interface of the java.beans package that helps in providing the information.

Introspection may be implemented in the following manner.

Design Pattern

The values assigned to the property of a JavaBean class determine its functionality. A property is a publicly accessible attribute that helps in modifying the behavior of a bean class. Although a property is publicly accessible, its direct use (read/write) is actually restricted by actual implementation to access data with member functions. Properties are also observable; that means they can trigger notification to interested parties regarding value changes. The method setter or getter is used to set or get the value of the bean property respectively. There are generally two types of properties: simple and indexed.

Simple Properties

Simple properties are read/write with getter, setter methods. The naming convention is:

T getX();
void setX(T arg);

For a boolean property, a method of the form isPropertyName( ) is used to access by convention.

class MyDriver{
   private String name;
   // ...
   public String getName(){
      return name;
   }
   public void setName(String name){
      this.name=name;
   }
   // ...
}

Indexed Properties

If the property contains multiple values, the pattern followed is this:

   private String data[];
   // ...
   public String getData(int index){
      return data[index];
   }
   public void setData(int index, String str){
      data[index]=str;
   }
   public String[] getData(){
      return data;
   }
   public void setData(String []sa){
      data=new String[sa.length];
      System.arraycopy(sa,0,data,0,sa.length);
   }
   // ...
}

Event Handling with JavaBeans

A JavaBean can trigger and respond to events. The model it follows for event handling is called the delegation event model. The method signature that is used to add or remove a listener for a particular event is as follows:

public void addTListener(TListener eventListener)
public void addTListener(TListener eventListener)
   throws java.util.TooManyListenersException
public void removeTListener(TListener eventListener)


class MyDriver{

   public void addMyDriverListener(MyDriverListener eventListener) {
   // ...
   }
   public void addMyDriverListener(MyDriverListener eventListener)
      throws java.util.TooManyListenersException{
      // ...
   }
   public void removeMyDriverListener(MyDriverListener eventListener){
      // ...
   }
}

BeanInfo Interface

If we do not implement a BeanInfo interface, the design pattern implicitly determines the availability of information to the bean user. On implementing the BeanInfo interface the control to manipulate bean, information is explicitly provided by the pattern. This is achieved through various methods available such as follows (*):

  • BeanInfo[] getAdditionalBeanInfo(): This method enables the current BeanInfo object to return an arbitrary collection of other BeanInfo objects that provide additional information about the current bean.
  • BeanDescriptor getBeanDescriptor(): Returns the bean descriptor that provides overall information about the bean, such as its display name or its customizer.
  • int getDefaultEventIndex(): A bean may have a default event typically applied when this bean is used.
  • int getDefaultPropertyIndex() A bean may have a default property commonly updated when this bean is customized.
  • EventSetDescriptor[] getEventSetDescriptors(): Returns the event descriptors of the bean that define the types of events fired by this bean.
  • Image getIcon(int iconKind): Returns an image that can be used to represent the bean in toolboxes or toolbars.
  • MethodDescriptor[] getMethodDescriptors(): Returns the method descriptors of the bean that define the externally visible methods supported by this bean.
  • PropertyDescriptor[] getPropertyDescriptors(): Returns descriptors for all properties of the bean.

(*) excerpt from Java API Documentation

Conclusion

JavaBeans’ architecture provides a platform-neutral component architecture. According to the JavaBean specification, when the top-level bean is contained within a platform-specific container such as Microsoft Word, ActiveX, or any proprietary software, the JavaBeans API should be integrated into the platform’s local component architecture. On occasions JavaBean can bridge between architectural platforms, providing a seamless integration of components in a Java application.

References

  • H.M.Dietel, P.J.Dietel, Java, How to Program, Pearson, 6th Edition
  • Herbert Schildt, Java, The Complete Reference, Oracle, 9th Edition
  • Java API documentation
  • JavaBean Specification 1.01

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories