JavaBeans

JavaBeans are software components for the Java programming language. JavaBeans developed out of the need for GUI classes (AWT, Swing ) simply instantiate and to transfer via RMI. JavaBeans also be used as a container for data transmission. Therefore, all Java Beans are characterized by the following properties:

  • Public default constructor (default constructor )
  • Serializability ( Serializable )
  • Public access methods (public getter / setter )

Due to these properties, JavaBeans are also suitable as data objects for persistence frameworks (see Enterprise Java Beans).

Overview

A JavaBean is a Java class that corresponds to a component model to allow automated access to their properties ( member variables ) and operations. The Java SDK provides application developers with the Bean API ( the java.beans package ) are available, for instance to display a brief description of a bean. The JavaBean component model was developed primarily to provide a unified API for easy development of GUI builders. Beans realize an improved serialization and so networking capability, reusability, portability and interoperability.

A component is a JavaBean only if it corresponds to the JavaBeans API Specification, which specifies the JavaBeans component architecture.

Instantiation

A JavaBean always has a constructor with no arguments ( default ). This allows for a standardized instantiation, such as a button:

JButton button = ( JButton ) Class.forName ( " javax.swing.JButton " ) newInstance ().; An alternative of this instantiation is as follows:

JButton button = new JButton (); access operations

Preserve all properties of a bean, the secret principle ( information hiding ). Access is possible via special operations, must meet certain conventions.

For a foo property is called the read operation ( "getter " ) getFoo. For Boolean variables is alternatively isFoo possible, which is generally preferred. The writing ( " setter " ) operation is called setFoo. Indexed properties each have two getters and setters: one for the whole, for a given index. A read-only property does not have a ( public ) setter.

Introspection

Introspection is the mechanism that a bean on their properties, events (events) and operations to analyze. The API provides this options that make an additional support for introspection on the part of the bean developer unnecessary. Beans can be studied by reflection, if they adhere to the conventions defined in the specification.

There is also the possibility for each bean to write a BeanInfo class that contains information about the bean, their properties and operations. The class introspector first looks for explicit information and completes this by reflection.

More

When you change a Bound Property ( bound property ) must be informed of the change objects. In Constrained Properties this may also veto. The Bean API provides ready for this one event delegation model. It can be graphical editors and customizers for certain data types and Beans created and changed easily through them properties.

There is a persistence for beans. Thus beans can for example be stored as an XML document.

To make a Java Bean operational for a visual editor, the class file must be packaged in a jar file, which must also contain a manifest file (. Mf ).

432274
de