What is java bean component

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.
Читайте также:  Изменить шрифт цвет html

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.
Читайте также:  This page requires java

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:

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

Источник

The Java EE 5 Tutorial

JavaBeans components are Java classes that can be easily reused and composed together into applications. Any Java class that follows certain design conventions is a JavaBeans component.

JavaServer Pages technology directly supports using JavaBeans components with standard JSP language elements. You can easily create and initialize beans and get and set the values of their properties.

JavaBeans Component Design Conventions

JavaBeans component design conventions govern the properties of the class and govern the public methods that give access to the properties.

A JavaBeans component property can be:

  • Read/write, read-only, or write-only
  • Simple, which means it contains a single value, or indexed, which means it represents an array of values

A property does not have to be implemented by an instance variable. It must simply be accessible using public methods that conform to the following conventions:

    For each readable property, the bean must have a method of the form:

PropertyClass getProperty()
setProperty(PropertyClass pc)

In addition to the property methods, a JavaBeans component must define a constructor that takes no parameters.

The Duke’s Bookstore application JSP pages bookstore.jsp, bookdetails.jsp, catalog.jsp, and showcart.jsp, all located at tut-install/javaeetutorial5/examples/web/bookstore2/web, use the tut-install/javaeetutorial5/examples/web/bookstore2/src/java/com/sun/bookstore2/database/BookDB.java JavaBeans component.

BookDB provides a JavaBeans component front end to the access object BookDBAO. The JSP pages showcart.jsp and cashier.jsp access the bean tut-install/javaeetutorial5/examples/web/bookstore/src/com/sun/bookstore/cart/ShoppingCart.java, which represents a user’s shopping cart.

The BookDB bean has two writable properties, bookId and database, and three readable properties: bookDetails, numberOfBooks, and books. These latter properties do not correspond to any instance variables but rather are a function of the bookId and database properties.

package database; public class BookDB < private String bookId = "0"; private BookDBAO database = null; public BookDB () < >public void setBookId(String bookId) < this.bookId = bookId; >public void setDatabase(BookDBAO database) < this.database = database; >public Book getBook() throws BookNotFoundException < return (Book)database.getBook(bookId); >public List getBooks() throws BooksNotFoundException < return database.getBooks(); >public void buyBooks(ShoppingCart cart) throws OrderException < database.buyBooks(cart); >public int getNumberOfBooks() throws BooksNotFoundException < return database.getNumberOfBooks(); >>

Creating and Using a JavaBeans Component

To declare that your JSP page will use a JavaBeans component, you use a jsp:useBean element. There are two forms:

The second form is used when you want to include jsp:setProperty statements, described in the next section, for initializing bean properties.

The jsp:useBean element declares that the page will use a bean that is stored within and is accessible from the specified scope, which can be application, session, request, or page. If no such bean exists, the statement creates the bean and stores it as an attribute of the scope object (see Using Scope Objects). The value of the id attribute determines the name of the bean in the scope and the identifier used to reference the bean in EL expressions, other JSP elements, and scripting expressions (see Chapter 9, Scripting in JSP Pages). The value supplied for the class attribute must be a fully qualified class name. Note that beans cannot be in the unnamed package. Thus the format of the value must be package-name.class-name.

The following element creates an instance of mypkg.myLocales if none exists, stores it as an attribute of the application scope, and makes the bean available throughout the application by the identifier locales:

Setting JavaBeans Component Properties

The standard way to set JavaBeans component properties in a JSP page is by using the jsp:setProperty element. The syntax of the jsp:setProperty element depends on the source of the property value. Table 5-6 summarizes the various ways to set a property of a JavaBeans component using the jsp:setProperty element.

Syntax rules of attribute values used in this table:

  1. beanName must be the same as that specified for the id attribute in a useBean element.
  2. There must be a setPropName method in the JavaBeans component.
  3. paramName must be a request parameter name.

Table 5-6 Valid Bean Property Assignments from String Values

Источник

Оцените статью