使用 Java Bean 有什么好处?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3461199/
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-10-30 01:57:04  来源:igfitidea点击:

What is the advantage of using Java Beans?

javajavabeans

提问by Brian S

I believe I understand what Java Beans are: Java class(es) which contain a no-arg constructor, are serializable, and expose their fields with getters and setters.

我相信我理解 Java Beans 是什么:包含无参数构造函数的 Java 类,可序列化,并使用 getter 和 setter 公开它们的字段。

  1. Does a Java Bean have to expose allof its fields in order to qualify as a bean? If no, does it even have to expose any?

  2. May Java Beans include constructors with arguments as well as a no-arg constructor?

  3. What is the purpose of Java Beans, other than to conform to a certain coding style? It seems there is a lot of talk about 'beans this' or 'beans that', but I don't know whythey are advantageous, specifically.

  1. Java Bean 是否必须公开其所有字段才能成为 Bean?如果没有,它甚至必须暴露任何吗?

  2. Java Beans 可以包括带参数的构造函数和无参数的构造函数吗?

  3. 除了符合某种编码风格之外,Java Beans 的目的是什么?似乎有很多关于“这个豆子”或“那个豆子”的讨论,但我不知道它们为什么特别有利。

I can totally get making the no-arg constructor. There can be a slew of reasons for it, and I wouldn't be surprised if a no-arg constructor helps the compiler do some optimizations, either. I can also understand making your class serializable. Even if the class is never serialized, it couldbe, and going back to do it retroactively could be annoying (or impossible in a black-boxed library).

我完全可以制作无参数构造函数。可能有很多原因,如果无参数构造函数帮助编译器进行一些优化,我也不会感到惊讶。我也可以理解使您的类可序列化。即使该类从未被序列化,它也可能是,并且回溯性地进行序列化可能会很烦人(或者在黑盒库中是不可能的)。

