← Back to Questions
Spring Boot

What is @Configuration annotation in Spring?

Learn What is @Configuration annotation in Spring? with simple explanations, real-time examples, interview tips and practical use cases.

What is @Configuration Annotation in Spring?

The @Configuration annotation in Spring is used to define a Java class as a source of Spring Bean configurations.

It tells the Spring IoC (Inversion of Control) container that the class contains bean definitions and configuration metadata.

In simple terms:

  • @Configuration marks a class as a configuration class
  • Spring scans the class during startup
  • Methods annotated with @Bean create Spring Beans
  • Spring manages these beans automatically

The @Configuration annotation is widely used in:

  • Spring Framework
  • Spring Boot
  • Microservices Architecture
  • Enterprise Applications
  • Cloud-Native Systems

Simple Definition

The @Configuration annotation is used to declare a Java class as a Spring configuration class containing bean definitions.


Why @Configuration Annotation is Important

Enterprise applications require:

  • Centralized configuration management
  • Bean creation control
  • Dependency management
  • Scalable application architecture

The @Configuration annotation helps organize and manage application configuration efficiently.


Real-Time Banking Example

Consider a banking application requiring:

  • Payment Service
  • Notification Service
  • Database Configuration
  • Security Configuration
  • Email Configuration

Instead of XML configuration files, developers can create Java configuration classes using:

@Configuration
    

Spring automatically processes these configurations during application startup.


How @Configuration Works Internally

Application Starts
      |
Spring Scans Classes
      |
@Configuration Class Detected
      |
@Bean Methods Processed
      |
Beans Created
      |
Dependencies Injected
      |
Application Ready
    

Basic @Configuration Example

@Configuration
public class AppConfig {

}
    

Here:

  • Spring recognizes AppConfig as a configuration class
  • Bean definitions can be added inside the class

Using @Bean Inside @Configuration

The @Configuration annotation is commonly used together with:

@Bean
    

Example

@Configuration
public class AppConfig {

    @Bean
    public NotificationService notificationService() {

        return new NotificationService();
    }

}
    

Spring:

  • Executes the method
  • Creates NotificationService object
  • Registers it as a Spring Bean

Complete Banking Example

public class NotificationService {

    public void sendNotification() {

        System.out.println("Notification Sent");
    }

}
    
public class PaymentService {

    private NotificationService notificationService;

    public PaymentService(
        NotificationService notificationService) {

        this.notificationService =
            notificationService;
    }

}
    
@Configuration
public class BankingConfig {

    @Bean
    public NotificationService notificationService() {

        return new NotificationService();
    }

    @Bean
    public PaymentService paymentService() {

        return new PaymentService(
            notificationService()
        );
    }

}
    

How Spring Manages @Configuration Classes

Spring internally creates a proxy object for configuration classes.

This ensures:

  • Singleton bean behavior
  • Proper dependency management
  • Bean lifecycle handling

Singleton Bean Management Example

@Configuration
public class AppConfig {

    @Bean
    public NotificationService notificationService() {

        return new NotificationService();
    }

    @Bean
    public PaymentService paymentService() {

        return new PaymentService(
            notificationService()
        );
    }

}
    

Spring ensures:

  • Only one NotificationService object exists
  • Same bean instance is reused

@Configuration vs @Component

Feature @Configuration @Component
Purpose Configuration Class General Component
Contains @Bean Methods Yes Possible but Not Recommended
Proxy Support Yes No
Usage Application Configuration Business Components

Why Proxy Support is Important

Spring creates proxy objects for configuration classes to:

  • Prevent duplicate bean creation
  • Maintain singleton behavior
  • Handle inter-bean method calls correctly

Using @ComponentScan with @Configuration

Spring allows component scanning inside configuration classes.

Example

@Configuration
@ComponentScan(basePackages = "com.bank")
public class AppConfig {

}
    

Spring automatically scans:

  • @Component
  • @Service
  • @Repository
  • @Controller

Using @PropertySource

External properties files can be loaded using:

@PropertySource
    

Example

@Configuration
@PropertySource(
"classpath:application.properties"
)
public class AppConfig {

}
    

Using @Import

Additional configuration classes can be imported.

Example

@Configuration
@Import(SecurityConfig.class)
public class AppConfig {

}
    

Advantages of @Configuration Annotation

  • Centralized configuration management
  • Better maintainability
  • Type-safe configuration
  • Flexible bean management
  • Excellent IDE support

1. Centralized Configuration

Application configurations remain organized in dedicated classes.


2. Better Maintainability

Developers can manage configurations easily using Java code.


3. Type Safety

Java compiler validates configurations during compilation.


4. Better IDE Support

IDEs provide:

  • Auto completion
  • Error highlighting
  • Refactoring support

Disadvantages of @Configuration Annotation

  • Requires Java coding knowledge
  • Complex configurations may become large
  • Improper bean management can create issues

@Configuration in Spring Boot

Spring Boot heavily relies on:

@Configuration
    

Many Spring Boot auto-configuration classes internally use:

  • @Configuration
  • @Bean
  • @Conditional annotations

@SpringBootApplication Relationship

The @SpringBootApplication annotation internally contains:

@Configuration
    

Example

@SpringBootApplication
public class BankingApplication {

    public static void main(String[] args) {

        SpringApplication.run(
            BankingApplication.class,
            args
        );
    }

}
    

@Configuration in Microservices

Modern microservices architectures heavily use configuration classes for:

  • API Gateway Configuration
  • Database Configuration
  • Security Configuration
  • Cloud Integrations
  • REST Client Setup

Microservices Banking Example

@Configuration
public class SecurityConfig {

    @Bean
    public JwtFilter jwtFilter() {

        return new JwtFilter();
    }

}
    

Spring manages JWT security configuration automatically.


Common Spring Annotations Related to @Configuration

  • @Configuration
  • @Bean
  • @ComponentScan
  • @PropertySource
  • @Import
  • @Autowired

Real-World Enterprise Use Cases

  • Banking systems
  • Security configuration
  • Cloud-native systems
  • Microservices architecture
  • REST API configuration

Common Interview Questions

What is @Configuration annotation in Spring?

The @Configuration annotation marks a Java class as a Spring configuration class containing bean definitions.

Why is @Configuration used with @Bean?

@Configuration allows Spring to process @Bean methods and manage beans properly inside the IoC container.

What is the difference between @Configuration and @Component?

@Configuration is specifically designed for application configuration and supports proxy-based bean management, while @Component is a general-purpose component annotation.


Professional Interview Answer

The @Configuration annotation in Spring is used to declare a Java class as a source of bean definitions and application configuration metadata. Spring IoC container processes classes annotated with @Configuration, executes @Bean methods, creates Spring-managed objects, and handles dependency injection automatically. The @Configuration annotation is a core part of Java-Based Configuration and is heavily used in Spring Boot and microservices architectures for centralized and maintainable application configuration.


Conclusion

The @Configuration annotation is one of the most important annotations in Spring Framework and Spring Boot.

It enables developers to manage application configuration using clean, maintainable, and type-safe Java code instead of XML files.

Modern enterprise applications, cloud-native systems, banking applications, and microservices architectures rely heavily on @Configuration for scalable and centralized configuration management.

Understanding the @Configuration annotation is essential for Java developers, Spring Boot developers, backend engineers, and software architects preparing for interviews and building enterprise-grade applications.

Why this Spring Boot question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.