JavaBeans

Last updated

In computing based on the Java Platform, JavaBeans is a technology developed by Sun Microsystems and released in 1996, as part of JDK 1.1.

Contents

The 'beans' of JavaBeans are classes that encapsulate one or more objects into a single standardized object (the bean). This standardization allows the beans to be handled in a more generic fashion, allowing easier code reuse and introspection. This in turn allows the beans to be treated as software components, and to be manipulated visually by editors and IDEs without needing any initial configuration, or to know any internal implementation details.

As part of the standardization, all beans must be serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods.

Features

Introspection
Introspection is a process of analyzing a Bean to determine its capabilities. This is an essential feature of the Java Beans specification because it allows another application, such as a design tool, to obtain information about a component.
Properties
A property is a subset of a Bean's state. The values assigned to the properties determine the behaviour and appearance of that component. They are set through a setter method and can be obtained by a getter method.
Customization
A customizer can provide a step-by-step guide that the process must follow to use the component in a specific context.
Events
Beans may interact with the EventObject EventListener model.[ clarification needed ]
Persistence
Persistence is the ability to save the current state of a Bean, including the values of a Bean's properties and instance variables, to nonvolatile storage and to retrieve them at a later time.
Methods
A Bean should use accessor methods to encapsulate the properties. A Bean can provide other methods for business logic not related to the access to the properties.

Advantages

Disadvantages

JavaBeans API

The JavaBeans functionality is provided by a set of classes and interfaces in the java.beans package.

InterfaceDescription
AppletInitializerMethods in this interface are used to initialize Beans that are also applets.
BeanInfoThis interface allows the designer to specify information about the events, methods and properties of a Bean.
CustomizerThis interface allows the designer to provide a graphical user interface through which a bean may be configured.
DesignModeMethods in this interface determine if a bean is executing in design mode.
ExceptionListenerA method in this interface is invoked when an exception has occurred.
PropertyChangeListenerA method in this interface is invoked when a bound property is changed.
PropertyEditorObjects that implement this interface allow the designer to change and display property values.
VetoableChangeListenerA method in this interface is invoked when a Constrained property is changed.
VisibilityMethods in this interface allow a bean to execute in environments where the GUI is not available.

JavaBean conventions

In order to function as a JavaBean class, an object class must obey certain conventions about method naming, construction, and behaviour. These conventions make it possible to have tools that can use, reuse, replace, and connect Java Beans.

The required conventions are as follows:

Code example

packageplayer;publicclassPersonBeanimplementsjava.io.Serializable{/** Properties **/privatebooleandeceased=false;privateListlist;/** Property "name", readable/writable. */privateStringname=null;/** No-arg constructor (takes no arguments). */publicPersonBean(){}publicListgetList(){returnlist;}publicvoidsetList(finalListlist){this.list=list;}/**     * Getter for property "name".     */publicStringgetName(){returnname;}/**     * Setter for property "name".     *     * @param value     */publicvoidsetName(finalStringvalue){this.name=value;}/**     * Getter for property "deceased"     * Different syntax for a boolean field (is vs get)     */publicbooleanisDeceased(){returndeceased;}/**     * Setter for property "deceased".     * @param value     */publicvoidsetDeceased(booleanvalue){deceased=value;}}

TestPersonBean.java:

