top of page
image 82.png

Beans and Dependency Injection:

In Spring Boot, we can use Spring Framework to define our beans and dependency injection. Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure so that we can focus on our application.

Annotations make it easier to configure the dependency injection in Spring. Instead of using XML Configurations, we can use Spring Bean annotations on classes and methods to define Beans. Then, the Spring IOC container configures and manages the beans:

@ComponentScan - While developing an application, we need to tell the Spring framework to look for Spring-managed components.

@ComponentScan enables Spring to scan for things like configurations, controllers, services, and other components we define.

Additionally, the Configuration classes can contain @Bean annotations, which register the methods as beans in the Spring application context. After that, the @ComponentScan annotation can auto-detect such beans. Furthermore, the @ComponentScan annotation can also scan, detect, and register beans for classes annotated with @Component, @Controller, @Service, and @Repository.

In our application, since we have followed the Spring Boot typical layout, there is no need to specify any arguments for @ComponentScan annotation. All component class files are automatically registered with Spring Beans.

The following example provides an idea about Auto wiring the ModelMapper object and creating a Bean for the same –


The following code shows the code for autowired ModelMapper object in the RestController component –

image 65.png
bottom of page