JavaBeans

from Wikipedia, the free encyclopedia

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

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

overview

A JavaBean is a Java class that corresponds to a component model in order to allow automated access to its properties ( member variables ) and operations ( methods ). The Java SDK makes the Bean API (the package java.beans) available to application developers , for example to display a brief description of a bean. The JavaBean component model was primarily developed to provide a unified API for easier development of GUI builders. Beans realize improved serialization and thus network capability, reusability , portability and interoperability .

A component is only a JavaBean if it complies with the JavaBeans API Specification , which is specified by the JavaBeans Component Architecture .

Instantiation

A JavaBean always has a constructor without parameters (standard constructor). This enables standardized instantiation, for example a button :

JButton button = (JButton) Class.forName("javax.swing.JButton").newInstance();

An alternative to this instantiation is:

JButton button = new JButton();

Access operations

All properties of a bean preserve the secrecy principle (information hiding) . Access is possible via special operations that must comply with certain conventions.

For a property foo, the read operation is called (getter) getFoo . With Boolean variables, it is also isFoopossible as an alternative , which is generally preferred. The writing operation (setter) is called setFoo. Indexed properties each have two getters and setters: one for the entirety, one for a specific index. A read-only property has no (public) setter.

Introspection

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

It is also possible to write a BeanInfo class for each bean that contains information about the bean, its properties and operations. The class first java.beans.Introspectorlooks for explicit information and completes it through reflection.

additional

When changing a bound property , objects must be informed of the change. At Constrained Properties , they can also veto . The Bean API provides an event delegation model for this.

Graphic editors and customizers can be created for certain data types and beans, and properties can be easily adjusted using them.

There is a persistence mechanism for beans. For example, beans can be saved as an XML document.

literature

Web links