Spring Boot Interview Questions

25 Questions
Spring Boot

Spring Boot

Java

Java

BackendWeb Development

Question 10

What is the purpose of the @Configuration annotation?

Answer:

The @Configuration annotation in Spring is used to define configuration classes that can provide bean definitions to the Spring application context. It is part of Spring's Java-based configuration support, which allows developers to configure the application using Java classes rather than XML files. Classes annotated with @Configuration are recognized by the Spring container as sources of bean definitions.

Purpose and Key Features of @Configuration

  1. Define Beans:

    • The primary purpose of @Configuration is to define bean methods annotated with @Bean. These methods are used to instantiate, configure, and initialize beans that are managed by the Spring container.
    • Beans defined in a @Configuration class are singletons by default, meaning that the same instance of the bean will be returned for every request.
  2. Component Scanning:

    • @Configuration classes can be automatically detected through classpath scanning if component scanning is enabled (e.g., using @ComponentScan or @SpringBootApplication).
  3. Equivalent to XML Configuration:

    • @Configuration classes serve as a direct replacement for XML configuration files. They provide a more type-safe and refactor-friendly way to configure Spring applications.
  4. Modular Configuration:

    • Configuration classes can be divided into multiple smaller configuration classes for better modularity and organization. This helps manage complex configurations more easily.
  5. Proxying and Enhancements:

    • Spring automatically enhances @Configuration classes by subclassing them at runtime using CGLIB proxies. This allows Spring to manage the lifecycle of the beans and ensure that method invocations are properly intercepted to return singleton instances.

Example Usage

Here is an example demonstrating how to use the @Configuration annotation to define beans in a Spring application.

Step 1: Create a Configuration Class

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }

    @Bean
    public MyRepository myRepository() {
        return new MyRepositoryImpl();
    }
}

In this example:

  • The AppConfig class is annotated with @Configuration, indicating that it is a source of bean definitions.
  • The myService and myRepository methods are annotated with @Bean, meaning that they will be managed by the Spring container as beans.

Step 2: Use the Configuration in the Application

You can now use these beans in other parts of your application by autowiring them.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {

    private final MyService myService;

    @Autowired
    public MyComponent(MyService myService) {
        this.myService = myService;
    }

    public void performTask() {
        myService.execute();
    }
}

Modular Configuration Example

You can split your configuration into multiple classes for better organization.

Step 1: Define Modular Configuration Classes

@Configuration
public class ServiceConfig {

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}

@Configuration
public class RepositoryConfig {

    @Bean
    public MyRepository myRepository() {
        return new MyRepositoryImpl();
    }
}

Step 2: Import Configuration Classes

You can import these configuration classes into a main configuration class if needed.

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({ServiceConfig.class, RepositoryConfig.class})
public class AppConfig {
    // Centralized configuration class
}

Using Configuration in a Spring Boot Application

In a Spring Boot application, you typically use @SpringBootApplication, which combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.

Example

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Summary

  • Define Beans: @Configuration classes are used to define beans with @Bean methods.
  • Component Scanning: These classes can be detected through classpath scanning.
  • XML Replacement: They serve as a type-safe and refactor-friendly alternative to XML configuration.
  • Modular Configuration: Configuration can be split into multiple classes for better organization.
  • Proxying and Enhancements: Spring enhances @Configuration classes to manage bean lifecycles and ensure singleton instances.

The @Configuration annotation provides a powerful and flexible way to configure Spring applications using Java, promoting better type safety, readability, and maintainability.

Recent job openings