简体   繁体   English

JavaBean 和 BeanInfo 的关系,它们有关系吗?

[英]JavaBean and relation with BeanInfo, are they related?

Is there any relationship between JavaBean and BeanInfo ? JavaBean 和BeanInfo之间有什么关系吗? I read various posts/questions and about Java Bean it is mentioned a bean is a regular class which adheres to some rule (private data members, getters() , setters(), implements Serializabe interface...).我阅读了各种帖子/问题,关于 Java Bean 提到一个 bean 是一个遵循某些规则的常规类(私有数据成员、getters()、setters()、实现Serializabe接口......)。

I was going through book "Java Complete reference, 8th Edition" and came across BeanInfo, in chapter on "Java Beans".我正在阅读“Java 完整参考,第 8 版”一书,并在“Java Beans”一章中遇到了 BeanInfo。 What relation a Java Bean has with BeanInfo? Java Bean 与 BeanInfo 有什么关系?

Although I tried to find on various posts, I am still not able to fully understand how Java beans are helpful, how does following some rules by a class (thereby making it a bean) makes it helpful which a regular Java class can't do?虽然我试图在各种帖子中找到,但我仍然无法完全理解 Java bean 是如何有用的,遵循类的一些规则(从而使其成为 bean)如何使它有用,而常规 Java 类无法做到?

tl;dr tl;博士

  • Implicit隐式
    • The getter/setter method naming conventions implicitly identify properties when the class is considered to be a JavaBean .当类被视为JavaBean时,getter/setter 方法命名约定隐式标识属性。
  • Explicit显式
    • Alternatively, you can identify those properties explicitly by defining a partner class that implements the BeanInfo interface.或者,您可以通过定义实现BeanInfo接口的合作伙伴类来显式标识这些属性。
    • In Java 9 and later, you may use annotations to more conveniently implement BeanInfo .在 Java 9 及更高版本中,您可以使用注释来更方便地实现BeanInfo

Details细节

The JavaBeans spec was originally meant to be "a reusable software component that can be manipulated visually in a builder tool" such as drag-and-drop IDE form-building tools. JavaBeans 规范最初旨在成为“一个可重用的软件组件,可以在构建器工具中进行可视化操作”,例如拖放式IDE表单构建工具。 That never really worked out.那从来没有真正奏效。

Instead, people commonly use the JavaBeans approach as a way of identifying properties.相反,人们通常使用 JavaBeans 方法作为识别属性的一种方式。 For example, the BeanItemContainer in Vaadin 7.例如, Vaadin 7 中的BeanItemContainer

At a minimum, a JavaBean must:至少,JavaBean 必须:

You can define a JavaBean either implicitly or explicitly.您可以隐式或显式定义 JavaBean。

JavaBean naming conventions JavaBean 命名约定

The implicit way to define a JavaBean is through naming conventions.定义 JavaBean 的隐式方法是通过命名约定。 Any methods starting with get , set , or is are detected byreflection / introspection and considered to identify a property.任何以getsetis开头的方法get反射/内省检测到并被认为是识别属性。 The imaginary property may or may not indeed be backed by a member variable on the class.虚构的属性可能确实由类上的成员变量支持,也可能不支持。

If a Person class has getEyeColor and setEyeColor methods, then as a JavaBean we perceive a read-write “eyeColor” property.如果Person类具有getEyeColorsetEyeColor方法,那么作为 JavaBean,我们会感知到一个可读写的“eyeColor”属性。 A getter without a setter makes the property read-only.没有 setter 的 getter 使属性只读。

BeanInfo interface BeanInfo接口

The explicit way to define a JavaBean is to create another class alongside you intended JavaBean class.定义 JavaBean 的显式方法是在您想要的 JavaBean 类旁边创建另一个类。 The other class implements the BeanInfo interface.另一个类实现BeanInfo接口。 Most likely the other class is actually a subclass of the SimpleBeanInfo class.很可能另一个类实际上是SimpleBeanInfo类的子类。 That SimpleBeanInfo class implements the BeanInfo interface in a negative manner, denying info.SimpleBeanInfo类以否定方式实现BeanInfo接口,拒绝信息。 You override the methods for the pieces of info you want to identify aspects of your JavaBean class.您可以覆盖要标识 JavaBean 类方面的信息片段的方法。

