Let me (JBS) provide you a simplest look at what a bean is. A JavaBean is a 100% Java component that works on any Java Virtual Machine.
The minimum requirements that make a component a JavaBean are as follows:
- It must support the JDK 1.1 and later Serialization model.
- It must use get/set accessors to expose its properties.
There is nothing magical about creating a JavaBean. You just create a Java class that implements the java.io.Serializable interface and uses public get/set methods to expose its properties.
The following listing provide you a simple JavaBean:
import java.io.Serializable;
public class SimpleJavaBean implements java.io.Serializable
{
private String simpleProperty = new String("");
public SimpleJavaBean() { }
public String getSimpleProperty()
{
return simpleProperty;
}
public void setSimpleProperty(String value)
{
simpleProperty = value;
}
}
This class is now a JavaBean. It satisfies the minimum requirements. You can now load the SimpleJavaBean into any JavaBeans–aware program that uses introspection and change its properties. Its state can then be saved and reloaded anytime, because of its support for serialization.
Let's take a look at an example that illustrates how to serialize our new bean. The second example on the next listing creates an instance of our SimpleJavaBean, sets the simpleProperty to "simple property value", serializes the bean to a file, reads the bean back in, and finally displays proof that its state was maintained.
import java.io.*;
public class SimpleJavaBeanTester
{
public SimpleJavaBeanTester() { }
public void storeBean(SimpleJavaBean value)
{
try
{
// Create the ObjectOutputStream passing it the
// FileOutputStream object that points to our
// persistent storage.
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("file.dat"));
// Write the SimpleJavaBean to the ObjectOutputStream
os.writeObject(value);
os.flush();
os.close();
}
catch (IOException ioe)
{
System.err.println(ioe.getMessage());
}
}
public SimpleJavaBean getBean()
{
SimpleJavaBean value = null;
try
{
// Create the ObjectInputStream passing it the FileInputStream object that points to our persistent storage.
ObjectInputStream is = new ObjectInputStream(new FileInputStream("file.dat"));
// Read the stored object and downcast it back to a SimpleJavaBean
value = (SimpleJavaBean)is.readObject();
is.close();
}
catch (IOException ioe)
{
System.err.println(ioe.getMessage());
}
catch (ClassNotFoundException cnfe)
{
System.err.println(cnfe.getMessage());
}
return value;
}
public void testBean()
{
// Create the Bean
SimpleJavaBean simpleBean = new SimpleJavaBean();
// Use accessor to set property
simpleBean.setSimpleProperty("simple property value");
// Serialize the Bean to a Persistent Store
storeBean(simpleBean);
// Get the Bean from the Persistent Store
SimpleJavaBean newBean = getBean();
System.out.println("The newBean's simpleProperty == " +newBean.getSimpleProperty());
}
public static void main(String[] args)
{
SimpleJavaBeanTester simpleJavaBeanTester = new SimpleJavaBeanTester();
simpleJavaBeanTester.testBean();
try
{
System.out.println("Press enter to continue...");
System.in.read();
}
catch (IOException ioe)
{
System.err.println(ioe.getMessage());
}
}
}
No comments:
Post a Comment