Friday, December 2, 2011

POJO and JavaBean

Pojo stands for Plain Old Java Objects. "POJO" is mainly used to denote a Java object which does not follow any of the major Java object models, conventions, or frameworks. The term continues the pattern of older terms for technologies that do not use fancy new features. The equivalent to POJO on the .NET framework is Plain Old CLR Object

Pojo contains the business logic for extensions.

An example of POJO:


package springapp.domain;

import java.io.Serializable;

public class Product implements Serializable {

    private String description;
    private Double price;
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public Double getPrice() {
        return price;
    }
    
    public void setPrice(Double price) {
        this.price = price;
    }
    
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("Description: " + description + ";");
        buffer.append("Price: " + price);
        return buffer.toString();
    }
}
 
JavaBean 

A JavaBean is a POJO that is serializable, has a no-argument constructor, and 
allows access to properties using getter and setter methods that follow a simple 
naming convention. Because of this convention, simple declarative references can 
be made to the properties of arbitraryJavaBeans. 
Code using such a declarative reference does not have to know anything about the 
type of the bean, and the bean can be used with many frameworks without these 
frameworks having to know the exact type of the bean.
 
e.g:
 
public class MyBean implements java.io.serializable {
 
    private String someProperty;
 
    public String getSomeProperty() {
         return someProperty;
    }
 
    public void setSomeProperty(String someProperty) {
        this.someProperty = someProperty;
    }
} 

Advantages of JavaBean

  • A Bean obtains all of the benefits of Java's "write once, run anywhere" paradigm.
  • The properties, events, and methods of a Bean that are exposed to another application can be controlled.
  • Auxiliary software can be provided to help configure a Bean.
  • The configuration settings of a Bean can be saved in a persistent storage and can be restored at a later time.
  • A Bean may register to receive events from other objects and can generate events that are sent to other objects.
Java Bean Conventions:
 
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 JavaBeans.
The required conventions are as follows:
  • The class must have a public default constructor (no-argument). This allows easy instantiation within editing and activation frameworks.
  • The class properties must be accessible using get, set, is (used for boolean properties instead of get) and other methods (so-called accessor methods and mutator methods), following 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 must receive only one argument.
  • The class should be serializable. It allows applications and frameworks to reliably save, store, and restore the bean's state in a fashion independent of the VM and of the platform.
 

No comments:

Post a Comment