37

可能重复:
豆子有什么意义?

什么是javabean?它是干什么用的?还有哪些代码示例?我听说它用于与 getter 和 setter 方法有关的事情?我对 java bean 是什么以及你在哪里访问它感到很困惑。我用谷歌搜索了它,但找不到明确的答案。

4

4 回答 4

52

Java Bean 是一个普通的 Java 类,它具有私有属性及其公共 getter 和 setter 方法。

Java Beans 通常用作帮助类。

例子 -

public class User implements java.io.Serializable {

    private String name;
    private Integer age;

    public String getName(){
        return this.name;
    }

    public void setName(String name){
        this.name = name;
    }

    public Integer getAge(){
        return this.age;
    }

    public void setAge(Integer age){
        this.age = age;
    }
}

实现Serializable不是强制性的,但如果您想将 Javabeans 持久化或传输到 Java 内存之外,例如在硬盘中或通过网络,实现是非常有用的。

使用JavaBeans的地方?

于 2012-07-10T05:15:53.443 回答
25

JavaBean 是 Java 的可重用软件组件。实际上,它们是用符合特定约定的 Java 编程语言编写的类。它们用于将许多对象封装到单个对象(bean)中,以便它们可以作为单个 bean 对象而不是多个单独的对象传递。JavaBean 是可序列化的 Java 对象,具有 0 参数构造函数,并允许使用 getter 和 setter 方法访问属性。

优点

  • 一个 Bean 获得了 Java 的“一次编写,随处运行”范式的所有好处。
  • 可以控制暴露给另一个应用程序的 Bean 的属性、事件和方法。
  • 可以提供辅助软件来帮助配置 Bean。
  • Bean 的配置设置可以保存在持久存储中,并且可以在以后恢复。
  • 一个 Bean 可以注册以接收来自其他对象的事件,并且可以生成发送给它的事件。

缺点

  • 具有空构造函数的类可能会在无效状态下被实例化。如果这样的类是由开发人员手动实例化的(而不是由某种框架自动实例化),开发人员可能不会意识到他已将类实例化为无效状态。编译器无法检测到此类问题,即使已记录在案,也不能保证开发人员会看到文档。
  • 必须为每个属性创建一个 getter 并为许多、大多数或所有属性创建一个 setter,这会产生大量的样板代码。

例子 :

package beans;

/**
 * Class <code>PersonBean</code>.
 */
public class PersonBean implements java.io.Serializable {

    private String name;

    private boolean deceased;
    static final long serialVersionUID = 1L;

    /** No-arg constructor (takes no arguments). */
    public PersonBean() {
    }

    /**
     * Property <code>name</code> (note capitalization) readable/writable.
     */
    public String getName() {
        return this.name;
    }

    /**
     * Setter for property <code>name</code>.
     * @param name
     */
    public void setName(final String name) {
        this.name = name;
    }

    /**
     * Getter for property "deceased"
     * Different syntax for a boolean field (is vs. get)
     */
    public boolean isDeceased() {
        return this.deceased;
    }

    /**
     * Setter for property <code>deceased</code>.
     * @param deceased
     */
    public void setDeceased(final boolean deceased) {
        this.deceased = deceased;
    }
}

参考http://en.wikipedia.org/wiki/JavaBeans

根据@Andy 的评论,我接受我们应该声明serialVersionUID。根据Java 文档

If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members. Array classes cannot declare an explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is waived for array classes.

如果您没有明确指定 serialVersionUID,则会自动生成一个值 - 但这很脆弱,因为它依赖于编译器实现。

于 2012-07-10T05:18:25.830 回答
7

Well, the JavaBean API defines a number of conventions regarding to JavaBeans. According to Wikipedia:

The required conventions are as follows:

  • The class must have a public default constructor (no-argument). This allows easy instantiation within editing and activation frameworks.
  • The class properties must be accessible using get, set, is (used for boolean properties instead of get) and other methods (so-called accessor methods and mutator methods), following a standard naming convention. This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties. Setters must receive only one argument.
  • The class should be serializable. It allows applications and frameworks to reliably save, store, and restore the bean's state in a fashion independent of the VM and of the platform.

Quite often, these are the most common types of classes that can be found in an application, as they can be used to model the data that is used. An example of such a bean can be seen below:

public class Person implements Serializable
{
  private String name;
  private boolean alive;

  public Person()
  {
    // constructor logic...
  }

  public String getName()
  {
    return name;
  }

  public void setName(String name)
  {
    this.name = name;
  }

  public boolean isAlive()
  {
    return alive;
  }

  public void setAlive(boolean alive)
  {
    this.alive = alive;
  }
}

As you can see, properties are reflected in the getters and setters.

HTH

于 2012-07-10T05:23:16.193 回答
1

如果您在谈论java-beans 而不是 EJB Beans,那么这里是解释......

1. JavaBean 有一个不带参数的构造函数。

2. JavaBean 有一组属性。

3. JavaBean 具有允许访问其底层属性的访问器(getXxx,或布尔属性的 isXxx)方法和修改器方法(setXxx)。

第三点说明了一个带有私有实例变量和公共 getter、setter 的 java 类。

例如:

import java.util.Date;

public class User {
    private Date loginDate;
    private String name;
    private String password;

    public User() { }

    public Date getLoginDate() {
        return loginDate;
    }

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    public void setLoginDate(Date loginDate) {
        this.loginDate = loginDate;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void delete() {
        // code to delete user from database
    }

    public void update() {
        // code to update user in database
    }

    public static User getUser(String name) {
        // code returning a new User object
        // populated from the database
    }
}
于 2012-07-10T05:18:29.700 回答