Package java.beans
Use the BeanInfo interface to create a BeanInfo class and provide explicit information about the methods, properties, events, and other features of your beans.
This interface is intended to be implemented by, or delegated from, instances of java.beans.beancontext.BeanContext, in order to propagate to its nested hierarchy of java.beans.beancontext.BeanContextChild instances, the current «designTime» property.
A PropertyEditor class provides support for GUIs that want to allow users to edit a property value of a given type.
A BeanDescriptor provides global information about a «bean», including its Java class, its displayName, etc.
The DefaultPersistenceDelegate is a concrete implementation of the abstract PersistenceDelegate class and is the delegate used by default for classes about which no information is available.
An Encoder is a class which can be used to create files or streams that encode the state of a collection of JavaBeans in terms of their public APIs.
The EventHandler class provides support for dynamically generating event listeners whose methods execute a simple statement involving an incoming event object and a target object.
An Expression object represents a primitive expression in which a single method is applied to a target and a set of arguments to return a result — as in «a.getFoo()» .
The FeatureDescriptor class is the common baseclass for PropertyDescriptor, EventSetDescriptor, and MethodDescriptor, etc.
An «IndexedPropertyChange» event gets delivered whenever a component that conforms to the JavaBeans™ specification (a «bean») changes a bound indexed property.
An IndexedPropertyDescriptor describes a property that acts like an array and has an indexed read and/or indexed write method to access specific elements of the array.
The Introspector class provides a standard way for tools to learn about the properties, events, and methods supported by a target Java Bean.
A MethodDescriptor describes a particular method that a Java Bean supports for external access from other components.
The ParameterDescriptor class allows bean implementors to provide additional information on each of their parameters, beyond the low level type information provided by the java.lang.reflect.Method class.
The PersistenceDelegate class takes the responsibility for expressing the state of an instance of a given class in terms of the methods in the class’s public API.
A class which extends the EventListenerProxy specifically for adding a PropertyChangeListener with a «bound» property.
A Statement object represents a primitive statement in which a single method is applied to a target and a set of arguments — as in «a.setFoo(b)» .
A class which extends the EventListenerProxy specifically for adding a VetoableChangeListener with a «constrained» property.
The XMLDecoder class is used to read XML documents created using the XMLEncoder and is used just like the ObjectInputStream .
The XMLEncoder class is a complementary alternative to the ObjectOutputStream and can used to generate a textual representation of a JavaBean in the same way that the ObjectOutputStream can be used to create binary representation of Serializable objects.
A PropertyVetoException is thrown when a proposed change to a property represents an unacceptable value.
An annotation on a constructor that shows how the parameters of that constructor correspond to the constructed object’s getter methods.
Indicates that an attribute called «transient» should be declared with the given value when the Introspector constructs a PropertyDescriptor or EventSetDescriptor classes associated with the annotated code element.
Package java.beans Description
Contains classes related to developing beans — components based on the JavaBeans™ architecture. A few of the classes are used by beans while they run in an application. For example, the event classes are used by beans that fire property and vetoable change events (see PropertyChangeEvent ). However, most of the classes in this package are meant to be used by a bean editor (that is, a development environment for customizing and putting together beans to create an application). In particular, these classes help the bean editor create a user interface that the user can use to customize the bean. For example, a bean may contain a property of a special type that a bean editor may not know how to handle. By using the PropertyEditor interface, a bean developer can provide an editor for this special type.
To minimize the resources used by a bean, the classes used by bean editors are loaded only when the bean is being edited. They are not needed while the bean is running in an application and therefore not loaded. This information is kept in what’s called a bean-info (see BeanInfo ).
Unless explicitly stated, null values or empty Strings are not valid parameters for the methods in this package. You may expect to see exceptions if these parameters are used.
Long-Term Persistence
As of v1.4, the java.beans package provides support for long-term persistence — reading and writing a bean as a textual representation of its property values. The property values are treated as beans, and are recursively read or written to capture their publicly available state. This approach is suitable for long-term storage because it relies only on public API, rather than the likely-to-change private implementation.
Note: The persistence scheme cannot automatically instantiate custom inner classes, such as you might use for event handlers. By using the EventHandler class instead of inner classes for custom event handlers, you can avoid this problem.
You read and write beans in XML format using the XMLDecoder and XMLEncoder classes, respectively. One notable feature of the persistence scheme is that reading in a bean requires no special knowledge of the bean.
Writing out a bean, on the other hand, sometimes requires special knowledge of the bean’s type. If the bean’s state can be expressed using only the no-argument constructor and public getter and setter methods for properties, no special knowledge is required. Otherwise, the bean requires a custom persistence delegate — an object that is in charge of writing out beans of a particular type. All classes provided in the JDK that descend from java.awt.Component , as well as all their properties, automatically have persistence delegates.
If you need (or choose) to provide a persistence delegate for a bean, you can do so either by using a DefaultPersistenceDelegate instance or by creating your own subclass of PersistenceDelegate . If the only reason a bean needs a persistence delegate is because you want to invoke the bean’s constructor with property values as arguments, you can create the bean’s persistence delegate with the one-argument DefaultPersistenceDelegate constructor. Otherwise, you need to implement your own persistence delegate, for which you’re likely to need the following classes: PersistenceDelegate The abstract class from which all persistence delegates descend. Your subclass should use its knowledge of the bean’s type to provide whatever Statement s and Expression s are necessary to create the bean and restore its state. Statement Represents the invocation of a single method on an object. Includes a set of arguments to the method. Expression A subclass of Statement used for methods that return a value.
Once you create a persistence delegate, you register it using the setPersistenceDelegate method of XMLEncoder .
Related Documentation
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
What is a Java Bean Class?
It is a Java class that is developed with some standards. The standards are:
• Must be public and should not be a final or abstract class.
• Can implement Serializable(I) if needed.
• Must have a direct or indirect 0-param constructor.
• Member variables are called bean properties and they must be taken as private and non-static.
• Every bean property should have setter and getter methods(public).
Example:
//Person class represents Java Bean class class Person < //private id property private Integer id; //private id property private String name; //private mobileNo property private Integer mobileNo; //0-param constructor public PersonBean() <>//getter method of the id property public int getId() < return id; >//setter method of the id property public void setId(int id) < this.id = id; >//getter method of the name property public String getName() < return name; >//setter method of the name property public void setName(String name) < this.name = name; >//getter method of the mobileNo property public Integer getMobileNo() < return mobileNo; >//setter method of the mobileNo property public void setMobileNo(Integer mobileNo) < this.mobileNo = mobileNo; >>
→ In the above example we created a Bean class with the name “Person”. Properties of this class id, name and mobileNo and modifiers of these properties are private. This java class contains a 0-param constructor. This class also contains a setter and getter method of each property for sending and retrieving data from one layer to another layer. This is an example of the Java Bean class that uses every property of the Java Bean in this Person class to make a Java Bean class.
→ In real time we will use Java Bean as a Model class. When sending data from one layer to another (like View tier to Service tier etc.).
→ Java Bean class helps to use readability and reusability features in the code.
→ Java Bean class is the POJO class which follows some standards.
→ Java Bean class is following security that’s why all properties must be private.
→ Java Bean class is very helpful for developers because they develop real-time projects. Suppose in these projects Model classes are having a lot of properties that to manage with the help of their constructors to initialize when instantiating them. It is very difficult. The Java Bean class solve this problem with its getter and setter methods for sending and retrieving data from one class to another because all setter and getter are public modifiers used.
POJO class vs Java Bean class
POJO class:
• The Pojo class doesn’t have any restrictions to follow some standards.
• The Pojo class doesn’t have much control over the members.
• The Pojo class members are visible for other classes.
• The Pojo class has properties that can be accessible.
• The Pojo class may contain parameterised constructor.
Java Bean Class:
• The Java Bean class has certain restrictions to follow some standards.
• The Java Bean class has much control over the members.
• The Java Bean class members are not visible for other classes.
• The Java Bean class has properties that can be accessible by their getter methods.
• The Java Bean class contains the 0-param constructor.
Conclusion:
This topic is explained What is a Java Bean class? What are the rules to make a Java Bean class?