Comprehensive Guide to Spring MVC, Bean, and Boot Annotations
This article provides a detailed overview of Spring MVC request‑mapping annotations, Spring Bean stereotypes, dependency‑injection and scope annotations, as well as Spring Boot conditional and lifecycle annotations, complete with code examples and usage tips for Java backend development.
1. Spring Web MVC Annotations
The @RequestMapping annotation maps HTTP requests to handler methods and is supported by RequestMappingHandlerMapping and RequestMappingHandlerAdapter. It offers six attributes: value, method, params, header, consume, and product. Before using it, the class must be annotated with @Controller or @RestController.
Examples include class‑level @RequestMapping to prefix all method paths and method‑level usage for specific URLs.
@RequestBody
@RequestBodybinds the request body to a method parameter via HttpMessageConverter and can be combined with @Valid for validation.
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @PatchMapping
These are shortcut annotations for @RequestMapping with specific HTTP methods (GET, POST, PUT, DELETE, PATCH). Each example shows the typical usage pattern.
@ControllerAdvice
Marks a class as a global exception handler and allows the use of @ExceptionHandler, @InitBinder, and @ModelAttribute to process exceptions, data binding, and model attributes across all controllers.
2. Spring Bean Annotations
@ComponentScanconfigures the packages to scan for components. @Component marks a generic bean, while @Service, @Repository, and @Controller are specialized stereotypes for business logic, DAO, and MVC controllers respectively.
3. Dependency Injection and Scope Annotations
@Autowiredinjects dependencies on constructors, fields, or setter methods. @Primary gives a bean higher priority when multiple candidates exist. @Qualifier disambiguates injection by name. @Bean defines a bean method; its initMethod and destroyMethod attributes control lifecycle callbacks. @DependsOn forces bean initialization order. @Scope specifies bean scope such as singleton, prototype, request, session, etc. Example shows prototype usage with ConfigurableBeanFactory.SCOPE_PROTOTYPE.
4. Spring Boot Configuration Annotations
@SpringBootApplicationcombines @Configuration, @EnableAutoConfiguration, and @ComponentScan. @EnableAutoConfiguration triggers auto‑configuration based on classpath dependencies.
Conditional annotations ( @ConditionalOnClass, @ConditionalOnMissingClass, @ConditionalOnBean, @ConditionalOnMissingBean, @ConditionalOnProperty, @ConditionalOnResource, @ConditionalOnWebApplication, @ConditionalOnNotWebApplication) enable or disable beans/configurations based on the presence of classes, beans, properties, resources, or application type.
Lifecycle annotations from JSR‑250, @PostConstruct and @PreDestroy, are used for initialization and cleanup methods.
5. Summary
The article consolidates the most commonly used Spring MVC, Spring Bean, dependency‑injection, scope, and Spring Boot conditional annotations, providing clear explanations and code snippets to help Java backend developers apply these annotations effectively in real projects.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn 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.
