What is a JavaBean exactly?

后端 未结 19 1526
清酒与你
清酒与你 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:34

    They are serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods. The name "Bean" was given to encompass this standard, which aims to create reusable software components for Java.according to wiki

    The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. according to spring io.

    0 讨论(0)
  • 2020-11-22 02:36

    A Java Bean is any java class that satisfies the following three criteria:

    1. It should implement serializable interface (A Marker interface).
    2. The constructor should be public and have no arguments (What other people call a "no-arg constructor").
    3. It should have getter and setters.

    Good to note the serialVersionUID field is important for maintaining object state. Below code qualifies as a bean:

    public class DataDog implements java.io.Serializable {
    
    private static final long serialVersionUID = -3774654564564563L;
    
    private int id;
    private String nameOfDog;
    
    //The constructor should NOT have arguments
    public DataDog () {}
    
    
    /** 4. getter/setter */
    
    // getter(s)
    public int getId() {
        return id;
    }
    public String getNameOfDog() {
        return nameOfDog;
    }
    // setter(s)
    public void setId(int id) {
        this.id = id;
    }
    public void setNameOfDog(String nameOfDog) {
        this.nameOfDog = nameOfDog;
    }}
    
    0 讨论(0)
  • 2020-11-22 02:40

    Java Beans are using for less code and more work approach... Java Beans are used throughout Java EE as a universal contract for runtime discovery and access. For example, JavaServer Pages (JSP) uses Java Beans as data transfer objects between pages or between servlets and JSPs. Java EE's JavaBeans Activation Framework uses Java Beans for integrating support for MIME data types into Java EE. The Java EE Management API uses JavaBeans as the foundation for the instrumentation of resources to be managed in a Java EE environment.

    About Serialization:

    In object serialization an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.

    After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.

    0 讨论(0)
  • 2020-11-22 02:41

    Regarding the second part of your question, Serialization is a persistence mechanism used to store objects as a sequence of signed bytes. Put less formally, it stores the state of an object so you can retrieve it later, by de-serialization.

    0 讨论(0)
  • 2020-11-22 02:41

    A Java Bean is a java class [conceptual] that should follow following conventions:

    1. It should have a no-arg constructor.
    2. It should be Serializable.
    3. It should provide methods to set and get the values of the properties, known as getter and setter methods.

    It is a reusable software component. It can encapsulate many object into one object so that same object can be accessed from multiples places and is a step towards easy maintenance of code.

    0 讨论(0)
  • 2020-11-22 02:41

    In practice, Beans are just objects which are handy to use. Serializing them means to be able easily to persist them (store in a form that is easily recovered).

    Typical uses of Beans in real world:

    • simple reusable objects POJO (Plain Old Java Objects)
    • visual objects
    • Spring uses Beans for objects to handle (for instance, User object that needs to be serialized in session)
    • EJB (Enterprise Java Beans), more complex objects, like JSF Beans (JSF is old quite outdated technology) or JSP Beans

    So in fact, Beans are just a convention / standard to expect something from a Java object that it would behave (serialization) and give some ways to change it (setters for properties) in a certain way.

    How to use them, is just your invention, but most common cases I enlisted above.

    0 讨论(0)
提交回复
热议问题