What are Spring Boot Beans

AGS
4 min readFeb 29, 2024

New to Spring Boot ? lets Understand!

What are Spring Boot Beans ?

As per documentation, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.

Keep the above definition aside for a while!

Consider the below classes

public class BlogService {
private final BlogRepository blogRepository;

public BlogService(BlogRepository blogRepository) {
this.blogRepository = blogRepository;
}

// Method to retrieve a blog post by its ID
public BlogPost getBlogPostById(int id) {
return blogRepository.findById(id);
}

// Method to save a new blog post
public void saveBlogPost(BlogPost blogPost) {
blogRepository.save(blogPost);
}
}

Note: BlogRepository is a class which is used for CRUD operations on Blog Post, and BlogPost is a Entity class of Blog with id, tittle, author etc..

So traditional way of setting the value of blogRepository would be creating an object of BlogRepository and passing it to the BlogService constructor while creating the object of BlogService.

We will be using a BlogController class to demostrate this use case. BlogController is the entry point for this program.

public class BlogController{

public void saveBlog(BlogPost blogPost){
BlogRepository blogRepository = new BlogRepository();
BlogService blogService = new BlogService(blogRepository);
blogService.saveBlogPost(blogPost)
}

}

So these objects are created and managed by us (Technically Humans).

Now what if I say, spring Boot can create and manage blogRepository object for you, Yes that is called a bean.

There are multiple ways to tell a Spring Boot Application to create and manage objects using annotations, below are commonly used annotations

  1. @Component
  2. @Service
  3. @Repository
  4. @Controller
  5. @RestController
  6. @Configuration
  7. @Autowired
  8. @Value
  9. @Bean

Definition of each annotation

What is IOC

This is another abbreviation for which you might get multiple definitions, but it is simply, Inversion of Control.

As the name suggest, it is just inversion of Control, instead of you i.e. your code creating and managing the objects, you are externalizing that to a Framework which is Spring Boot, which creates and manages the objects aka beans. Yes it is as simple as that :)

okay, it might sound like I am randomly switching to different topics, but spare for a few minutes.

Dependency Injection

Dependency injection refers to the process of providing the dependencies of a class from an external source, rather than the class creating its dependencies internally. In Spring boot you can perform DI in the below ways.

  1. Constructor Injection
  2. Field Injection
  3. Method Injection

Hope you will google them again :)

Now coming back to title of our blog, The BEANS

Does it make sense to say, beans are objects created and maintained by Spring boot, and Dependency Injection is used to inject the values the objects created by Spring Boot directly into other classes rather than initializing the objects in code.

So the above code with Spring boot Can be refactored into the below types of injection Where BlogRepository’s bean has been created either by @Repository annotation.

  1. Constructor Injection: In constructor injection, dependencies are provided via a class constructor. Spring Boot automatically resolves the dependencies and injects them when an instance of the class is created. Here’s an example:
@Service
public class BlogService {

private final BlogRepository blogRepository;

// SpringConstructor injection
@Autowired
public BlogService(BlogRepository blogRepository) {
this.blogRepository = blogRepository;
}

public BlogPost getBlogPostById(int id) {
return blogRepository.findById(id);
}

public void saveBlogPost(BlogPost blogPost) {
blogRepository.save(blogPost);
}
}

2. Field Injection: In field injection, dependencies are injected directly into class fields using annotations like @Autowired. However, field injection is generally discouraged due to potential issues with testability and encapsulation.

@Service
public class BlogService {
@Autowired
private final BlogRepository blogRepository;

public BlogPost getBlogPostById(int id) {
return blogRepository.findById(id);
}

public void saveBlogPost(BlogPost blogPost) {
blogRepository.save(blogPost);
}
}

3. Method Injection: In method injection, dependencies are injected via setter methods. However, this approach is less common in Spring Boot compared to constructor injection.

@Service
public class BlogService {

private final BlogRepository blogRepository;

@Autowired
public setBlogService(BlogRepository blogRepository) {
this.blogRepository = blogRepository;
}

public BlogPost getBlogPostById(int id) {
return blogRepository.findById(id);
}

public void saveBlogPost(BlogPost blogPost) {
blogRepository.save(blogPost);
}
}

And the final Controller would look like using Constructor injection to inject service:

@RestController
public class BlogController{

private BlogService blogService;

@Autowired
public BlogController(BlogService blogService){
this.blogService = blogService;
}

public void saveBlog(BlogPost blogPost){
blogService.saveBlogPost(blogPost)
}
}

So the Objects of BlogRepository, BlogService and BlogController are maintained by Spring boot since they are annotated with @Repository, @Service and @RestContoller respectively and objects of these classes can be called as bean which are maintained by Inversion of control containers.

Conclusion

Beans are Singleton (mostly) objects of instantiated classes created and managed by Spring Boot, read to be used (injected) in other classes of the applications.

References

  1. Dependency Injection: https://medium.com/@vipulkumar1397/dependency-injection-52a6891e622e
  2. Singleton Classes: https://www.baeldung.com/spring-boot-singleton-vs-beans
  3. IOC: https://medium.com/@ankur-s20/inversion-of-control-ioc-dependency-injection-di-dependency-inversion-e694d38db136

--

--

AGS

Dev Dominus | Cyber security | Software Engineer