Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

What is @Bean Annotation in Spring Framework? Example Tutorial

Hello Java programmers, if you are wondering what is @Bean annotation in Spring Framework, what is the purpose, and how to use it then you have come to the right place. Earlier, I have shared the best free Spring core and spring MVC courses and In this tutorial, you will learn the most frequently used Spring annotations @Bean, which are used to define different types of beans. Beans are the fundamentals of Spring framework, they represent the POJO class which is created and managed by Spring Framework. Whole Spring framework is about beans and their relationships. 

Beans can be defined as XML configuration and also declared using the Java configuration. If you have used XML confirmation in past then you can think of @Bean as a replacement for bean tag in XML. 

Any method annotated with @Bean indicates that it returns a Spring bean and it is also scanned during auto-configuration process. If this is confusing to you don't worry, we will see a couple of examples of @Bean annotation to understand this fundamental spring concept in depth. 

1. What is @Bean Annotation in Spring Framework?

Definition of the @Bean varies from simple to complex to explain. The description given by the Spring Framework document is that Beans are the objects in Spring that constitute the backbone of your application and are maintained by the Spring IoC container. 

A bean is an object that a Spring IoC container instantiates, assembles, and generally manages.

2. Spring IOC Container

The process of inversion of control (IoC) is when an object declares its dependencies rather than creating them. The task of generating such dependencies is delegated to an IoC container via this object.

3. Traditional Approach

In below, there is an explanation of the traditional approach to building the beans in Spring. Here we get the Student and Grade classes as an example.

public class Student {
    private Grade grade;

    public Student(Grade grade) {
        this.grade = grade;

This Student class uses Grade class indicates the grades of each student.

public class Grade {
    private String name;
    private int currentYear;
    private int grade;

    public Grade(String name, int currentYear, int grade) { = name;
        this.currentYear = currentYear;
        this.grade = grade;

In the above example, this is okay with the small number of instances. But think about you have many cases, and one model repeatedly wants to have connections with each class. 

This is where the IOC (Inversion of Control) should come, and all you need to have provided the
appropriate configuration metadata.

4. Bean Configuration

We are going to get the example above for this using bean configuration. First, annotate the Student class with @Component annotation.

public class Student {
    //your code here

After that, configuration class Grade is used to serve the metadata to the IOC container. This is shown
is the below example class Grade.

@ComponentScan(basePackageClasses = Student.class)
public class Configuration {
    public Grade getGrade() {
        return new Grade("Grade 5", 2021, 5);

Here, the @ComponentScan is responsible for looking at the beans named Student, and the configuration class produces the bean type Grade. Because of the IoC container manages all of the objects, they are referred to as Spring beans.

Spring Bean annotation Example

5. Inversion of Control

The AnnotationConfigApplicationContext is used to build up the container, and the below results will show you that it is correctly initialized.

ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
Student student = context.getBean("student", Student.class);
assertEquals("Student", Student.getGrade().getname());

The above code segment clearly proves that the beans are created by an Ioc container. As summary,
The @Bean annotation specifies that the annotated method generates a bean that the Spring container may handle, and @Bean supports init-method, destroy-method, autowiring, lazy-init, dependency-check, depends-on, and scope. 

Spring @Bean Example? What does the @Bean annotation do? Example Tutorial

That's all about what is @Bean annotation in Spring Framework and how to use it. So in this tutorial, we discussed how to create the spring beans and their relationship to produce a managed spring bean. As I said, you can use @Bean annotation to mark a method which can return a Bean object which is created and managed by Spring framework.  Will see you in the following tutorial.

Other Java and Spring Tutorial you may like
  • 20+ Spring MVC Interview Questions for Programmers (answer)
  • Difference between @Autowired and @Inject in Spring? (answer)
  • Top 5 Frameworks Java Developer Should Know (frameworks)
  • 10 Advanced Spring Boot Courses for Java developers (courses)
  • Difference between @RequestParam and @PathVariable in Spring (answer)
  • Top 7  Courses to learn Microservices in Java (courses)
  • How Spring MVC works internally? (answer)
  • Spring Data JPA @Query Example (query example)
  • Spring Data JPA Repository (JpaReposistory example)
  • 5 Courses to learn Spring Cloud for Microservices (courses)
  • 10 Spring MVC annotations Java developer should learn (annotations)
  • @SpringBootApplication vs @EnableAutoConfiguration? (answer)
  • 15 Spring Boot Interview Questions for Java Developers (questions)
  • Top 5 Courses to Learn and Master Spring Cloud (courses)
  • 5 Courses to Learn Spring Security for Java programmers (courses)
  • Top 5 Spring Boot Annotations Java Developers should know (read)
  • Top 5 Spring Cloud annotations Java programmer should learn (cloud)
  • Difference between @Component, @Service, and @Controller in Spring (answer)

Thanks for reading this article so far. If you find this Spring @Bean Tutorial and Example then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are a beginner and want to learn the Spring MVC from scratch, and looking for some best online resources then you can also check out these best Spring MVC courses for beginners. This list contains free Udemy and Pluralsight courses to learn Spring MVC from scratch.

1 comment:

Feel free to comment, ask questions if you have any doubt.