POJO vs Java Bean vs Spring Bean

POJO vs Java Bean vs Spring Bean

·

3 min read

In Java, a Plain Old Java Object (POJO) is a Java object that does not have any special structure or behavior. It is simply a Java object with instance variables and getter/setter methods.

Here is an example of a Plain Old Java Object (POJO) in Java:

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

  public Person() {
  }

  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String getName() {
    return name;
  }

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

  public int getAge() {
    return age;
  }

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

A Java Bean is a POJO that follows certain conventions, such as having a default constructor, implementing the Serializable interface, and having getter and setter methods for its instance variables. Java Beans are often used in Java application frameworks, such as JavaServer Faces (JSF), to provide a standard way of representing data and logic in a reusable and consistent way.

Here is an example of a Java Bean in Java:

Copy code
public class Person implements Serializable {
  private String name;
  private int age;

  public Person() {
  }

  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String getName() {
    return name;
  }

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

  public int getAge() {
    return age;
  }

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

A Spring Bean is a Java object that is managed by the Spring Framework. In the Spring Framework, a bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. The Spring Framework provides a range of features for dependency injection, transaction management, and other services that can be applied to a bean.

Here is an example of a Spring Bean in Java:

Copy code
@Component
public class Person {
  private String name;
  private int age;

  @Autowired
  private Configuration config;

  public Person() {
  }

  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String getName() {
    return name;
  }

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

  public int getAge() {
    return age;
  }

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

This Person class is a Spring Bean because it is annotated with the @Component annotation, which tells the Spring Framework to manage it as a bean. It also has a dependency on a Configuration object, which is annotated with the @Autowired annotation, indicating that it should be injected by the Spring Framework.

Spring Beans are managed by the Spring Framework and can be injected into other objects using dependency injection. They can be easily accessed and manipulated using the Spring Framework APIs, and can benefit from the various features and services offered by the Spring Framework, such as dependency injection, transaction management, and others.

So, in summary, a POJO is a basic Java object, a Java Bean is a POJO that follows certain conventions, and a Spring Bean is a Java object that is managed by the Spring Framework.

This article was originally posted on devcorner.blog