Spring @Bean Example? What does the @Bean annotation do in Spring Framework?
1. What is @Bean Annotation in Spring Framework?
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 {
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) {
this.name = 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.
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.
@Configuration
is the below example class Grade.
@Configuration
@ComponentScan(basePackageClasses = Student.class)
public class Configuration {
@Bean 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.
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.
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.
So in this tutorial, we discussed how to create the spring beans and their relationship to produce a managed spring bean. Will see you in the following tutorial.
Comments
Post a Comment