Comprehensive Guide to Spring MVC, Spring Bean, and Spring Boot Annotations
This article provides a detailed overview of Spring MVC request‑mapping annotations, Spring Bean lifecycle and dependency‑injection annotations, as well as Spring Boot configuration annotations, illustrating each with explanations and code examples to help Java backend developers use these tools effectively.
1. Spring Web MVC Annotations
The @RequestMapping annotation maps HTTP requests to handler methods; it supports six attributes (value, method, params, header, consumes, produces) and can be applied at both class and method levels. Example usage:
@RequestMapping(value="/users", method=RequestMethod.GET, params="id")Shortcut annotations such as @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping are equivalent to @RequestMapping with a specific HTTP method.
2. Spring Bean Annotations
Core component annotations include @Component, @Service, @Repository, and @Controller, which register classes as Spring beans. Scoping is controlled with @Scope, supporting singleton, prototype, request, session, etc. @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) Dependency injection is performed with @Autowired (field, setter, or constructor), optionally qualified by @Qualifier or prioritized with @Primary. Lifecycle callbacks use @PostConstruct and @PreDestroy.
3. Container‑Configuration Annotations
Exception handling and model binding are simplified with @ControllerAdvice, @ExceptionHandler, @ModelAttribute, and @ResponseBody. Cross‑origin support is added via @CrossOrigin, and data binding customization uses @InitBinder.
4. Spring Boot Annotations
The meta‑annotation @SpringBootApplication combines @Configuration, @EnableAutoConfiguration, and @ComponentScan, enabling rapid application setup.
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}Conditional bean creation can be controlled with annotations such as @ConditionalOnClass, @ConditionalOnMissingBean, @ConditionalOnProperty, and others, allowing fine‑grained configuration based on classpath, bean presence, or property values.
Overall, the article serves as a reference sheet for developers to understand and correctly apply the wide range of Spring and Spring Boot annotations in modern Java backend 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.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.
