I often encounter these conceptual problems, such as JavaBean, POJO, EntityBean, in Java development. Some concepts may easy confusion. So, I spent some time to figure out these concepts in Java Development.

Although, some concepts are not used in actual development, and the understanding may not be accurate enough, just as a note for reference.

What is POJO?

POJO is a simple, common Java object. It contains business logic processing or persistence logic, but it is not JavaBean, EntityBean, etc. It does not have any special role, does not extend or implement any other Java classes or interfaces. POJO can contain setter and getter methods similar to JavaBean properties and access to properties.

For example, a POJO should NOT:

  • Extend the predetermined class, such as public class Foo extends javax.servlet.http.HttpServlet {…
  • Implement a predetermined interface, such as public class Bar implements javax.ejb.EntityBean {…
  • Contains predetermined annotations, such as @ javax.ejb.Entity public class Baz {…

Another example, when building a database mapping object in a web application, we can only call it a POJO.

package com.demo.spring;
 
public class DbHello { //a simple and common Java object, not extend or implement any other Java classes or interfaces.
 
     private DictionaryDAO dao;
 
     public void setDao(DictionaryDAO dao) {
 
            this.dao = dao;
 
     }
 
}

What is JavaBean?

In Java, there are many class definitions that conform to this specification:

  • Several private instance fields.
  • By the public method to get fields of instance.

For example:

public class Person {
     private String name;
     private int age;

     public String getName() { return this.name; }
     public void setName(String name) { this.name = name; }

     public int getAge() { return this.age; }
     public void setAge(int age) { this.age = age; }
}

If the read and write methods conform to the following naming convention:

// read method
public Type getXyz()
// write method
public void setXyz(Type value)

Then this class is a JavaBean.

The syntax for setter methods:

  1. It should be public in nature.
  2. The return type should be void.
  3. The setter method should be prefixed with the set.
  4. It should take some argument i.e. it should not be a no-arg method.

The syntax for getter methods:

  1. It should be public in nature.
  2. The return type should not be void i.e. according to our requirement we have to give return type.
  3. The getter method should be prefixed with get.
  4. It should not take any argument.

In summary

  • JavaBean must have a public parameterless constructor
  • All fields(property) are private
  • The private property must be exposed to other programs through public type methods (getters and setters), and the method naming must also follow a certain naming convention.
  • Should be serializable. For example, you can implement the Serializable interface for bean persistence.

What is the role of JavaBean?

In Java EE development, JavaBeans are commonly used to encapsulate data. A set of data encapsulates into a JavaBean for easy transmission.

What is Bean?

Bean are reusable Java components. A component is a group composed of one or more categories that can perform internal management on their own, and the outside world does not understand its internal information and operation mode. Objects using it can only be manipulated through interfaces.

Bean does not need to extend a special base class or implement a specific interface. Bean writing specifications enable the Bean’s container to analyze a Java class file and translate its methods into properties. That is to use the Java class as a bean class.

Bean writing specifications include Bean class constructor methods, defining attributes and access method writing rules.

Java Bean is a Java-based component model consisting of three parts: properties, methods, and events. In this model, JavaBeans can be modified or combined with other components to generate new components or complete programs. It is a Java class that is encapsulated into an object that has a certain function or handles a certain business.

Therefore, you can also access beans and their properties through Java code embedded in JSP pages.