What does the @Configuration annotation do in Spring Boot?

What does the @Configuration annotation do in Spring Boot?

A) It marks a class as a Spring component
B) It marks a class as a source of bean definitions
C) It marks a class as a Spring controller
D) It marks a class as a RESTful service

Answer:

B) It marks a class as a source of bean definitions

Explanation:

The @Configuration annotation in Spring Boot is used to mark a class as a source of bean definitions. This annotation indicates that the class contains one or more @Bean methods, which Spring will process to generate and manage the beans defined in the class. These beans are then registered in the Spring application context, making them available for dependency injection throughout the application.

The @Configuration annotation is typically used in conjunction with @Bean methods to define beans programmatically:


@Configuration
public class AppConfig {

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

In this example, the AppConfig class is marked as a configuration class, and the myService() method is a bean definition. Spring Boot will invoke this method to create an instance of MyServiceImpl and register it as a bean in the application context.

The @Configuration annotation plays a crucial role in Spring’s Java-based configuration, providing a way to define beans and configure the application without relying on XML configuration files. This approach promotes type safety, refactorability, and improved readability of the application configuration.

Reference links:

Spring Boot Tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top