Master Spring Boot Annotations: From @RequestMapping to @Conditional
This comprehensive guide explains the most common Spring Boot annotations—including request mapping, bean definitions, dependency injection, scopes, and auto‑configuration—providing clear examples and usage tips for Java backend developers.
1. Spring Web MVC and Spring Bean Annotations
Spring Web MVC Annotations
@RequestMapping maps web requests to handler methods. Spring MVC and Spring WebFlux use RequestMappingHandlerMapping and RequestMappingHandlerAdapter to support it. The annotation has six attributes: value , method , params , header , consume , produce .
Note: Controllers must be annotated with @Controller or @RestController before using @RequestMapping .
Examples of @RequestMapping usage:
@RequestMapping can also be placed on a class, concatenating its value with method mappings.
@RequestBody
Used on method parameters to bind the request body to an object via HttpMessageConverter . Can be combined with @Valid for validation.
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @PatchMapping
Shortcut annotations for specific HTTP methods, equivalent to @RequestMapping(method = RequestMethod.GET) etc.
@ControllerAdvice
Extension of @Component for global exception handling, used together with @ExceptionHandler , @InitBinder , @ModelAttribute .
@ResponseBody
Writes the return value directly to the HTTP response. When used on a class annotated with @RestController , it is implicit.
@PathVariable
Binds method parameters to URI template variables defined in @RequestMapping . Supports value or name attributes and optional required = false .
@RequestParam
Binds method parameters to request query parameters, with optional default values.
@Controller, @RestController
@Controller marks a Spring MVC controller; @RestController combines @Controller and @ResponseBody .
@ModelAttribute
Exposes a method’s return value as a model attribute; also can be used on parameters to bind existing model data.
@CrossOrigin
Enables CORS support for a controller or method.
@InitBinder
Customizes data binding for web requests, e.g., date formatting.
2. Spring Bean Annotations
@ComponentScan configures packages to scan for components.
@Component marks a generic bean.
@Service specialization for service‑layer beans.
@Repository specialization for DAO beans.
3. Spring Dependency Injection and Bean Scopes
DI Annotations
@DependsOn forces initialization order.
@Bean declares a bean method; attributes initMethod and destroyMethod specify lifecycle callbacks.
Scope Annotations
@Scope defines bean scope: singleton , prototype , request , session , globalSession , etc. Example for prototype scope:
<code>@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)</code>Singleton scope is the default; a bean annotated with @Scope("singleton") is instantiated only once.
4. Container Configuration Annotations
@Autowired
Injects dependencies; can be applied to constructors, setters, or fields.
@Primary
Designates a bean as the default when multiple candidates of the same type exist.
@PostConstruct and @PreDestroy
Lifecycle callbacks from JSR‑250, executed before bean initialization and before destruction.
@Qualifier
Disambiguates injection when multiple beans of the same type exist.
5. Spring Boot Annotations
@SpringBootApplication combines @Configuration , @EnableAutoConfiguration , and @ComponentScan . Example main class:
<code>@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}</code>Conditional annotations such as @ConditionalOnClass , @ConditionalOnBean , @ConditionalOnMissingBean , @ConditionalOnProperty , @ConditionalOnResource , @ConditionalOnWebApplication , and @ConditionalExpression enable fine‑grained auto‑configuration based on classpath, bean presence, property values, or custom expressions.
Summary
The article reviews the most commonly used Spring Boot annotations, covering request mapping, bean definition, dependency injection, scopes, and auto‑configuration, providing a comprehensive reference for developers.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.