[转载]
JavaBeans就是Java的可重用组件技术。ASP通过COM来扩充复杂的功能, 如文件上载、发送email以及将业务处理或复杂计算分离出来成为独立可重复利用的模块。JSP通过JavaBean实现了同样的功能扩充。JSP 对于在Web 应用中集成JavaBean 组件提供了完善的支持。这种支持不仅能缩短开发时间(可以直接利用经测试和可信任的已有组件,避免了重复开发),也为JSP 应用带来了更多的可伸缩性。JavaBean 组件可以用来执行复杂的计算任务,或负责与数据库的交互以及数据提取等。在实际的JSP开发过程中,读者将会发现,和传统的A S P或P H P页面相比,JSP页面将会是非常简洁的,由于JavaBeans开发起来简单,又可以利用J a v a语言的强大功能,许多动态页面处理过程实际上被封装到了JavaBeans中。
Summary of JavaBean Rules
A JavaBean has these characteristics:
· It is a public class.
· It has a public parameterless constructor (though it may have other constructors
as well)
· It implements Serializable interface (i.e. it can be made persistent, so its state can
be saved)
· It has properties with “getter” and “setter” methods named by following
JavaBeans naming patterns
· It has events which follow the standard Java event model with the registration
methods named by following the JavaBeans naming patterns
· It may have other methods which do not follow the naming patterns. These
methods are not exposed by a builder tool.
Comments:
A user of JavaBean components can built an application consisting of JavaBeans, by
customizing, and wiring JavaBeans together at design time, by using a builder tool in
an IDE (Integrated Development Environment), such as JBuilder.
The builder tool analyses and exposes the properties and events of the JavaBeans at
design time by means of introspection. A builder tool can do this only if proper naming
patterns have been followed to name the properties, and event registration methods (we
will not talk about the beanInfo class in these notes)
Naming patterns for properties:
Property name: <propertyName>
e.g. temperature
Property type: <Type>
e.g. double
Getter method: public <Type> get<PropertyName>() { //… }
e.g. double getTemperature() { // … }
Boolean Property Getter Method: public boolean is<PropertyName>() {
// … }
e.g. public boolean isFull() { // … }
Setter method: public void set<PropertyName>(<Type> p) { // … }
e.g. void setTemperature(double temp) { // … }
… Notes to be continued …