importplayer.PersonBean;/** * Class "TestPersonBean". */publicclassTestPersonBean{/**     * Tester method "main" for class "PersonBean".     *     * @param arguments     */publicstaticvoidmain(finalString[]arguments){finalPersonBeanperson=newPersonBean();person.setName("Bob");person.setDeceased(false);person.setList(newArrayList());// Output: "Bob [alive]"System.out.print(person.getName());System.out.println(person.isDeceased()?" [deceased]":" [alive]");}}
<jsp:useBeanid="person"class="player.PersonBean"scope="page"/><jsp:setPropertyname="person"property="*"/><html><body>Name:<jsp:getPropertyname="person"property="name"/><br/>Deceased?<jsp:getPropertyname="person"property="deceased"/><br/><br/><formname="beanTest"method="POST"action="testPersonBean.jsp">Enteraname:<inputtype="text"name="name"size="50"><br/>Chooseanoption: <selectname="deceased"><optionvalue="false">Alive</option><optionvalue="true">Dead</option></select><inputtype="submit"value="Test the Bean"></form></body></html>

See also

Related Research Articles

Eiffel is an object-oriented programming language designed by Bertrand Meyer and Eiffel Software. Meyer conceived the language in 1985 with the goal of increasing the reliability of commercial software development; the first version becoming available in 1986. In 2005, Eiffel became an ISO-standardized language.

In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. It is a kind of lazy evaluation that refers specifically to the instantiation of objects or other resources.

In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.

A method in object-oriented programming (OOP) is a procedure associated with an object, and generally also a message. An object consists of state data and behavior; these compose an interface, which specifies how the object may be utilized by any of its various consumers. A method is a behavior of an object parametrized by a consumer.

In computer science, reflective programming or reflection is the ability of a process to examine, introspect, and modify its own structure and behavior.

In class-based, object-oriented programming, a constructor is a special type of function called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

<span class="mw-page-title-main">Java syntax</span> Set of rules defining correctly structured program

The syntax of Java is the set of rules defining how a Java program is written and interpreted.

In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter, which returns the value of the private member variable.

In object-oriented programming, a member variable is a variable that is associated with a specific object, and accessible for all its methods.

<span class="mw-page-title-main">Dependency injection</span> Software programming technique

In software engineering, dependency injection is a programming technique in which an object or function receives other objects or functions that it depends on. Dependency injection aims to separate the concerns of constructing objects and using them, leading to loosely coupled programs. The pattern ensures that an object or function which wants to use a given service should not have to know how to construct those services. Instead, the receiving 'client' is provided with its dependencies by external code, which it is not aware of. Dependency injection makes implicit dependencies explicit and helps solve the following problems:

In software engineering, a plain old Java object (POJO) is an ordinary Java object, not bound by any special restriction. The term was coined by Martin Fowler, Rebecca Parsons and Josh MacKenzie in September 2000:

"We wondered why people were so against using regular objects in their systems and concluded that it was because simple objects lacked a fancy name. So we gave them one, and it's caught on very nicely."

In the Java programming language, the final keyword is used in several contexts to define an entity that can only be assigned once.

The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program.

An interface in the Java programming language is an abstract type that is used to declare a behavior that classes must implement. They are similar to protocols. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations. All methods of an Interface do not contain implementation as of all versions below Java 8. Starting with Java 8, default and static methods may have implementation in the interface definition. Then, in Java 9, private and private static methods were added. At present, a Java interface can have up to six different types.

In the Java computer programming language, an annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and Java packages may be annotated. Like Javadoc tags, Java annotations can be read from source files. Unlike Javadoc tags, Java annotations can also be embedded in and read from Java class files generated by the Java compiler. This allows annotations to be retained by the Java virtual machine at run-time and read via reflection. It is possible to create meta-annotations out of the existing ones in Java.

Generics are a facility of generic programming that were added to the Java programming language in 2004 within version J2SE 5.0. They were designed to extend Java's type system to allow "a type or method to operate on objects of various types while providing compile-time type safety". The aspect compile-time type safety was not fully achieved, since it was shown in 2016 that it is not guaranteed in all cases.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

In software engineering, the module pattern is a design pattern used to implement the concept of software modules, defined by modular programming, in a programming language with incomplete direct support for the concept.

Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTSTEP operating system. Due to Apple macOS’s direct lineage from NeXTSTEP, Objective-C was the standard programming language used, supported, and promoted by Apple for developing macOS and iOS applications until the introduction of the Swift programming language in 2014.

References

  1. 1 2 Bloch, Joshua (2008). Effective Java (Second ed.). Addison-Wesley. p.  13. ISBN   978-0-321-35668-0.