What is Spring Bean

Chakresh Tiwari
ShoutLoudz
Published in
3 min readDec 7, 2022

--

Spring beans
Photo by Patrick Fore on Unsplash

Introduction

Spring bean is basically the objects which are managed by spring IoC container. These beans form the backbone of any spring application. Their lifecycle is managed by an IoC container. So this brings us to a new point what is IoC?

Inversion of Control (IoC)

Inversion of control basically meant, instead of developers creating the object by themselves, they just define where they need which object and spring will automatically provide those objects. meaning giving control to someone else that is spring.
Or you can say object defines its dependencies without creating them and spring will create them.

Code Sample

We can check this by an example:

class Employee {
public String name;
public Address address;

Employee(String name, Address address) {
this.name = name;
this.address = address;
}
//getter and setters
}
class Address {
int houseNo;
String city;
String street;

Address(String city, String street) {
this.city = city;
this.street = street;
}
//getters and setters
}

Now if want to construct an object of Employee we need the Address class first

Address a1 = new Address("kanpur", "abcd");
Employee e1 = new Employee(("shubh", a1);.

Here we have created the object of Address by ourselves and passed that into the Employee constructor. Nothing wrong with this approach but this makes code tightly coupled.

Need of IoC

And if there are more number of dependent classes then providing all objects every time is going to be very difficult. So instead of that, we can just define which all objects a class needs and we can pass them with the help of metadata which will store in our IoC Container.

Now, these changes we can do with the help of annotations:

Firstly we need to mark the Employee class as a Component. Then we can create Bean for dependent classes.

@Component
class Employee {

}

now create Bean of Address class, Bean we can either create in the class which has the main method because it will have SpringBootApplication annotation also. or we can create a new package called configuration and inside that, we can have classes related to all configs.

@Configuration
class BeanConfig {

@Bean
public Address getAddress() {
return new Address("kanpur", "abcd");
}

}

So now when the application starts at that time it will create a bean for the Address class. and by using Autowired annotation that can be used in other classes.

Conclusion

So all the objects which are managed by the IoC container are called Beans.
Apart from that, we can create a bean for any dependency which we want to use in our application, like any third-party class or library, we can just create a bean of that and then auto-wire it in the class where we want to use it and call its methods directly.

Example Code

Add this to the class which has the main method, it will create a bean-of-rest template.

@Bean
public RestTemplate getResttemplate() {
return new RestTemplate();
}
@Autowired
private RestTemplate restTemplate;

restTemplate.getForEntity("https://www.google.com", String.class);

Above I have used the rest template and called its method directly.

In the next post, we will check other important interview topics related to Spring boot.

Thanks for reading!!

Ref:

https://stackoverflow.com/questions/44475523/how-to-understand-bean-in-spring

--

--

Chakresh Tiwari
ShoutLoudz

Software Engineer at Cisco , Sharing my knowledge and experience related to work. I am here to help learners to prepare for tech interviews.