Comprehensive Guide to Spring MVC, Spring Bean, and Spring Boot Annotations

This article provides a detailed overview of the most commonly used Spring MVC request‑mapping annotations, Spring Bean stereotypes, dependency‑injection and scope annotations, container configuration annotations such as @Autowired and @Primary, and essential Spring Boot annotations, illustrating each with clear code examples and diagrams.

Java Captain
Java Captain
Java Captain
Comprehensive Guide to Spring MVC, Spring Bean, and Spring Boot Annotations

1. Spring Web MVC Annotations

1‑1. Request Mapping Annotations

1‑1‑1. @RequestMapping

The @RequestMapping annotation maps web requests to handler methods. Spring MVC and Spring WebFlux support it via RequestMappingHandlerMapping and RequestMappingHandlerAdapter. It has six attributes: value, method, params, header, consumes, produces.

Tip: The handler class must be annotated with @Controller or @RestController before using @RequestMapping.

Examples of @RequestMapping usage are shown in the following images.

1‑1‑2. @RequestBody

@RequestBody binds the request body to a method parameter object using HttpMessageConverter and can be combined with @Valid for validation.

1‑1‑3. @GetMapping

@GetMapping is a shortcut for @RequestMapping(method = RequestMethod.GET).

1‑1‑4. @PostMapping

@PostMapping is a shortcut for @RequestMapping(method = RequestMethod.POST).

1‑1‑5. @PutMapping

@PutMapping is a shortcut for @RequestMapping(method = RequestMethod.PUT).

1‑1‑6. @DeleteMapping

@DeleteMapping is a shortcut for @RequestMapping(method = RequestMethod.DELETE).

1‑1‑7. @PatchMapping

@PatchMapping handles HTTP PATCH requests.

1‑1‑8. @ControllerAdvice

@ControllerAdvice is a specialization of @Component that allows global exception handling, data binding, and model attribute configuration.

1‑1‑9. @ResponseBody

@ResponseBody writes the method return value directly to the HTTP response. When used with @RestController, it is implicit.

1‑1‑10. @ExceptionHandler

@ExceptionHandler marks a method to handle specific exception types thrown by controller methods.

1‑1‑11. @ResponseStatus

@ResponseStatus sets the HTTP status code for the response.

1‑1‑12. @PathVariable

@PathVariable binds a method parameter to a URI template variable.

Tip: Set required = false for optional path variables.

1‑1‑13. @RequestParam

@RequestParam binds a method parameter to a request query parameter, with optional default values.

1‑1‑14. @Controller

@Controller marks a class as a Spring MVC controller.

1‑1‑15. @RestController

@RestController combines @Controller and @ResponseBody for RESTful services.

1‑1‑16. @ModelAttribute

@ModelAttribute adds attributes to the model or binds method return values to the model.

1‑1‑17. @CrossOrigin

@CrossOrigin enables CORS support for a controller or method.

1‑1‑18. @InitBinder

@InitBinder customizes data binding for web request parameters.

2. Spring Bean Annotations

2‑1. Stereotype Annotations

2‑1‑1. @ComponentScan

@ComponentScan configures the packages to scan for components.

2‑1‑2. @Component

@Component marks a generic component class for Spring to manage.

2‑1‑3. @Service

@Service is a specialization of @Component for business logic classes.

2‑1‑4. @Repository

@Repository is a specialization of @Component for DAO/persistence classes.

3. Spring Dependency Injection and Scope Annotations

3‑1. DI Annotations

3‑1‑1. @DependsOn

@DependsOn forces the initialization of specified beans before the annotated bean.

3‑1‑2. @Bean

@Bean declares a method that returns an object to be managed by the Spring container, with optional initMethod and destroyMethod.

3‑2. Scope Annotations

3‑2‑1. @Scope

@Scope defines the lifecycle of a bean (singleton, prototype, request, session, etc.). Example for prototype scope:

@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)

Two usage styles are illustrated in the following image.

3‑2‑2. @Scope Singleton

When set to singleton, the bean is instantiated only once by the container.

4. Container Configuration Annotations

4‑1. @Autowired

@Autowired injects dependencies into constructors, fields, or setter methods.

4‑1‑1. Constructor Injection

4‑1‑2. Setter Injection

4‑1‑3. Field Injection

4‑2. @Primary

@Primary gives higher precedence to a bean when multiple candidates exist.

4‑3. @PostConstruct and @PreDestroy

These JSR‑250 annotations define methods to run after bean creation and before bean destruction.

4‑4. @Qualifier

@Qualifier narrows injection when multiple beans of the same type exist.

5. Spring Boot Annotations

5‑1. @SpringBootApplication

@SpringBootApplication combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

5‑2. @EnableAutoConfiguration

Enables Spring Boot’s auto‑configuration based on classpath dependencies.

5‑3. Conditional Annotations

@ConditionalOnClass, @ConditionalOnMissingClass, @ConditionalOnBean, @ConditionalOnMissingBean, @ConditionalOnProperty, @ConditionalOnResource, @ConditionalOnWebApplication, @ConditionalOnNotWebApplication, @ConditionalExpression, and @Conditional allow fine‑grained bean registration based on environment conditions.

Examples are shown in the following code snippets.

@Configuration
@ConditionalOnClass(DataSource.class)
class MySQLAutoConfiguration { /* ... */ }
@Bean
@ConditionalOnBean(name = "dataSource")
LocalContainerEntityManagerFactoryBean entityManagerFactory() { /* ... */ }

@Bean
@ConditionalOnMissingBean
MyBean myBean() { return new MyBean(); }
@Bean
@ConditionalOnProperty(name = "alipay", havingValue = "on")
Alipay alipay() { return new Alipay(); }
@ConditionalOnResource(resources = "classpath:website.properties")
Properties addWebsiteProperties() { /* ... */ }
@ConditionalOnWebApplication
HealthCheckController healthCheckController() { /* ... */ }
@ConditionalException("${localstore} && ${local == 'true'}")
LocalFileStore store() { /* ... */ }
@Conditional(CustomConditional.class)
CustomProperties addCustomProperties() { /* ... */ }

Conclusion

This article summarized the most commonly used Spring MVC, Spring Bean, and Spring Boot annotations, providing a unified reference for developers. Less common annotations will be covered in a future post.

(End)

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.

JavaspringSpring BootannotationsSpring MVC
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

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.