What is a Spring Bean?

In this post of Spring, We will try to give you answer to one of the common question “What is a Spring Bean?

 

Introduction

Spring Bean is the key concept or backbone of the Spring Framework. Spring Bean is the object whose life-cycle managed by the Spring IoC. It is important to understand it before we work with the Spring Framework. In simple words Spring Bean is the core building block for any Spring application. My goal is to give a clear answer to a basic question “What is a Spring Bean?“. Let’s try to get an answer to this simple question.

 

1. Spring Bean Definition

This is the standard definition of Spring Bean from Spring documentation:

[pullquote align=”normal”]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 one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.[/pullquote]

This provides a complete definition of the Spring Bean, lets cover some important point in this definition to get a clear answer to our question.

 

2. Spring IOC Containers

The most important concept while working on the Spring beans is the Ioc Container. Spring IoC container is the central management system of the Spring Framework. It is responsible to create, configure and manage the life cycle of these objects. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata provided in the form of either XML configurations or annotations.It allows you to express the objects that compose your application and the rich interdependencies between such objects. Let’s look at the high level workflow of the Spring Ioc container.

Spring IoC container

For a better understanding, let’s take the following example:

  1. Customer for our online shop.
  2. Orders placed by Customers on our online shop.

A typical domain classes will look like:

public class Customer{
    private String firstName;
    private String lastName;
    private List orders;

    //getter and setter
}

public class Order{
    private String code;
    private double total;
    private double tax;
    private Product product;
    private Customer owner;

    //getter and setter
}

If we are working on an application with no support for the Spring Framework, we might do the following to create an instance of both Customer and Order class:

Customer customer = new Customer(); // pass any constructor arguments
customer.setFirstName("FirstName");
// fill other data in customer mmodel

// Create order instance by getting all orders for a customer
List orders = get_All_Orders_For_Customer(Customer);
customer.setOrders(orders);

This code will work fine with no issue, however there are few points which can make this code complex and error prone.

  1. Developer need to create each instance and fill out dependencies (orders in our case).
  2. For a large application with a hundred of classes, this is difficult and error prone also if we need to change in any class, we need to make sure that all dependencies are working correctly.
  3. We also need to take care of the sequence 

To handle all these cross dependencies in a large application, Spring Ioc container prove its power and flexibility. As stated above, we need to pass the class information and its dependencies to Spring container using as metadata and Spring Ioc container will manage the bean life cycle for us.

  1. It will also take care of any dependencies while instating the bean (if bean B needs bean A, it will first create bean A)

 

3. Bean Configuration

Spring provide the following options for bean configuration:

  1. Bean configuration using Java configurations.
  2. XML declaration

 

3.1 Java Configuration

@Component
public class Customer{
    private String firstName;
    private String lastName;
    private List orders;
  
    //getter and setter
}

@Component
public class Order{
    private String code;
    private double total;
    private double tax;
    private Product product;
    private Customer owner;

    //getter and setter
}

Here is our configuration class:

@Configuration
@ComponentScan(basePackages = { "com.javadevjournal"})
public class AppConfig{
   
    @Bean
    public Customer getCustomer() {
        return new Customer();
    }
}

 

4. Bean Dependencies

One of the main feature of Spring managed beans are the dependency management. When Spring creates a bean which define dependency to another bean, the Spring Ioc container will create that bean first.It will make sure that all dependencies are in place before it create the bean. This dependency graph creation and making sure that the beans are getting created in right order is one of the most powerful feature of the Spring Ioc container and it also take away the complexity from our code. Below, I summarize the bean life cycle under Spring IoC.

Spring BeanPostProcessor

For more details, read our article Spring BeanPostProcessor. If you are starting with Spring Framework basic, I will suggest going through the following topics for more details.

  1. How @Autowired works in Spring?
  2. Spring Bean Scopes

 

Summary

In this post, we tried to give an answer to a basic question “What is a Spring Bean?”. We discuss few important points about Spring beans and how the life cycle of the Spring beans managed by IoC container. We briefly talk about the IoC benefits and flexibility.

1 thought on “What is a Spring Bean?”

Comments are closed.

Scroll to Top