Master Spring MVC & Bean Annotations: A Complete Guide
This article provides a comprehensive overview of Spring MVC request‑mapping annotations, core Spring Bean annotations, dependency‑injection and scope configurations, container setup annotations, and essential Spring Boot annotations, each illustrated with clear code examples and usage tips.
1. Spring Web MVC and Spring Bean Annotations
Spring Web MVC Annotations
@RequestMapping maps web requests to handler methods. It is supported by RequestMappingHandlerMapping and RequestMappingHandlerAdapter. The annotation offers six attributes: value: URL or alias method: HTTP method params: request parameter filtering header: header filtering consume: allowed request body media types produce: allowed response media types
Before using @RequestMapping, the class must be annotated with @Controller or @RestController.
Examples of @RequestMapping usage are shown in the following diagrams:
@RequestMappingcan also be placed on a class, concatenating the class‑level value with method‑level paths.
@RequestBody
@RequestBodybinds the request body to a method parameter using HttpMessageConverter. Validation can be added with @Valid.
Shortcut Annotations
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping, and @PatchMapping are composed variants of @RequestMapping for specific HTTP methods.
@ControllerAdvice
@ControllerAdviceworks with @ExceptionHandler, @InitBinder, and @ModelAttribute to provide global exception handling and data binding.
@ResponseBody
@ResponseBodywrites the method return value directly to the HTTP response. In @RestController, this behavior is implicit.
@PathVariable
@PathVariablebinds method parameters to URI template variables. The value or name attribute can define an alias.
@RequestParam
@RequestParambinds request parameters to method arguments, with optional defaultValue and required settings.
@Controller, @RestController, @Component, @Service, @Repository
These stereotypes mark classes for component scanning. @RestController combines @Controller and @ResponseBody.
@ModelAttribute
Exposes a method’s return value as a model attribute, simplifying data sharing between controller methods.
@CrossOrigin
Enables CORS support for a controller or specific handler methods.
2. Spring Bean Annotations
@ComponentScanconfigures packages to scan for components. @Component marks a generic bean.
@Serviceand @Repository are specializations of @Component for business logic and DAO layers respectively.
3. Spring Dependency Injection and Bean Scopes
DI Annotations
@DependsOnforces bean initialization order.
@Beandeclares a bean method; initMethod and destroyMethod define lifecycle callbacks.
Scope Annotations
@Scopedefines bean scope: singleton, prototype, request, session, globalSession, etc.
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)4. Container Configuration Annotations
@Autowired
Injects dependencies into constructors, fields, or setter methods.
@Primary
Designates a bean as the primary candidate when multiple beans of the same type exist.
@PostConstruct & @PreDestroy
Lifecycle callbacks defined in JSR‑250; executed before bean initialization and before destruction.
@Qualifier
Disambiguates injection when multiple beans of the same type are present.
5. Spring Boot Annotations
@SpringBootApplicationcombines @Configuration, @EnableAutoConfiguration, and @ComponentScan.
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}Conditional annotations ( @ConditionalOnClass, @ConditionalOnMissingBean, @ConditionalOnProperty, etc.) enable fine‑grained auto‑configuration based on classpath, bean presence, or property values.
@Configuration
@ConditionalOnClass(DataSource.class)
class MySQLAutoConfiguration { /* ... */ } @Bean
@ConditionalOnMissingBean
public MyBean myBean() { return new MyBean(); } @Bean
@ConditionalOnProperty(name = "alipay", havingValue = "on")
Alipay alipay() { return new Alipay(); }Summary
This tutorial consolidates the most commonly used Spring and Spring Boot annotations, providing clear explanations and visual examples to help developers confidently apply them 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.
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.
