Good programmers learning Java Java bean line is what the concept

  Good programmers learning Java Java bean line is what the concept, Bean in Chinese means "beans", as the name suggests JavaBean is a piece of Java applets. JavaBean actually refers to a special class of Java, it is usually used to implement some of the more common simple function, and can easily be reused or to insert another application. All follow certain principles of programming Java class can be called a JavaBean.
A. Technical Overview Java Bean
        Java Bean is a Java-based component model, consists of three parts properties, methods and events make up. In this model, JavaBean or may be modified to generate a new program component or in combination with other components intact. It is a Java class, by encapsulating the object of treatment with a certain function or a service. Therefore, you can also access the property by Bean and Java code embedded in the JSP page.

        Bean is the meaning of reusable Java components. The so-called component is one of a class or several internal management may consist themselves, do not understand the outside world community its internal information and operation mode. Using its object can only be operated via the interface.
Two. Java Bean specification written in
        Java Bean is actually the norm written in the Java Bean class name specified in terms of design and technical standard JavaBean. These classes follow an interface format, in order to make the function name, as well as the underlying behavior implemented or inherited behavior, its biggest advantage is that the code is implemented may reusability. Bean does not need to inherit a particular base class (BaseClass) or implement particular interface (Interface). Bean written specification makes Bean container (Container) a Java class file can be analyzed, and a method (Methods) translated properties (the Properties), i.e., the Java class as a class Bean used. Bean written specification includes constructor Bean class defines the properties and methods to access write rules.
2.1 working mechanism Bean component
defines five important mechanism of the assembly in JavaBeansVersion1.01 A specification:

(1) introspection (Introspection): formation can express its support operations and properties, but also found support reusable object library, such as user access control and e-mail auto-reply in other components and so on.

(2) Communication (Communication): Event message generation and collection assembly.

(3) Length (Persistence): storage state of the component.

(4) Properties (Properties): layout control support assembly, including the space and the relative positions of components as occupied.

(5) custom (Customization): Developers can change the control mechanisms required components.

2.2 Java Bean written request
  to write JavaBean must meet the following requirements:

(1) All JavaBean must be placed a package (Package) In.

(2) JavaBean must generate public class type, file name should be consistent with the class name.

(3) all properties must be packaged, there should be a common JavaBean class instance and class variables are private.

(4) The attribute value shall be accessed through a set of access methods (getXxx and setXxx): For each property, there should be a dedicated public instance variable matching with getter and setter methods.

(5) Java Bean class must have a null constructor: public class must have a constructor with no arguments, this constructor should be to set the default value of the property by calling the method of setting each attribute.
. 2.3 Java Bean naming conventions
   Java Bean naming conventions are as follows:

(1) Package name: all lowercase.

(2) Class name: the first letter of each word capitalized.

(3) Property name: The first word in all lowercase, after each word capitalized.

(4) Method name: the same name attribute method.

(5) constant names: all capital letters.
2.4. Java Bean bag
        package, ie package, JavaBean package and described in the previous section contains the meaning is basically the same, but there are differences, described earlier packages are defined in Java itself, but JavaBean package is user-defined of.

        After each JavaBean source files are compiled into .class files must be stored in a corresponding folder, store the .class file folder is a package. JavaBean package must be stored in a particular directory, in each JSP engine are specified storage location JavaBean package, different JSP engines have different requirements for the position JavaBean stored, such as in Tomcat, all packages are JavaBean stored in the WEB-INF / classes folder. All parent directory if there is multi-level directory, you will need to .class file directory is included in the package name, the directory between each level of English punctuation. "" Separated. For example the following codes: packagejsp.example.mybean;
2.5 Java Bean structure.
(1) Properties: i.e. Java Bean class member variables used to describe the state of the JavaBean object, object attribute value change trigger event, the event source attribute itself is .

(2) Method: In the Java Bean, the process is referred to as the function and method, and to change the value of the attribute acquired by the method. The method can be divided into construction methods, access methods and general methods.

(3) Event: The event is actually a special kind of Java Bean, change the property value of trigger events, event fires related objects react to receive by registering Java Bean object event listener mechanism to deal with the event, which implements the Java Bean communication between.
Three. Java Bean property
        in the previous section we briefly mentioned JavaBean properties, the properties and the general properties of Java Bean Java programs referred to, or that all object-oriented programming language, object properties is a concept, in the program is a concrete manifestation of the class variables. Attributes are divided into four categories, i.e., a single value (the Simple), index (Index), association (Bound) and constraints (Constrained) properties. This section provides a detailed description of these properties.
3.1. Single-valued (Simple) property
        single value (the Simple) attribute is the most common type of attribute, property class only a single data value, the data type of the data value can be any data type in Java, including classes and interfaces and other types.

        Defines the properties, we need to define corresponding access method, each single-valued attribute generally accompanied with a pair of get / set methods. Corresponding to the attribute name and the attribute relating to get / set method name. For example, if there is a property called "xxx", then there will be setXxx and getXxx method.

        In addition, Boolean (Boolean) a special attribute is single-valued attribute, which allows only two values: true and false, if there is a file named "xxx" Boolean attribute, the method can be accessed by isX.
3.2. Index (Indexed) property
        if you need to define a number of different types of properties, using a single value of the property will become very cumbersome, in order to solve this problem, JavaBean provides index (Indexed) property, property index refers to the JavaBean array type the member variables. Using the attribute corresponding to set / get the method can obtain the value of the array. Obtaining property index value of an element or attribute of the corresponding access method through, or may be provided to obtain a value of the entire property.

