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.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Master Spring MVC & Bean Annotations: A Complete Guide

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:

@RequestMapping

can also be placed on a class, concatenating the class‑level value with method‑level paths.

@RequestBody

@RequestBody

binds 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

@ControllerAdvice

works with @ExceptionHandler, @InitBinder, and @ModelAttribute to provide global exception handling and data binding.

@ResponseBody

@ResponseBody

writes the method return value directly to the HTTP response. In @RestController, this behavior is implicit.

@PathVariable

@PathVariable

binds method parameters to URI template variables. The value or name attribute can define an alias.

@RequestParam

@RequestParam

binds 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

@ComponentScan

configures packages to scan for components. @Component marks a generic bean.

@Service

and @Repository are specializations of @Component for business logic and DAO layers respectively.

3. Spring Dependency Injection and Bean Scopes

DI Annotations

@DependsOn

forces bean initialization order.

@Bean

declares a bean method; initMethod and destroyMethod define lifecycle callbacks.

Scope Annotations

@Scope

defines 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

@SpringBootApplication

combines @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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

springSpring Bootannotationsdependency-injectionSpring MVCJava backend
Java Architect Essentials
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.