JavaBean and relation with BeanInfo, are they related?
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?
tl;dr
BeanInfo
interface. 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.
BeanInfo Annotations
Java 9 brings a new and quite handy way to identify properties and other aspects of a JavaBean: annotations. Rather than go to the trouble of a building a separate partner class, you can simply add some annotation tags to your JavaBean class' source code. You can identify properties this way as well as identify other BeanInfo aspects.
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.
链接地址: http://www.djcxy.com/p/47658.html