Published: 2026-06-01 โ€ข Updated: 2026-07-05

Annotation-Based Configuration in Spring

Interview Preparation Hub for Backend and Cloud-Native Engineering Roles

1. Introduction

Traditionally, Spring applications were configured using XML files. While powerful, XML configuration was verbose and hard to maintain. With the advent of annotation-based configuration, developers can configure beans, dependencies, and application context directly in code, making applications cleaner, more readable, and easier to maintain.

2. Core Annotations

Annotation Description Example
@Component Marks a class as a Spring-managed bean.
@Component
public class UserService { }
@Autowired Injects dependencies automatically.
@Autowired
private UserRepository repo;
@Configuration Marks a class as a source of bean definitions.
@Configuration
public class AppConfig { }
@Bean Defines a bean inside a @Configuration class.
@Bean
public DataSource dataSource() { ... }
@Service Specialized @Component for service layer.
@Service
public class PaymentService { }
@Repository Specialized @Component for DAO layer.
@Repository
public class UserRepository { }
@Controller Marks a class as a Spring MVC controller.
@Controller
public class UserController { }
@RestController Combines @Controller and @ResponseBody for REST APIs.
@RestController
public class ApiController { }

3. Realistic Example: E-Commerce Application

Consider an e-commerce system with services and repositories:

@Service
public class OrderService {
  @Autowired
  private PaymentService paymentService;

  public void placeOrder(Order order) {
    paymentService.process(order);
  }
}

@Repository
public class OrderRepository {
  // Database operations
}

@Configuration
public class AppConfig {
  @Bean
  public DataSource dataSource() {
    return new HikariDataSource();
  }
}
    

Here, annotations replace XML configuration, making the system modular and readable.

4. Diagram: Annotation-Based Configuration Flow

Diagram: Spring Annotation Flow

@Component / @Service / @Repository โ†’ Bean Discovery โ†’ IoC Container โ†’ @Autowired Injection โ†’ Application Ready

5. Advantages

  • Reduced boilerplate compared to XML.
  • Improved readability and maintainability.
  • Type safety and IDE support.
  • Encourages modular design.
  • Supports modern Spring Boot conventions.

6. Best Practices

  • Use constructor injection
  • Organize annotations by layer (@Service, @Repository, @Controller).
  • Combine @Configuration and @Bean for external resources.
  • Use @Qualifier when multiple beans of the same type exist.
  • Leverage Spring Bootโ€™s auto-configuration to reduce manual setup.

7. Common Mistakes

  • Overusing field injection โ†’ harder testing.
  • Mixing XML and annotation configs inconsistently.
  • Not using @Qualifier when multiple beans exist.
  • Ignoring lifecycle annotations like @PostConstruct.

8. Interview Notes

  • Be ready to explain the difference between XML and annotation-based configuration.
  • Discuss @Autowired injection types (constructor, setter, field).
  • Explain stereotype annotations (@Service, @Repository, @Controller).
  • Know how @Configuration and @Bean work together.
  • Understand how Spring Boot leverages annotations for auto-configuration.

9. Summary

Annotation-based configuration in Spring simplifies application development by reducing XML verbosity and enabling cleaner, type-safe code. It leverages annotations like @Component, @Autowired, @Configuration, and @Bean to manage beans and dependencies. For interviews, focus on core annotations, realistic examples, advantages, and best practices. Mastery of annotation-based configuration demonstrates readiness for backend engineering and modern Spring Boot development roles.

About the Author

Naresh Kumar

Naresh Kumar

Senior Java Backend Engineer experienced in Banking, Payments, ISO 20022, Spring Boot, Microservices, Kafka, Docker, Kubernetes, AWS and Cloud Native Systems.

Built enterprise payment solutions, transaction processing systems, API platforms and scalable microservices used in production.

LinkedIn Profile