究竟什么是 JavaBean?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3295496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:50:04  来源:igfitidea点击:

What is a JavaBean exactly?

javajavabeansserializable

提问by Amir Rachum

I understood, I think, that a "Bean" is a Java class with properties and getters/setters. As much as I understand, it is the equivalent of a C struct. Is that true?

我认为,“Bean”是具有属性和 getter/setter 的 Java 类。据我所知,它相当于一个 C 结构体。真的吗?

Also, is there a real syntacticdifference between a bean and a regular class? Is there any special definition or an interface?

另外,bean 和普通类之间是否存在真正的语法差异?有什么特殊的定义或接口吗?

Basically, why is there a term for this?

基本上,为什么有一个术语?

Also what does the Serializableinterface mean?

还有Serializable界面是什么意思?

采纳答案by hvgotcodes

A JavaBean is just a standard

JavaBean 只是一个标准

  1. All properties private (use getters/setters)
  2. A public no-argument constructor
  3. Implements Serializable.
  1. 所有私有属性(使用getter/setter
  2. 公共无参数构造函数
  3. 实施Serializable.

That's it. It's just a convention. Lots of libraries depend on it though.

就是这样。这只是一个约定。很多图书馆都依赖它。

With respect to Serializable, from the API documentation:

关于Serializable,来自API 文档

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

类的可序列化由实现 java.io.Serializable 接口的类启用。未实现此接口的类将不会对其任何状态进行序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。

In other words, serializable objects can be written to streams, and hence files, object databases, anything really.

换句话说,可序列化的对象可以写入流,因此可以写入文件、对象数据库等。

Also, there is no syntactic difference between a JavaBean and another class -- a class is a JavaBean if it follows the standards.

此外,JavaBean 和另一个类之间在语法上没有区别——如果一个类遵循标准,那么它就是一个 JavaBean。

There is a term for it because the standard allows libraries to programmatically do things with class instances you define in a predefined way. For example, if a library wants to stream any object you pass into it, it knows it can because your object is serializable (assuming the lib requires your objects be proper JavaBeans).

它有一个术语,因为该标准允许库以编程方式使用您以预定义方式定义的类实例进行操作。例如,如果一个库想要流式传输你传递给它的任何对象,它知道它可以,因为你的对象是可序列化的(假设 lib 要求你的对象是正确的 JavaBeans)。

回答by cHao

There's a term for it to make it sound special. The reality is nowhere near so mysterious.

有一个术语使它听起来很特别。现实远没有那么神秘。

Basically, a "Bean":

基本上,一个“豆”:

  • is a serializable object (that is, it implements java.io.Serializable, and does so correctly), that
  • has "properties" whose getters and setters are just methods with certain names (like, say, getFoo()is the getter for the "Foo" property), and
  • has a public 0-arg constructor (so it can be created at will and configured by setting its properties).
  • 是一个可序列化的对象(也就是说,它实现了java.io.Serializable,并且正确地执行了),即
  • 具有“属性”,其 getter 和 setter 只是具有某些名称的方法(例如,getFoo()是“Foo”属性的 getter),并且
  • 有一个公共的 0-arg 构造函数(因此可以随意创建并通过设置其属性进行配置)。

Update:

更新:

As for Serializable: That is nothing but a "marker interface" (an interface that doesn't declare any functions) that tells Java that the implementing class consents to (and implies that it is capable of) "serialization" -- a process that converts an instance into a stream of bytes. Those bytes can be stored in files, sent over a network connection, etc, and have enough info to allow a JVM (at least, one that knows about the object's type) to reconstruct the object later -- possibly in a different instance of the application, or even on a whole other machine!

至于Serializable:那只不过是一个“标记接口”(一个不声明任何函数的接口),它告诉 Java 实现类同意(并暗示它能够)“序列化”——一个转换的过程一个实例转换成字节流。这些字节可以存储在文件中,通过网络连接等发送,并且有足够的信息允许 JVM(至少,知道对象类型的 JVM)稍后重建对象 - 可能在不同的实例中应用程序,甚至在整个其他机器上!

Of course, in order to do that, the class has to abide by certain limitations. Chief among them is that all instance fields must be either primitive types (int, bool, etc), instances of some class that is also serializable, or marked as transientso that Java won't try to include them. (This of course means that transientfields will not survive the trip over a stream. A class that has transientfields should be prepared to reinitialize them if necessary.)

当然,为了做到这一点,类必须遵守某些限制。其中最主要的是所有实例字段必须是原始类型(int、bool 等)、某些类的实例也是可序列化的,或者标记为transientJava 不会尝试包含它们。(这当然意味着transient字段将无法在流中存活。transient如果需要,应该准备好具有字段的类重新初始化它们。)

A class that can not abide by those limitations should not implement Serializable(and, IIRC, the Java compiler won't even letit do so.)

不能遵守这些限制的类不应该实现Serializable(而且,IIRC,Java 编译器甚至不会让它这样做。)

回答by Mike

Regarding the second part of your question, Serialization is a persistence mechanism used to store objects as a sequence of signed bytes. Put less formally, it stores the state of an object so you can retrieve it later, by de-serialization.

关于问题的第二部分,序列化是一种用于将对象存储为带符号字节序列的持久性机制。不那么正式地说,它存储对象的状态,以便您以后可以通过反序列化来检索它。

回答by Kamal

JavaBeans are Java classes which adhere to an extremely simple coding convention. All you have to do is to

JavaBean 是遵循极其简单的编码约定的 Java 类。你所要做的就是

  1. implement java.io.Serializableinterface - to save the state of an object
  2. use a public empty argument constructor - to instantiate the object
  3. provide public getter/setter methods - to get and set the values of private variables (properties ).
  1. 实现java.io.Serializable接口 - 保存对象的状态
  2. 使用公共空参数构造函数 - 实例化对象
  3. 提供公共 getter/setter 方法 - 获取和设置私有变量(属性)的值。

回答by Truong Ha

You will find Serialization useful when deploying your project across multiple servers since beans will be persisted and transferred across them.

在跨多个服务器部署项目时,您会发现序列化很有用,因为 bean 将在它们之间持久化和传输。

回答by Marcus Thornton

To understand JavaBean you need to notice the followings: JavaBean is a conceptual stuff and can not represent a class of specific things

理解JavaBean需要注意以下几点: JavaBean是一个概念性的东西,不能代表一类具体的东西

JavaBean is a development tool can be visualized in the operation of reusable software components

JavaBean 是一个开发工具,可以在可重用的软件组件中可视化运行

JavaBean is based on the Sun JavaBeans specification and can be reusable components. Its biggest feature is the re-usability.

JavaBean 基于 Sun JavaBeans 规范并且可以是可重用的组件。它最大的特点是可重复使用。

回答by HANU

Java Beans are using for less code and more work approach... Java Beans are used throughout Java EE as a universal contract for runtime discovery and access. For example, JavaServer Pages (JSP) uses Java Beans as data transfer objects between pages or between servlets and JSPs. Java EE's JavaBeans Activation Framework uses Java Beans for integrating support for MIME data types into Java EE. The Java EE Management API uses JavaBeans as the foundation for the instrumentation of resources to be managed in a Java EE environment.

Java Beans 用于更少的代码和更多的工作方法...... Java Beans 在整个 Java EE 中用作运行时发现和访问的通用契约。例如,JavaServer Pages (JSP) 使用 Java Bean 作为页面之间或 servlet 和 JSP 之间的数据传输对象。Java EE 的 JavaBeans Activation Framework 使用 Java Beans 将 MIME 数据类型的支持集成到 Java EE 中。Java EE 管理 API 使用 JavaBean 作为在 Java EE 环境中管理要管理的资源的基础。

About Serialization:

关于序列化:

In object serialization an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.

在对象序列化中,对象可以表示为字节序列,其中包括对象的数据以及有关对象类型和存储在对象中的数据类型的信息。

After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.

序列化对象写入文件后,可以从文件中读取并反序列化,即表示对象及其数据的类型信息和字节可用于在内存中重新创建对象。

回答by Md Azaharuddin Ali

Properties of JavaBeans

JavaBean 的属性

A JavaBean is a Java object that satisfies certain programming conventions:

JavaBean 是满足某些编程约定的 Java 对象:

  1. The JavaBean class must implement either Serializableor Externalizable

  2. The JavaBean class must have a no-arg constructor

  3. All JavaBean properties must have public setter and getter methods

  4. All JavaBean instance variables should be private

  1. JavaBean 类必须实现SerializableExternalizable

  2. JavaBean 类必须有一个无参数构造函数

  3. 所有 JavaBean 属性都必须有公共的 setter 和 getter 方法

  4. 所有 JavaBean 实例变量都应该是私有的

Example of JavaBeans

JavaBean 示例

@Entity
public class Employee implements Serializable{

   @Id
   private int id;
   private String name;   
   private int salary;  

   public Employee() {}

   public Employee(String name, int salary) {
      this.name = name;
      this.salary = salary;
   }
   public int getId() {
      return id;
   }
   public void setId( int id ) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName( String name ) {
      this.name = name;
   }
   public int getSalary() {
      return salary;
   }
   public void setSalary( int salary ) {
      this.salary = salary;
   }
}

回答by Diganta

As per the Wikipedia:

根据维基百科:

  1. The class must have a public default constructor (with no arguments). This allows easy instantiation within editing and activation frameworks.

  2. The class properties must be accessible using get, set, is (can be used for boolean properties instead of get), and other methods (so-called accessor methods and mutator methods) according to 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 can have one or more than one argument.

  3. The class should be serializable. [This allows applications and frameworks to reliably save, store, and restore the bean's state in a manner independent of the VM and of the platform.]

  1. 该类必须有一个公共默认构造函数(没有参数)。这允许在编辑和激活框架内轻松实例化。

  2. 根据标准命名约定,必须使用 get、set、is(可用于布尔属性而不是 get)和其他方法(所谓的访问器方法和修改器方法)来访问类属性。这允许在框架内轻松自动检查和更新 bean 状态,其中许多包括用于各种类型属性的自定义编辑器。Setter 可以有一个或多个参数。

  3. 该类应该是可序列化的。[这允许应用程序和框架以独立于 VM 和平台的方式可靠地保存、存储和恢复 bean 的状态。]

For more information follow this link.

有关更多信息,请点击此链接。

回答by kenju

Explanation with an example.

举例说明。

1. import java.io.Serializable

1.导入java.io.Serializable

As for the Serialization, see the documentation.

至于序列化,请参阅文档

2. private fields

2. 私人领域

Fields should be private for prevent outer classes to easily modify those fields. Instead of directly accesing to those fields, usuagly getter/setter methods are used.

字段应该是私有的,以防止外部类轻松修改这些字段。不是直接访问这些字段,而是使用通常的 getter/setter 方法。

3. Constructor

3. 构造函数

A public constructor without any argument.

没有任何参数的公共构造函数。

4. getter/setter

4. 吸气剂/吸气剂

Getter and setter methods for accessing and modifying private fields.

用于访问和修改私有字段的 getter 和 setter 方法。

/** 1. import java.io.Serializable */
public class User implements java.io.Serializable {
    /** 2. private fields */
    private int id;
    private String name;

    /** 3. Constructor */
    public User() {
    }
    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    /** 4. getter/setter */
    // getter
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    // setter
    public void setId(int id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
}