How does Spring Boot manage bean creation and dependency injection?

Category : Interview Questions | Sub Category : Spring Boot Interview Questions | By Prasad Bonam Last updated: 2023-08-05 00:24:44 Viewed : 313


How does Spring Boot manage bean creation and dependency injection?

Spring Boot leverages the core capabilities of the Spring Framework for managing bean creation and dependency injection. It follows the Inversion of Control (IoC) and Dependency Injection (DI) principles, allowing you to focus on writing business logic while Spring Boot takes care of the bean creation and wiring.

Here is how Spring Boot manages bean creation and dependency injection:

  1. Component Scanning: Spring Boot automatically scans the classpath for components, such as classes annotated with @Component, @Service, @Repository, and @Controller, to create and manage beans. This process is known as component scanning.

  2. Auto-Configuration: Spring Boot provides a feature called auto-configuration, which automatically configures beans based on the classpath and dependencies present in the project. By including specific starters (e.g., spring-boot-starter-web), Spring Boot will automatically set up the necessary beans and configurations required for that feature to work.

  3. Conditional Bean Creation: Spring Boot uses conditional bean creation, where beans are created based on certain conditions or property values. Conditional annotations like @ConditionalOnProperty, @ConditionalOnClass, and @ConditionalOnBean are used to control when beans should be created.

  4. Dependency Injection (DI): Spring Boot injects dependencies into beans using constructor injection, setter injection, or field injection, depending on the configuration and annotations used.

    • Constructor Injection:

      java
      @Service public class MyService { private final MyRepository myRepository; public MyService(MyRepository myRepository) { this.myRepository = myRepository; } }
    • Setter Injection:

      java
      @Service public class MyService { private MyRepository myRepository; @Autowired public void setMyRepository(MyRepository myRepository) { this.myRepository = myRepository; } }
    • Field Injection:

      java
      @Service public class MyService { @Autowired private MyRepository myRepository; }
  5. @Autowired Annotation: The @Autowired annotation is used to signal Spring Boot to automatically wire dependencies into the beans. It can be used on constructors, methods, or fields.

  6. Spring Boot Application Context: Spring Boot creates and maintains an application context, which serves as the container for managing beans and their dependencies. The application context handles the lifecycle of the beans, including creation, initialization, and destruction.

  7. Bean Lifecycle Management: Spring Boot provides hooks for managing the lifecycle of beans. You can use annotations like @PostConstruct and @PreDestroy to execute methods after bean creation and before bean destruction, respectively.

    Example:

    java
    @Service public class MyService { @PostConstruct public void init() { // Code to execute after bean creation } @PreDestroy public void cleanUp() { // Code to execute before bean destruction } }

By following these principles and mechanisms, Spring Boot simplifies the bean management and dependency injection process. It automatically creates and wires beans based on the applications configuration and annotations, enabling developers to build robust and modular applications with minimal boilerplate code.

Search
Related Articles

Leave a Comment: