Backend Development 16 min read

Overview of Spring Web MVC and Spring Bean Annotations

This article provides a comprehensive guide to Spring Web MVC and Spring Bean annotations, detailing usage, attributes, and examples of @RequestMapping, @GetMapping, @PostMapping, @ControllerAdvice, @Autowired, @Scope, and various Spring Boot conditional annotations, helping developers understand and apply them effectively.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Overview of Spring Web MVC and Spring Bean Annotations

1. Spring Web MVC Annotations

@RequestMapping maps web requests to handler methods. It is supported by RequestMappingHandlerMapping and RequestMappingHandlerAdapter . The annotation has six attributes: value , method , params , header , consume , and produces . The controller class must be annotated with @Controller or @RestController .

Examples of using @RequestMapping are shown in the following images:

@RequestBody binds the request body to a method parameter using HttpMessageConverter . Validation can be added with @Valid .

@GetMapping , @PostMapping , @PutMapping , @DeleteMapping , and @PatchMapping are shortcut annotations for @RequestMapping with specific HTTP methods.

@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 , it is implicit.

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

@ResponseStatus can set the HTTP status code for a response.

@PathVariable binds URI template variables to method parameters.

@RequestParam binds request parameters to method arguments, with support for default values.

@Controller marks a class as a Spring MVC controller; @RestController combines @Controller and @ResponseBody .

@ModelAttribute adds attributes to the model before a controller method is invoked.

@CrossOrigin enables CORS support for a controller or method.

@InitBinder customizes data binding for web requests.

2. Spring Bean Annotations

@ComponentScan configures the packages to be scanned for components.

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

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

@Repository marks DAO classes for persistence.

3. Spring Dependency Injection and Bean Scopes

@DependsOn forces initialization order of beans.

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

@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 gives a bean higher precedence when multiple candidates exist.

@PostConstruct and @PreDestroy define lifecycle callbacks (not Spring‑specific).

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

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);
    }
}

@EnableAutoConfiguration triggers auto‑configuration based on classpath dependencies.

@ConditionalOnClass and @ConditionalOnMissingClass activate configuration based on the presence of a class.

@Configuration
@ConditionalOnClass(DataSource.class)
class MySQLAutoConfiguration { /* ... */ }

@ConditionalOnBean and @ConditionalOnMissingBean depend on the existence of a bean.

@Bean
@ConditionalOnBean(name="dataSource")
LocalContainerEntityManagerFactoryBean entityManagerFactory() { /* ... */ }

@Bean
@ConditionalOnMissingBean
public MyBean myBean() { /* ... */ }

@ConditionalOnProperty checks configuration properties.

@Bean
@ConditionalOnProperty(name="alipay", havingValue="on")
Alipay alipay() { return new Alipay(); }

@ConditionalOnResource activates when a resource exists.

@ConditionalOnResource(resources="classpath:website.properties")
Properties addWebsiteProperties() { /* ... */ }

@ConditionalOnWebApplication and @ConditionalOnNotWebApplication detect the application type.

@ConditionalOnWebApplication
HealthCheckController healthCheckController() { /* ... */ }

@ConditionalExpression and @Conditional allow custom conditional logic.

@Conditional(CustomConditional.class)
CustomProperties addCustomProperties() { /* ... */ }

6. Summary

The article summarizes the most commonly used Spring Boot annotations, providing clear explanations and visual examples to help developers master annotation‑driven configuration and dependency injection in Spring applications.

SpringSpring BootAnnotationsdependency injectionWeb MVC
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login 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.