3.3. And associated attributes (Bound)
        is associated (Bound) when the attribute indicates the attribute of the seed value is changed, to notify other objects. Each time property values change, this property will trigger a PropertyChange event (in the Java program, the event is also an object). Event encapsulates the attribute name, the new value of the original value, attribute change attribute. This event delivery to other Beans, Beans as to what should be done to receive event action, by its own definition.

        It called JavaBean property change events. External class objects associated with these events are called Java Bean listener (Listener). Listeners may only have a JavaBean property related to events of interest, may also have interest in all property related events, thus providing two types of JavaBean event listener method registered and unregistered, that is a global event listener registration, cancellation method and generic event listener registration, cancellation of methods.
3.4. Constraints (Constrained) property
        when Java Bean properties if changes related to the external object should first check the reasonableness of this property changes and then decide whether to accept this change, such JavaBean property called constraint (Constrained) property. When the change restriction attribute is denied, a method of changing the constraint attribute generates a constraint attribute change exception (PropertyVetoException), by the exception handling, JavaBean constraint property revert back to the original value, and for the restore operation to send a new attribute modification notification .

        Change the restriction attribute may be denied, so it's setXxx general setXxx other JavaBean properties are different. Write Constrained properties are as follows:

        public void setXxx (xxxType newXxx) throws PropertyVetoException
four. Java Bean Methods
4.1. constructor
        Java Bean constructor and before the ordinary java class constructor mean the same thing, is to JavaBean properties and methods to initialize, namely set an initial value for the properties and methods defined constructor name to the class name of the same JavaBean.
4.2. Access method
       in the definition of the Bean property, and by the constructor after its initialization, to make other programs to access these attributes Bean, it is necessary to create an access method. Access method is defined in the access to the properties of the components comprising two kinds of read and write access. The value read is the value of a function of one kind of property taken for Bean, i.e. getters; written assignment function is provided for the Bean properties, i.e. setter. Listed below are the specific syntax Bean property accessor method:

        public void setPropertyName (PropertyTypevalue); // assigns them to write a method that is

        public PropertyType getPropertyName (); // read the attribute values, i.e., the read method

4.3 General methods
        in addition to property access methods, you can also create a general method to achieve the Bean calls to functions, as long as the Bean is generally defined as a public method of method, you can call for other programs.
Five. Java Bean event
        event handling is one of the core JavaBeans architecture. By event handling mechanism that allows some components as the event source, emit events that can be received describing the environment or other components. Thus, different components can be combined with the configuration tool, communication between the components by passing event, constitute an application. Conceptually, the event is a mechanism to transfer some state changes between "sources" and "listener objects." Events have many different uses, such as mouse events in the Windows system often to be processed, window borders, change events, keyboard events.

5.1 event model

        Java Bean event model as shown, is a JavaBean class event source object that passes the property changes of the time object to the event listener, the event listener handles the event. Event listeners must register at the event source.
Good programmers learning Java Java bean line is what the concept
5.2. Event state object

        Status information related to an event occurring in the event state are generally packaged objects (EventState Object), this object is a subclass of java.util.EventObject. According to the design habits, such an event object class name of the state should be based on Event ends. For example, a mouse movement event code example.

5.3. Event listener interfaces and event listeners

        Since Java event model is based on a method call, and therefore need a way to define and organize the event manipulation method. JavaBeans, the event manipulation methods are defined in the Good programmers learning Java Java bean line is what the conceptinherited class java.util.EventListener event listener (EventListener) interface, according to regulations, EventListener interface name to end with Listener. If you want to manipulate any of the classes in the EventListener interface, the method must be defined in order to implement the interface mode. This class is the event listener. For example the following code:

// define a mouse move event object

public class MouseMovedExampleEventextends java.util.EventObject {

// contains status information and events related to mouse movement in this class

...

}

// define a listener interfaces mouse motion events

interface MouseMovedExampleListener extends java.util.EventListener {

// define a method for moving a mouse event listener should support this interface

void mouseMoved(MouseMovedExampleEvent mme);

}

5.4 The event listener registration and cancellation

        In order to make every possible event listeners to register themselves into the appropriate event source, the event flow between the source and an event listener, the event source must provide registration and cancellation method for the event listener is established. In practice, the event listener registration and deregistration To use the following format standard design:

public void add<ListenerType>(< ListenerType> listener);

public void remove<ListenerType>(< ListenerType> listener);

        Here is a specific example, first defines an event listener interfaces:

import java.util. *;

public interface ModelChangedListenerextends EventListener

{ void modelChanged(EventObjecte);}

5.5 The adaptation class
        adaptation class Java event model is an extremely important part. In some applications, the event should be "forwarded" from the source to transfer between the listener through the adaptation class. For example: When an event source emits an event, there are several event listener objects can receive the event, but only react to the specified object, the adapter will insert an event class between the event source and the event listener, the adapter class to specify which events should be in response to the listener. Adaptation class has become an event listener, the event is the actual source adaptation class as a listener registered listener into the queue, while those who did not respond to the real event listener in the queue, incident response should be made by the appropriate action decision with class. At present, most of the development tools when generating code, event processing is carried out by adapting the class.

Guess you like

Origin blog.51cto.com/14479068/2426765