What is a JavaBean exactly?

后端 未结 19 2105
清酒与你
清酒与你 2020-11-22 01:55

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?

<
19条回答
  •  执笔经年
    2020-11-22 02:27

    A Java Bean is a component or the basic building block in the JavaBeans architecture. The JavaBeans architecture is a component architecture that benefits from reusability and interoperability of a component-based approach.

    A valid component architecture should allow programs to be assembled from software building blocks (Beans in this case), perhaps provided by different vendors and also make it possible for an architect / developer to select a component (Bean), understand its capabilities, and incorporate it into an application.

    Since classes/objects are the basic building blocks of an OOP language like Java, they are the natural contenders for being the Bean in JavaBeans architecture.

    The process of converting a plain Java class to a Java bean is actually nothing more than making it a reusable and interoperable component. This would translate into a java class having abilities like :

    1. controlling the properties, events, and methods of a class that are exposed to another application. (You can have a BeanInfo class that reports only those properties, events and methods that the external application needs)
    2. persistence (being serialisable or externizable - this would also imply having no-argument constructors, using transient for fields)
    3. ability to register for events and also to generate events (for e.g. making use of bound and constraint properties)
    4. customizers (to customise the Bean via GUIs or by providing documentation)

    In order for a Java class to be termed a Java bean it is not necessary that they need to possess all the above abilities, instead it implies to implement a subset of the above relevant to the context (for e.g. a bean in a certain framework may not need customizers, some other bean may not need bound and constrained properties etc)

    Almost all leading frameworks and libraries in Java adhere to the JavaBeans architecture implicitly, in order to reap the above benefits.

提交回复
热议问题