《chapter06 中间件 j2ee javabean》由会员分享,可在线阅读,更多相关《chapter06 中间件 j2ee javabean(38页珍藏版)》请在金锄头文库上搜索。
1、1 1JavaBeanJavaBean Components - Agenda Components - Agendau What is JavaBean? u JavaBean Charictristics u JavaBean Properties u JavaBean Event u JavaBean Persistance u JavaBean Versioning u JavaBean Introspection u JavaBean Customization u JavaBean Deployment u JavaBean Usage2 2What is What is Java
2、BeanJavaBean? ? A JavaBean is a reusable software component that can be manipulated visually in a builder tool JavaBeans is a portable, platform-independent component model written in the Java programming language, developed in collaboration with industry leaders. It enables developers to write reus
3、able components once and run them anywhere - benefiting from the platform-independent power of Java technology. The goal of JavaBeans is to create a system whereby application developers can take a set of beans from a stock library and wire them together to make a full application3 3JavaBeanJavaBean
4、 CharctristicsCharctristics Support for properties both for customization and for programmatic use Support for events as a simple communication metaphor than can be used to connect up beans Support for persistence so that a bean can be customized in an application builder and then have its customize
5、d state saved away and reloaded later Support for introspection so that a builder tool can analyze how a bean works Support for customization so that when using an application builder a user can customize the appearance and behavior of a bean4 4JavaBeanJavaBean Properties - 1 Properties - 1 Discrete
6、, named attributes that determine the appearance and behavior and state of a component Accessible programmatically through access methods Accessible visually through property sheets Exposed as object fields in a scripting environment5 5JavaBeanJavaBean Properties - 2 Properties - 2JavaBean Property
7、Types Simple Properties Bound Properties Constrained Properties6 6JavaBeanJavaBean Properties - 3 Properties - 3Simple Properties Represent a single value The access methods should follow standard naming conventionspublic get();public void set( value);Example:public String getHostName();public void
8、setHostName( String hostName );7 7JavaBeanJavaBean Properties - 4 Properties - 4Boolean Properties They are simple properties The getter methods follow an optional design patternpublic boolean is();Example:public boolean isConnected();8 8JavaBeanJavaBean Properties - 5 Properties - 5Indexed Properti
9、es Represent an array of valuespublic get(int index);public void set(int index, value);public get();public void set( values);Example:public Color setPalette(int index);public void setPalette(int index,Color value);public Color getPalette();public void setPalette(Color values);9 9JavaBeanJavaBean Pro
10、perties - 6 Properties - 6Bound Properties Registered listeners object are notified when the value of the property changes Listeners must implement the java.beans.PropertyChangeListener interfacepropertyChange(PropertyChangeEvent event);1010JavaBeanJavaBean Properties - 7 Properties - 7Bound Propert
11、ies Support FrameworkTerminalBean hostName portNumber addPropertyChangeListener()removePropertyChangeListener()PropertyChangeEvent sourcenewValuepropertyNameoldValuegeneratesusesnotifiesPropertyChangeListenerpropertyChange()PropertyChangeSupportaddPropertyChangeListener() removePropertyChangeListene
12、r() firePropertyChange()1111JavaBeanJavaBean Properties - 8 Properties - 8Bound Properties Notification Mechanism:PropertyEditor:Bean:PropertyChangeListener:PropertyChangeSupport1. addPropertyChangeListener2. addPropertyChangeListener3. SetHostName(“hostName”)4. firePropertyChange(event)5. propertyC
13、hange(event)1212JavaBeanJavaBean Properties - 9 Properties - 9Constrained Properties Allow registered listeners to validate a proposed change Listeners must implement the java.beans.VetoablecChangeListener interfacevetoableChange( PropertyChangeEvent event ) throws PropertyVetoException;1313JavaBean
14、JavaBean Properties - 10 Properties - 10Constrained Properties Support FrameworkBeanhostName portNumberaddVetoableChangeListener() removeVetoableChangeListener()PropertyChangeEventsourcenewValuepropertyName oldValueVetoableChangeSupportaddVetoableChangeListener() removeVetoableChangeListener()genera
15、tesusesnotifiesVetoableChangeListenervetoableChange()1414JavaBeanJavaBean Properties - 11 Properties - 11Constrained Properties - Examplepublic void setHostName( String newHostName ) throws java.beans.PropertyVetoException String oldHostName = this.hostName;/ First tell the vetoers about the change.
16、 If anyone objects, we/ dont catch the exception but just let if pass on to our caller.vetoableChangeSupport.fireVetoableChange( “hostName“, oldHostName, newHostName );/ change accepted; update statethis.hostName = newHostName;/ notify property change listenerspropertyChangeSupport.firePropertyChange(“hostName“, oldHostName, newHostName ); 1515JavaBeanJavaBean Properties - 12 Properties - 12Constraine