Persistence
Persistence is the ability to save the current state of a Bean, including the values of a Bean's properties and instance variables, to nonvolatile storage and to retrieve them at a later time.
Methods
A Bean should use accessor methods to encapsulate the properties. A Bean can provide other methods for business logic not related to the access to the properties.
- The properties, events, and methods of a bean can be exposed to another application.
- A bean may register to receive events from other objects and can generate events that are sent to those other objects.
- A class with a zero-argument constructor is subject to being instantiated in an invalid state. If such a class is instantiated manually by a developer (rather than automatically by some kind of framework), the developer might not realize that the class has been improperly instantiated. The compiler cannot detect such a problem, and even if it is documented, there is no guarantee that the developer will see the documentation.
- JavaBeans are inherently mutable and so lack the advantages offered by immutable objects.
- Having to create getters for every property and setters for many, most, or all of them can lead to an immense quantity of boilerplate code.
The JavaBeans functionality is provided by a set of classes and interfaces in the java.beans package.
| Interface | Description |
| java.beans.Customizer | This interface allows the designer to provide a graphical user interface through which a bean may be configured. |
| java.beans.PropertyChangeListener | A method in this interface is invoked when a bound property is changed. |
| java.beans.Visibility | Methods in this interface allow a bean to execute in environments where the GUI is not available. |
In order to function as a JavaBean class, an object class must obey certain conventions about method naming, construction, and behaviour. These conventions make it possible to have tools that can use, reuse, replace, and connect Java Beans.
The required conventions are as follows:
- The class must have a public default constructor (with no arguments). This allows easy instantiation within editing and activation frameworks.
- The class properties must be accessible using get, set, is (can be used for Boolean properties instead of get), to and other methods (so-called accessor methods and mutator methods) according to a standard naming convention. This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties. Setters can have one or more arguments.
- The class should be serializable. (This allows applications and frameworks to reliably save, store, and restore the bean's state in a manner independent of the VM and of the platform.)
list;\n\n // Property \"name\", readable/writable.\n private String name = null;\n\n // No-arg constructor (takes no arguments).\n public PersonBean() {\n\n }\n\n public List<String> getList() {\n return list;\n }\n\t\n public void setList(List<String> list) {\n this.list = list;\n }\n\n /**\n * Getter for property \"name\".\n */\n public String getName() {\n return name;\n }\n\n /**\n * Setter for property \"name\".\n *\n * @param value\n */\n public void setName(String value) {\n this.name = value;\n }\n\n /**\n * Getter for property \"deceased\"\n * Different syntax for a boolean field (is vs get)\n */\n public boolean isDeceased() {\n return deceased;\n }\n\n /**\n * Setter for property \"deceased\".\n * @param value\n */\n public void setDeceased(boolean value) {\n deceased = value;\n }\n}\n"}}'>packageorg.wikipedia.players;
importjava.io.Serializable;
importjava.util.List;
publicclass PersonBeanimplementsSerializable{
// Properties
privatebooleandeceased=false;
privateList<String>list;
// Property "name", readable/writable.
privateStringname=null;
// No-arg constructor (takes no arguments).
publicPersonBean(){
}
publicList<String>getList(){
returnlist;
}
publicvoidsetList(List<String>list){
this.list=list;
}
/**
* Getter for property "name".
*/
publicStringgetName(){
returnname;
}
/**
* Setter for property "name".
*
* @param value
*/
publicvoidsetName(Stringvalue){
this.name=value;
}
/**
* Getter for property "deceased"
* Different syntax for a boolean field (is vs get)
*/
publicbooleanisDeceased(){
returndeceased;
}
/**
* Setter for property "deceased".
* @param value
*/
publicvoidsetDeceased(booleanvalue){
deceased=value;
}
}
TestPersonBean.java:
());\n\n // Output: \"Bob is [alive]\"\n System.out.printf(\"%s is %s%n\", person.getName(), person.isDeceased() ? \" [deceased]\" : \" [alive]\");\n }\n}\n"}}'>packageorg.wikipedia.players;
importjava.util.ArrayList;
importorg.wikipedia.players.PersonBean;
/**
* Class "TestPersonBean".
*/
publicclass TestPersonBean{
/**
* Tester method "main" for class "PersonBean".
*
* @param arguments
*/
publicstaticvoidmain(String[]args){
PersonBeanperson=newPersonBean();
person.setName("Bob");
person.setDeceased(false);
person.setList(newArrayList<>());
// Output: "Bob is [alive]"
System.out.printf("%s is %s%n",person.getName(),person.isDeceased()?" [deceased]":" [alive]");
}
}
\n<jsp:setProperty name=\"person\" property=\"*\"/>\n\n<html>\n <body>\n Name: <jsp:getProperty name=\"person\" property=\"name\"/><br/>\n Deceased? <jsp:getProperty name=\"person\" property=\"deceased\"/><br/>\n <br/>\n <form name=\"beanTest\" method=\"POST\" action=\"testPersonBean.jsp\">\n Enter a name: <input type=\"text\" name=\"name\" size=\"50\"><br/>\n Choose an option:\n <select name=\"deceased\">\n <option value=\"false\">Alive</option>\n <option value=\"true\">Dead</option>\n </select>\n <input type=\"submit\" value=\"Test the Bean\">\n </form>\n </body>\n</html>\n"}}'><jsp:useBeanid="person"class="org.wikipedia.players.PersonBean"scope="page"/>
<jsp:setPropertyname="person"property="*"/>
<html>
<body>
Name:<jsp:getPropertyname="person"property="name"/><br/>
Deceased?<jsp:getPropertyname="person"property="deceased"/><br/>
<br/>
<formname="beanTest"method="POST"action="testPersonBean.jsp">
Enteraname:<inputtype="text"name="name"size="50"><br/>
Chooseanoption:
<selectname="deceased">
<optionvalue="false">Alive</option>
<optionvalue="true">Dead</option>
</select>
<inputtype="submit"value="Test the Bean">
</form>
</body>
</html>