[Spring] What is a Bean object?
Apr. 23, 2021One of the key concepts in the Spring framework is the Bean object. However, it can be difficult to get a hang of it and use it in an effective way. This post will try to explain what a Spring Bean is.
Definition
Here is how Bean is defined by the Spring Framework:
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 String IoC container.
There are two important pieces of information here. Firstly, a Bean is an object and secondly, it is instantiated, assembled, and managed by a Spring IoC container.
As you can see, another term, IoC, is introduced and I’m afraid that I will have to introduce another term to you. But we will go through these terms, and you will have a better understanding.
Dependency Injection
There are many definitions of Dependency Injection out there, but to me, they are too verbose and focus too much on the bell and whistle. Therefore, I would like to give you this really short definition of Dependency Injection by James Shore:
Dependency Injection means giving an object its instance variables. Really. That’s it.
Inside a class, we may have variables that are instances of another class. This creates a dependency. For example:
public class Person {
private Address address;
// constructor
}
public class Address {
private String street;
private int number;
public Address(String street, int number) {
this.street = street;
this.number = number;
}
// getters & setters
}
As you can see, the dependency of a Person object is an Address object. So when a Person object is instantiated, it requires an Address to be instantiated as well. The problem arise when the Address object is created by the Person object in its constructor:
public class Person {
private Address address;
public Person(String street, int number) {
this.address = new Address(String, number);
}
}
As you can see, we have to pass the arguments for creating an Address object to the constructor of the Person object. This can becomes complex when there are many dependencies.
Hence, the concept of Dependency Injection comes about. Instead of creating the dependencies by itself, the class Person lets the dependency to be injected. The Address object is created outside, and then the reference is passed to the Person object.
public class Person {
private Address address;
public Person(Address address) {
this.address = address;
}
}
This helps to isolate classes from one another. And this is not the only way to do Dependency Injection. There are variations as well.
Inversion of Control
Inversion of Control takes it up another notch. It concerns about the instantiation and management of dependency objects, in this case, how the Address object is created.
Address address = new Address("Jump Street", 21);
Person me = new Person(address);
As mentioned above, the example only has one dependency. However, in reality, a class usually has many dependencies. Imagine creating and managing all those dependency objects. And sometimes you want to share a single instance of a class, and sometimes multiple separate objects for each use case.
The IoC container will have all the configuration metadata of the classes, and will instantiated the object and pass it to the one that needed the object on demand.
So we use the @Bean annotation to indicate the configuration metadata of a class, and all the objects constructed by IoC Container are called Spring Bean.