The mission of a JavaBean is to Write once, run anywhere, reuse everywhere. This is really all about providing a simple, compact and excellent solution to the increasing complexity plaguing the software industry. Javabeans are designed to reduce duplicate code.

 

JavaBean can be divided into two types: JavaBean with User Interface (UI); There are also Javabeans that have no user interface and are primarily responsible for handling transactions, such as data operations and manipulating databases. JSPS typically access the latter javabeans.

 

JavaBean main function: encapsulate business logic (function implementation), database operations (database processing, database connection) and so on.

 

Basically, A JavaBean is a black box, a software device that only needs to know its functionality, not its internals. The black box describes and defines only its external features and interfaces with other parts, such as buttons, Windows, colors, shapes, handles, and so on.

 

A JavaBean consists of three parts: properties, methods, and events.

(1) getxxx: to get the property of XXX;

Setxxx: to set the properties of XXX;

(2) Allow the use of “is “for Boolean member variables, that is, Boolean logical attributes.

Instead of “get” and “set” above.

(3) All access attributes of a method in a class must be public.

(4) If a class has a constructor, the constructor is public and takes no arguments.

Examples of JavaBean:

</pre><pre name="code" class="html"><span style="font-size:18px;">import java.io.Serializable; Public class JavaBeanDemo implements Serializable{// Implements Serializable interface JavaBeanDemo(){} private int ID; // Implements Serializable interface JavaBeanDemo(){} // Private attribute Id private String name; // Private attribute name public int getId(){return Id; } public void setId(int id) {//set() this.id = id; } public String getName(){//get() return name; } public void setName(String name) { this.name = name; }}</span>
Copy the code

In the design of Javabeans, the Bean can be divided into 4 categories according to the different functions of its attributes, which are Simple, Indexed, Bound and Constrained. \

Simple properties: Variables represented as generic data types and the getXXX() and setXXX() methods are named after properties.

     

Public class Hello {Hello(){} private String name; // Define a simple String attribute name private Boolean info; Public String getName() {// Simple property getXxx() method return name; } public void setName(String name) {this.name = name; } public Boolean isInfo() {return info; } public void setInfo(Boolean info) {// setXxx method this.info = info; }Copy the code

Index property: Represents an array value or a collection. Like the Simple property, the value can be obtained using the getXXX() and setXXX() methods, for example:

      

public int[] array=new int[8]; Public int[] getArray() {return array; } public void setArray(int[] array) {this.array = array; } public void setArray(int index,int value) {this.array[index]=value; } public int getArray(int index){return array[index]; }Copy the code

Bound attributes:

A Bound property, also known as an association property, notifies other objects when the value of that property changes. Each time the property value changes, the property fires a PropertyChange event (in Java programs, an event is also an object). The event encapsulates the property name, the property’s original value, and the property’s new value. Such events are passed to other beans, and it is up to the receiving Bean to define what it should do. Beans containing associated properties must have the following functionality: \

(1) Allow event listeners to register and unregister attribute modification events related to them; (2) When modifying an associated attribute, the attribute modification event can be triggered on the related listener.

Using Java. Beans. PropertyChangeSupport class creates a PropertyChangeSupport class object, which can be used to manage registration list of listeners and attribute modify event notification sent. JavaBean also need to implement addPropertyChangeLinster () method and removePropertyChangeLinster () method, in order to add and cancel the property change listener.

Limiting attributes:

The constrained property means that when the value of the property changes, other external Java objects that have already made some kind of connection to the property can veto the change. (The listener of the constrained property can prevent the property from changing by throwing a PropertyVetoException.) The Bean itself can also reject changes to the Bean property value.

There are two types of listeners for a limiting property: listeners for property changes and listeners for unchanging property changes. The listener that cancels attribute change has corresponding control statement in its own object code. When it detects that a restricted attribute is about to change, it judges whether the change of the attribute value should be rejected in the control statement.

Using Java. Beans. VetoableChangeSupport class fireVetoableChange () method is passed the attribute name, the value of change before and after changing the value of the information. JavaBean also need to implement addVetoableChangeLinster () method and removeVetoableChangeLinster () method, in order to add and cancel the property change listener.

Note: Because the constraint attributes use error handling, pay special attention to how exceptions are handled when programming.

      

\

\

\