3
votes

Is there any relationship between JavaBean and 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...).

I was going through book "Java Complete reference, 8th Edition" and came across BeanInfo, in chapter on "Java Beans". What relation a Java Bean has with 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?

1
Read The Tutorial by Oracle, free of cost.Basil Bourque
@BasilBourque: Thanks for your pointers. ThanksCuriousMind
The JavaBeans specification is remarkably easy to read if you want all the details.Basil Bourque
While originally meant to be "a reusable software component that can be manipulated visually in a builder tool", JavaBeans are used in ways other than in a form-building IDE tool. For example, as a convenient data container, BeanItem, in the Vaadin data model.Basil Bourque
By the way, do not confuse JavaBeans with Enterprise JavaBeans (EJB). Technically unrelated. The first is easy to understand with simple practical application. The second… well, not so much. EJB is a whole other beast whose name was given by marketers trying to leverage the initial buzz and reputation of JavaBeans in the early days of Java technology.Basil Bourque

1 Answers

3
votes

tl;dr

  • Implicit
    • The getter/setter method naming conventions implicitly identify properties when the class is considered to be a JavaBean.
  • Explicit
    • Alternatively, you can identify those properties explicitly by defining a partner class that implements the BeanInfo interface.
    • In Java 9 and later, you may use annotations to more conveniently implement 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. That never really worked out.

Instead, people commonly use the JavaBeans approach as a way of identifying properties. For example, the BeanItemContainer in Vaadin 7.

At a minimum, a JavaBean must:

You can define a JavaBean either implicitly or explicitly.

JavaBean naming conventions

The implicit way to define a JavaBean is through naming conventions. Any methods starting with get, set, or is are detected by reflection/introspection and considered to identify a property. 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. A getter without a setter makes the property read-only.

BeanInfo interface

The explicit way to define a JavaBean is to create another class alongside you intended JavaBean class. The other class implements the BeanInfo interface. Most likely the other class is actually a subclass of the SimpleBeanInfo class. That SimpleBeanInfo class implements the BeanInfo interface in a negative manner, denying info. You override the methods for the pieces of info you want to identify aspects of your JavaBean class.

You can use the BeanInfo partner class to identify the properties (instead of using the getter/setter naming convention). And you can identify other aspects of a 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.

The reflection/introspection facilities in Java automatically detect and process your BeanInfo classes to provide the meta-data about your JavaBean classes.

See Oracle Tutorial page.

BeanInfo Annotations

Java 9 may help with some aspects of a 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. 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).

See JEP 256: BeanInfo Annotations on the OpenJDK project.

JavaBeans spec

There is much more to JavaBeans than just properties like “eyeColor”, though properties are certainly the most common purpose for using JavaBeans.

I suggest studying the quite readable JavaBeans 1.01 specification. And read the Oracle Tutorial.

For a technical overview, I suggest reading this posting, The JavaBeans specification by Stephen Colebourne.

Bean Validation

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. 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. Bean Validation can be used at either client-side (Swing, JavaFX, etc.) or server-side. Vaadin, for example, supports its own technology for validation during data-entry as well as supporting you plugging in a Bean Validation implementation.

Enterprise JavaBeans

Do not confuse JavaBeans with Enterprise JavaBeans (EJB). Re-using the “JavaBean” trademark for EJB was a poor decision by the Sun marketing staff. EJB is totally unconnected and distinct from the original 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.

Records provide a compact syntax for declaring classes which are transparent holders for shallowly immutable data.