But most curious is the requirement to have fields all accessible via getters and setters. I do use them in my own work when I have need of them, but it seems odd that Java Beans requires them (possibly all of them, depending on my answer to #1). If it's an issue with reflection, couldn't the reflection get the fields just as easily? If it's an issue with doing more than simply setting the value, couldn't the reflection use a getter/setter over a field if the method exists?

但最奇怪的是要求所有字段都可以通过 getter 和 setter 访问。当我需要它们时,我确实在我自己的工作中使用它们,但是 Java Beans 需要它们似乎很奇怪(可能是全部,取决于我对 #1 的回答)。如果是反射的问题,反射不能同样容易地获得字段吗?如果不仅仅是简单地设置值是一个问题,如果该方法存在,反射不能在字段上使用 getter/setter 吗?

采纳答案by PhilDin

A JavaBean on its own is not terribly interesting, it's just a Java class that conforms to some standards that you listed above. However, conformance with this standard is one of the pillars on which the Java EE framework is built and it comes up in quite a few places. I suspect that when you hear about all of the great things that JavaBeans can do, what's being referred to in Enterprise JavaBeans (EJBs). FYI, there are a few different types of EJB listed below:

JavaBean 本身并不是很有趣,它只是符合您上面列出的某些标准的 Java 类。但是,符合此标准是构建 Java EE 框架的支柱之一,它出现在很多地方。我怀疑当您听说 JavaBeans 可以做的所有伟大的事情时,Enterprise JavaBeans (EJBs) 中所指的是什么。仅供参考,下面列出了几种不同类型的 EJB:

  1. Entity Beans
  2. Stateful Session Beans
  3. Stateless Session Beans
  1. 实体豆
  2. 有状态会话 Bean
  3. 无状态会话 Bean

Some details now follow...

一些细节现在跟随......

Entity Beans

实体豆

You might want to read/write objects to/from an underlying database. You could use JDBC/SQL to do this but you could also use a persistance framework. The Java EE spec includes a spec for persistance whereby you declare your class to be an "entity bean" and have Java automatically generate database tables and logic to map between entries in your database and objects in your program. Originally, persistance was something that required the use of an Application Server (such as Glassfish, JBoss, Geronimo etc.) but AFAIK, you can do use it in desktop apps with no server component. The actual implementation is provided by a lower level library such as Eclipselink, Toplink, Hibernate etc. but the Java API abstracts away any differences between them.

您可能希望从底层数据库读取/写入对象。您可以使用 JDBC/SQL 来执行此操作,但您也可以使用持久性框架。Java EE 规范包括一个持久性规范,您可以通过该规范将类声明为“实体 bean”,并让 Java 自动生成数据库表和逻辑以在数据库中的条目和程序中的对象之间进行映射。最初,持久性需要使用应用服务器(例如 Glassfish、JBoss、Geronimo 等),但 AFAIK,您可以在没有服务器组件的桌面应用程序中使用它。实际实现由较低级别的库(例如 Eclipselink、Toplink、Hibernate 等)提供,但 Java API 抽象了它们之间的任何差异。

Stateful Session Beans

有状态会话 Bean

Imagine that you want to create an instance of a Java class which exists on separate JVM. The JVMs might be running on the same physical machine but equally, may be on separate machines communicating over a network. Using a Java EE application server, you can create a class which can be instantiated by clients of the app server. These clients can instantiate a class which will act just like a normal object but any methods that are invoked on the object get executed on the server with the results being passed back to the caller. It's basically an object oriented form of remote procedure calls.

想象一下,您想要创建一个存在于单独 JVM 上的 Java 类的实例。JVM 可能运行在同一台物理机器上,但同样也可能运行在通过网络通信的不同机器上。使用 Java EE 应用服务器,您可以创建一个可由应用服务器的客户端实例化的类。这些客户端可以实例化一个类,该类的行为就像一个普通对象,但是在该对象上调用的任何方法都会在服务器上执行,并将结果传递回调用者。它基本上是远程过程调用的面向对象形式。

Stateless Session Beans

无状态会话 Bean

This is a minor variation on stateful session beans. With stateful beans, if the server has 1000 clients then it will potentially have to create 1000 instances of the bean and remember which instance belongs to which client. With stateless beans, the server creates a pool of beans and doesn't bother to remember which client ownswhich bean. When a client invokes a method, the server picks a bean from the pool and uses it, returning it to the pool on completion. You use stateful session beans when you want the server to rememberdetails about each client, you will use stateless beans when you don't need to remember client specific details. Note that the stateless beans may well have state, it's just that this state won't be of interest to the client.

这是有状态会话 bean 的一个小变化。对于有状态 bean,如果服务器有 1000 个客户端,那么它可能必须创建 1000 个 bean 实例并记住哪个实例属于哪个客户端。使用无状态 bean,服务器创建一个 bean 池并且不会费心记住哪个客户端拥有哪个 bean。当客户端调用一个方法时,服务器从池中挑选一个 bean 并使用它,在完成时将它返回到池中。当您希望服务器记住有关每个客户端的详细信息时,您可以使用有状态会话 bean,当您不需要记住客户端特定的详细信息时,您将使用无状态会话 bean 。请注意,无状态 bean 可能有状态,只是客户端不会对这种状态感兴趣。

回答by BalusC

They adhere to a clear specification.

他们遵守明确的规范

Thanks to this there are insanely a lot of tools to ease working with Javabeans (or just the other way round). There are tools which can autogenerate them based on some data in a certain flavor (XML, JSON, CSV, DDL, etc) and/or vice versa, as well to read/manipulate/map them like Commons BeanUtils, Dozer, EZMorph, etcetera. Further there are a lot of MVC/ORM frameworks which works with Javabeans, like JPA, Hibernate, JSF, Spring, etc. Even a bit decent IDE like Eclipse knows how to autogenerate Javabeans based on just some fields.

多亏了这一点,有很多工具可以简化 Javabeans 的工作(或者相反)。有一些工具可以根据某种风格的数据(XMLJSONCSVDDL等)自动生成它们和/或反之亦然,以及读取/操作/映射它们,如Commons BeanUtilsDozerEZMorph等. 此外,还有很多 MVC/ORM 框架可以与 Javabeans 一起使用,例如JPAHibernateJSFSpring等。即使像 Eclipse 这样有点像样的 IDE 也知道如何仅基于某些字段自动生成 Javabeans。

It are the tools and frameworks around Javabeans which makes our life easier. It is the Javabeans specification which made those things exist.

正是围绕 Javabeans 的工具和框架让我们的生活更轻松。正是 Javabeans 规范使这些东西存在。

回答by Sjoerd

Most of all, a Java Bean is a reusable software component.

最重要的是,Java Bean 是一个可重用的软件组件

This means that it is not tightly coupled to other components. If your Java class creates an instance of another class, or returns some specific implementation class, it is no longer a bean. Beans cover some well-defined piece of functionality and are loosely coupled to other classes.

这意味着它没有与其他组件紧密耦合。如果您的 Java 类创建了另一个类的实例,或者返回了某个特定的实现类,则它不再是一个 bean。Bean 涵盖了一些定义明确的功能,并且与其他类松散耦合。

The advantage of this is that you get all these little pieces that you can then get to work together pretty easily. Also, these are easy to reuse and unittest.

这样做的好处是您可以获得所有这些小部件,然后您可以轻松地一起工作。此外,这些易于重用和单元测试。

Even if you are not using some visual environment to couple beans (as the bean spec suggests), this is still a reason to use beans: to get small pieces of code which are easily usable.

即使您没有使用一些可视化环境来耦合 bean(正如 bean 规范所建议的那样),这仍然是使用 bean 的一个原因:获得易于使用的小段代码。

If you don't use a visual tool, it is not that important that your bean has a 0-argument constructor, or is serializable.

如果您不使用可视化工具,那么您的 bean 是否具有 0 参数构造函数或可序列化并不那么重要。

回答by Suman Astani

1) Beans is platform independent, that means it can be run anywhere.

1) Beans 是平台无关的,这意味着它可以在任何地方运行。

2) It can be run in any locale and is distributed in nature.

2)它可以在任何语言环境中运行,并且是分布式的。

3) Methods, properties and events of Beans can be controlled.

3)可以控制Beans的方法、属性和事件。

4) It is easy to configure Java beans.

4) 配置Java bean 很容易。

5) A bean can both receive and create events.

5) bean 既可以接收事件,也可以创建事件。

6) Configuration settings of a bean can be stored persistently and can be retrieved any time.

6) bean 的配置设置可以永久存储并可随时检索。

回答by Vipin

I guess biggest advantage of bean is that one can pass data (more or less) in the form of single object. There is no need to make a lot of parameters in the definition of a method, rather one can use bean instead and pass data in the form of object directly. It provides better architecture of the programme...Thats what I think

我想 bean 的最大优点是可以以单个对象的形式传递数据(或多或少)。一个方法的定义不需要太多的参数,而是可以用bean代替,直接以对象的形式传递数据。它提供了更好的程序架构......这就是我的想法

回答by Johannes Wachter

The biggest advantage is that your beans are built according to a specification and can be used directly with "bean-compatible" libraries and frameworks.

最大的优点是您的 bean 是根据规范构建的,可以直接与“bean 兼容”库和框架一起使用。

For example, most frameworks for Serialization (XML, JSON, YAML, ...) often can use beans directly without configuration.

例如,大多数序列化框架(XML、JSON、YAML 等)通常可以直接使用 bean,无需配置。