Master Spring Annotations: From @RequestMapping to @Conditional – A Complete Backend Guide
This comprehensive guide explains the most common Spring MVC and Spring Boot annotations—including @RequestMapping, @GetMapping, @Autowired, @Component, @Scope, and conditional annotations—detailing their purposes, configuration attributes, and practical code examples to help Java backend developers write cleaner, more maintainable code.
Spring Web MVC Annotations
@RequestMapping maps HTTP requests to handler methods. It supports six attributes: value (URL or alias), method (HTTP method), params (parameter filtering), header (header filtering), consume (request media types), and produce (response media types). The class must also be annotated with @Controller or @RestController.
@RequestBody binds the request body to a method parameter using HttpMessageConverter. Validation can be added with @Valid.
@GetMapping is a shortcut for @RequestMapping(method=RequestMethod.GET). Example usage is shown in the accompanying image.
@PostMapping is a shortcut for @RequestMapping(method=RequestMethod.POST). Example usage is shown in the accompanying image.
@PutMapping is a shortcut for @RequestMapping(method=RequestMethod.PUT). Example usage is shown in the accompanying image.
@DeleteMapping is a shortcut for @RequestMapping(method=RequestMethod.DELETE). Example usage is shown in the accompanying image.
@PatchMapping is a shortcut for @RequestMapping(method=RequestMethod.PATCH). Example usage is shown in the accompanying image.
@ControllerAdvice works with @ExceptionHandler, @InitBinder, and @ModelAttribute to provide global exception handling and data binding.
@ResponseBody writes the return value of a controller method directly to the HTTP response. When used on a class annotated with @RestController, the annotation is implicit.
@PathVariable binds method parameters to URI template variables defined in @RequestMapping. It supports value or name as an alias and can be marked as optional with required=false.
@RequestParam binds query parameters or form data to method arguments. It shares configuration options with @PathVariable, including default values.
@Controller marks a class as a Spring MVC controller (a specialization of @Component).
@RestController combines @Controller and @ResponseBody, eliminating the need to annotate each method with @ResponseBody.
@ModelAttribute adds attributes to the model before a controller method is invoked; it can also be used on methods to populate the model.
@CrossOrigin enables CORS support for a controller or method.
@InitBinder customizes data binding for request parameters, such as date formatting.
Component Scanning and Stereotype Annotations
@ComponentScan configures the packages to scan for components.
@Component marks a generic bean for Spring management.
@Service is a specialization of @Component for business logic.
@Repository is a specialization of @Component for DAO classes.
Dependency Injection and Bean Lifecycle
@DependsOn forces initialization order between beans.
@Bean declares a bean method; initMethod and destroyMethod control lifecycle callbacks.
@Scope defines bean scope: singleton, prototype, request, session, globalSession, etc.
@Autowired injects dependencies and can be applied to constructors, setters, or fields.
@Primary gives a bean higher priority when multiple candidates exist.
@PostConstruct and @PreDestroy (from JSR‑250) define methods to run after bean creation and before bean destruction.
@Qualifier disambiguates injection when multiple beans of the same type are present.
Spring Boot Specific Annotations
@SpringBootApplication combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.
@EnableAutoConfiguration triggers auto‑configuration based on classpath dependencies.
@ConditionalOnClass , @ConditionalOnMissingClass , @ConditionalOnBean , @ConditionalOnMissingBean , @ConditionalOnProperty , @ConditionalOnResource , @ConditionalOnWebApplication , @ConditionalOnNotWebApplication , @ConditionalExpression , and @Conditional allow bean registration based on various runtime conditions.
Each conditional annotation is illustrated with a concise code snippet.
Link: https://www.ramostear.com
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
