Spring Boot and Beans ! š
Spring boot is one of the most widely used frameworks for backend development. I recently joined a project which has Spring Boot as the tech stack and I had never used the framework in my life ! So I started learning about it over the internet but was not able to grasp many things. I got to know how to set up a spring boot project, how to create a RESTful API and how to create services and repositories, etc. etc. But still I was confused about how it is actually doing all that magic of auto-wiring. So I started to explore the framework more and tried understanding one of the basic concept in Spring that is Beans. And here is what I understoodā¦
To understand Beans, we should first know about the Inversion of Control principle used in Spring.
In normal programming paradigm, we use the new keyword to create an object. This creates dependency between the two classes.
In this example, the Maths class directly depends on the Addition class and
we have no control on the Addition class while creating a Maths class.
In Inversion of Control, we do something like this :
In this case, we are receiving the addition object of type Addition class through a setter method. This removes the dependency between Maths class and Addition class.
Now we have control over which Addition class implementation to use while instantiating the Maths class.
This practice is also referred as dependency injection. And auto-wiring in Spring is mostly based on the same principle.
So What is a Bean Basically ?
Bean is a predefined, preconfigured and pre-instantiated object of a certain class.
In layman language,
Bean is an object that we can inject in any class and we need not worry about its instantiation or its dependencies as that is taken care by the framework itself.
The beans in Spring are stored in an IoC (Inversion of Control) container which is often referred as Application Context.
How do we create a Bean ?
There are many ways by which we can create a Bean. Using @Component annotation is one of those.
Letās use beans and auto-wiring in the example above.
Letās create a bean of Addition class.
Thatās it ! Now, a bean of class Addition will be created and stored in Application Context.
As a bean of Addition class is present in the application context, we will now be able to inject that bean directly using @Autowired annotation in any class.
As we can see, we have injected the object of type Addition directly using @Autowired annotation, as a Bean of type Addition is present in the application context.
And we are getting an already instantiated object so we wonāt need to worry about its instantiation.
But, now we will have to make the Maths class a Bean as well otherwise the auto-wiring logic wonāt work.
Also, if we notice, we are using the IoC or DI (Dependency Injection) principle here as we are injecting the required dependency from outside instead of instantiating the same in the class itself.
@Autowired annotation takes care of searching through the application context and finding the Bean of required class automatically.
Now if you read āSo What is a Bean Basically ?ā again, it will make more sense.
We can also verify that our beans are getting stored in the application context using getBean method of ApplicationContext.
And this will just work fine!
Hereās a github link to the same code, feel free to clone it and start experimenting on your own to clear the doubts that you may have about beans in Spring : https://github.com/yogeshnikam671/springioc