Essential Spring Annotations Overview
Fast recall
Quick revision
Core answer
Breadth of Spring annotation knowledge across configuration, dependency injection, and lifecycle management
Knowing web annotations but not configuration or lifecycle annotations, or treating all stereotypes as identical
Organize annotations by category and explain the purpose of each group with concrete use cases
Interview-ready guide
Interview answer
@Configuration and @Bean explicitly define objects. @Service and @Repository identify application and persistence components discovered by scanning. A class with one constructor normally needs no @Autowired; @Qualifier or @Primary resolves multiple candidates. @RestController and @GetMapping declare an MVC HTTP handler. @Transactional is commonly applied through a proxy, and @PostConstruct runs after dependency population. The useful answer explains the category and boundary rather than reciting one long list.
Essential Spring Annotations Overview
Direct Answer
Important Spring annotations are easiest to learn by job: bean definitions (@Configuration, @Bean), component roles (@Component, @Service, @Repository, @Controller), injection (@Autowired, @Qualifier, @Value), web mappings (@RestController, @GetMapping), transactions (@Transactional), and lifecycle callbacks (@PostConstruct, @PreDestroy). Each annotation is processed only when the relevant Spring infrastructure manages the class.
Code Example
@Configuration
public class AppConfig {
@Bean // Configuration: explicit bean definition
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
@Service // Stereotype: business logic component
public class OrderService {
private final OrderRepository repository;
private final int maxRetries;
@Autowired // Injection: constructor injection (preferred)
public OrderService(OrderRepository repository,
@Value("${order.max-retries:3}") int maxRetries) {
this.repository = repository;
this.maxRetries = maxRetries;
}
@PostConstruct // Lifecycle: runs after dependency injection
public void init() {
System.out.println("OrderService initialized with " + maxRetries + " retries");
}
}
@RestController // Web: combines @Controller + @ResponseBody
public class OrderController {
@GetMapping("/orders/{id}") // Web: maps HTTP GET to method
public Order getOrder(@PathVariable Long id) {
return orderService.findById(id);
}
}
Common Mistakes & Pitfalls
- Using @Component for everything: Freshers often use @Component for all classes instead of specific stereotypes like @Service or @Repository. This works but loses semantic meaning — @Repository adds persistence exception translation, @Service marks business logic, and @Controller handles web requests. Use the right stereotype for clarity and framework benefits.
- Forgetting @ComponentScan scope: Annotations only work if Spring scans the package. If your @Service is in com.example.external but your main class is in com.example.app, Spring won't find it. Always ensure components are in the same package or subpackages as your @SpringBootApplication class.
- Mixing field and constructor injection: Using @Autowired on fields is tempting but makes testing harder. Constructor injection (implicit @Autowired on the only constructor) is preferred — it makes dependencies explicit and enables immutability with final fields.
Check your understanding
Answer these without looking back at the article.
- What does @Transactional do?
- What is @Value vs @ConfigurationProperties?
- What is @Async in Spring?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.