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 |
| @Autowired | Injects dependencies automatically. | @Autowired |
| @Configuration | Marks a class as a source of bean definitions. | @Configuration |
| @Bean | Defines a bean inside a @Configuration class. | @Bean |
| @Service | Specialized @Component for service layer. | @Service |
| @Repository | Specialized @Component for DAO layer. | @Repository |
| @Controller | Marks a class as a Spring MVC controller. | @Controller |
| @RestController | Combines @Controller and @ResponseBody for REST APIs. | @RestController |
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
@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.