Sitemap
Iran Macedo

Iran Macedo

A writer to himself. A curious, challenger lover Software Developer at Avenue Code trying to improve my abilities and available to share knowledge

Java Bean vs Spring Bean

3 min readFeb 9, 2024
Press enter or click to view image in full size
A green set of beans on a table
Photo by Rachael Gorjestani on Unsplash

A good way to dive into this introduction is to have in mind the concept of POJO — let's assume you don't know what it is and introduce it here so you can compare and assimilate the sutil differences between the terms we will discuss. Please, bear with me for a second.

POJOs are ordinary Java Objects that do not require a framework to use, nor need to be run in an application server environment. A POJO may or may not have naming conventions for properties and methods. For example:

public class SuperHeroPojo { 
public String firstName;
public String power;

public EmployeePojo (String firstName, String power) {
this.firstName = firstName;
this.power = power;
}
public String name() {
return this.firstName;
}
public String getPower() { return this.power; }
}

By having the freedom to avoid following conventions, it is difficult for frameworks and other applications to use them. This section of Baeldung's article shows that Reflection has no effect on a POJO class. Some conventions were introduced by JavaBean many years ago.

What is a Bean according to JavaBean?

In the early days of Java's ecosystem development, it was born the concept of JavaBean — primarily for use in visual GUI but also useful for regular applications. According to the official documentation created by SunMicrosystems in 1997:

The goal of the JavaBeans APIs is to define a software component model for Java, so that third party ISVs can create and ship Java components that can be composed together into applications by end users.

A JavaBean is still a POJO but introduces a strict set of rules around how we implement it.

In summary, a JavaBean is a standard that aims in creating and shipping Java components (classes) into different applications. Beans are important pieces in modern libraries until today commonly used to store, access, and modify application states.

Get Iran Macedo’s stories in your inbox

Join Medium for free to get updates from this writer.

The three most important features of a Java Bean are:

  1. All properties are private (use getters/setters)
  2. A public no-argument constructor
  3. Implements Serializable.

That’s it. It’s just a convention. Lots of libraries depend on it though — such as Spring as we gonna dive into here.

An example of SuperHeroPojo as a JavaBean

public class SuperHeroPojo implements Serializable { 

private static final long serialVersionUID = -3760445487636086034L;
public String firstName;
public String power;

public SuperHeroPojo(){}

public SuperHeroPojo (String firstName, String power) {
this.firstName = firstName;
this.power = power;
}

public String getFirstName() {
return this.firstName;
}

public String setFirstName(String firstName) {
this.firstName = firstName;
}

// other setters and getters

Looking at the official Spring Documentation we can understand how Spring container utilizes Beans to implement the Inversion of Control (IoC) or Dependency Injection (DI). Quoting the docs:

It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method.

Spring Container, therefore, can be similar to a factory where it will create the beans and injects the dependencies according to the user's configuration.

In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.

Wait. So beans are conceptually different from Spring and JavaBean?

Yes. And this can be a tricky question. But before diving into Spring's beans scopes let's summarize those concepts.

Quoting here a great Shaun Abram's article

POJO's are ordinary Java Objects that do not require a framework to use, nor need to be run in a application server environment.

Can Spring beans be POJOs? Yes, and they usually are (although they don’t have to be — e.g. Spring can be used with ‘heavyweight’ Java objects, such as EJBs).

Can Spring beans be JavaBeans? As I have said, yes and again they often are but don’t have to be.

None of the 3 terms discussed are mutually exclusive. A Java object can be a JavaBean, a POJO and a Spring bean all at the same time.

--

--

Iran Macedo
Iran Macedo

Written by Iran Macedo

A writer to himself. A curious, challenger lover Software Developer at Avenue Code trying to improve my abilities and available to share knowledge