What is a “Java Bean”? [duplicate]

2019-01-13 01:52发布

问题:

This question already has an answer here:

  • What is a JavaBean exactly? 14 answers

The name really throws me off. I'm hoping someone can explain it in a way I won't forget :)

回答1:

Any serializable java class (implementing java.io.Serializable) that follows specific conventions: a no-argument constructor, and properties accessible via get/set/is accessors.

The idea is to make it predictable, so that properties etc can be discovered automatically through reflection - of great help in tool and framework development.



回答2:

http://en.wikipedia.org/wiki/JavaBean

JavaBeans are reusable software components for Java that can be manipulated visually in a builder tool. Practically, they are classes written in the Java programming language conforming to a particular convention. They are used to encapsulate many objects into a single object (the bean), so that they can be passed around as a single bean object instead of as multiple individual objects. A JavaBean is a Java Object that is serializable, has a nullary constructor, and allows access to properties using getter and setter methods.

continue reading »

alt text http://www.javalobby.org/articles/j2me-in-a-nutshell/CoffeeBeanSingle.jpg



回答3:

Sun's JavaBean Tutorial says...

The JavaBeans™ architecture is based on a component model which enables developers to >create software units called components. Components are self-contained, reusable software units that can be visually assembled into composite components, applets, applications, and servlets using visual application builder tools. JavaBean components are known as beans.

A set of APIs describes a component model for a particular language. The JavaBeans API specificationdescribes the core detailed elaboration for the JavaBeans component architecture.

Beans are dynamic in that they can be changed or customized. Through the design mode of a builder tool you can use the Properties window of the bean to customize the bean and then save (persist) your beans using visual manipulation. You can select a bean from the toolbox, drop it into a form, modify its appearance and behavior, define its interaction with other beans, and combine it and other beans into an applet, application, or a new bean.

If you've used Swing's 'button', then you've used a component (visible JavaBean). You can use developers tools (like NetbeansIDE) to change the Bean's available 'properties'. Netbeans uses something called 'introspection' to discover which JavaBean properties can be modified by the coder/user (e.g. name, text-title and alignment for a Swing Button JavaBean component). You can save its state too (the IDE/Beans developer might use 'serialization' to do this) allowing re-use with your favourite settings another time.

JavaBeans don't need to be visible (like a swing component). You could create your own JavaBean to encrypt text in a textbox when someone clicks an 'OK' button on a form. You don't see your custom written JavaBean, but some other developer could re-use your 'encryption' JavaBean in their code with some 'property' changes that you allowed to be public (i.e. encryption-type="blowfish").

Regards, SteJav



回答4:

JavaBeans are reusable software component written in java.The components can be configured and connected using builder tools.Three key properties that causes any class in java to become a javabean is

1.Class is serializable
2.class has a 0 argument constructor
3.class has getter and setter methods for data members

Here is a simple class that is eligible for becoming a javabean

import java.io.*;

public class Student implements Serializable {

    private String name = null;

    //0 argument constructor
    public Student() {
    }
   //getter method
   public String getName() {
        return name;
   }
   //settor method
   public void setName(final String name) {
       this.name = value;
   }

}


回答5:

A java bean is a class that is serializable, has a no-argument constructor, and uses getters and setter methods for its member fields. Its used in Java Enterprise Apps to store business logic data.



回答6:

A JavaBean is a Java object that satisfies below programming conventions:

  1. The JavaBean class must implement Serializable Interface
  2. The JavaBean class must have a no-arg constructor which should be public
  3. All JavaBean properties must have public Setter and Getter methods to set and get all bean properties.
  4. All JavaBean instance variables should be private and only accessible by Getter and setter.