You can use the BeanInfo partner class to identify the properties (instead of using the getter/setter naming convention).您可以使用BeanInfo合作伙伴类来标识属性(而不是使用 getter/setter 命名约定)。 And you can identify other aspects of a JavaBean.您可以识别 JavaBean 的其他方面。 Many of those other aspects are outmoded as they relate to the JavaBean being a widget to appear in an IDE form-building tool, but you may still find some aspects useful.许多其他方面已经过时,因为它们与 JavaBean 是出现在 IDE 表单构建工具中的小部件相关,但您可能仍然发现某些方面很有用。

The reflection/introspection facilities in Java automatically detect and process your BeanInfo classes to provide the meta-data about your JavaBean classes. Java 中的反射/内省功能会自动检测和处理您的 BeanInfo 类,以提供有关您的 JavaBean 类的元数据。

See Oracle Tutorial page .请参阅Oracle 教程页面

BeanInfo Annotations BeanInfo 注释

Java 9 may help with some aspects of a JavaBean: annotations .Java 9可能对 JavaBean 的某些方面有所帮助: annotations I have not yet understood their proper usage.我还没有理解它们的正确用法。 I have asked in another Question, How to use new BeanInfo Annotations in Java 9 .我在另一个问题中问过, How to use new BeanInfo Annotations in Java 9 I had hoped this would allow annotating member variables as properties to avoid having to write empty getter/setter methods — but apparently this JEP does not provide this feature (I'm not entirely sure).我曾希望这将允许将成员变量注释为属性以避免必须编写空的 getter/setter 方法——但显然这个 JEP 没有提供这个功能(我不完全确定)。

See JEP 256: BeanInfo Annotations on the OpenJDK project.请参阅JEP 256: OpenJDK项目上的BeanInfo 注释

JavaBeans spec JavaBeans 规范

There is much more to JavaBeans than just properties like “eyeColor”, though properties are certainly the most common purpose for using JavaBeans. JavaBeans 不仅仅是像“eyeColor”这样的属性,尽管属性肯定是使用 JavaBeans 的最常见目的。

I suggest studying the quite readable JavaBeans 1.01 specification .我建议研究可读性很强的JavaBeans 1.01 规范 And read the Oracle Tutorial .并阅读Oracle 教程

For a technical overview, I suggest reading this posting, The JavaBeans specification by Stephen Colebourne.对于技术概述,我建议阅读这篇文章,Stephen Colebourne 的 JavaBeans 规范

Bean Validation Bean 验证

On a related note… The Bean Validation standard is becoming a popular way to declare and enforce business rules for the conditions on data values within an object.与此相关的是…… Bean 验证标准正在成为一种流行的方式,用于为对象内数据值的条件声明和实施业务规则。 For example, rules might be "eye color is a required field, and must not be null or empty string" or "invoice total must be zero or positive, never a negative number".例如,规则可能是“眼睛颜色是必填字段,不得为空或空字符串”或“发票总额必须为零或正数,决不能为负数”。

There have been three versions of the standard (1.0, 1.1 in JSR 349 , & 2.0 in JSR 380 ) and various implementations.该标准三个版本JSR 349 中的1.0、1.1 和JSR 380 中的2.0 )和各种实现。 Bean Validation can be used at either client-side (Swing, JavaFX, etc.) or server-side. Bean 验证可用于客户端(Swing、JavaFX 等)或服务器端。 Vaadin , for example, supports its own technology for validation during data-entry as well as supporting you plugging in a Bean Validation implementation.例如, Vaadin支持其自己的数据输入期间的验证技术,并支持您插入Bean 验证实现。

Enterprise JavaBeans企业 JavaBeans

Do not confuse JavaBeans with Enterprise JavaBeans (EJB) .不要将 JavaBeans 与Enterprise JavaBeans (EJB)混淆。 Re-using the “JavaBean” trademark for EJB was a poor decision by the Sun marketing staff. Sun 营销人员对 EJB 重新使用“JavaBean”商标是一个糟糕的决定。 EJB is totally unconnected and distinct from the original JavaBeans. EJB 与原始的 JavaBeans 完全没有联系并且不同。

Records记录

If what you want is a simple way to represent structured data as properties in a Java object, look into the new Records feature being previewed in Java 14. See JEP 359 .如果您想要一种将结构化数据表示为 Java 对象中的属性的简单方法,请查看 Java 14 中预览的新记录功能。请参阅JEP 359

Records provide a compact syntax for declaring classes which are transparent holders for shallowly immutable data.记录为声明类提供了一种紧凑的语法,这些类是浅不可变数据的透明持有者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM