Master Spring Annotations: From @RequestMapping to @SpringBootApplication
This comprehensive guide explains the purpose and usage of Spring MVC, Bean, Dependency Injection, container‑configuration, and Spring Boot annotations, providing clear examples and code snippets that help Java developers master annotation‑driven configuration for backend applications.
1. Spring Web MVC Annotations
@RequestMapping maps web requests to handler methods. It supports the six attributes value, method, params, header, consume, and product. Before using @RequestMapping, the class must be annotated with @Controller or @RestController. Example usage is illustrated below.
@RequestMapping can also be placed on a class, automatically prefixing the class‑level path to each method’s mapping.
@RequestBody binds the request body to an object via HttpMessageConverter and can be validated with @Valid.
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping are shortcut annotations for @RequestMapping with specific HTTP methods.
2. Spring Bean Annotations
@ComponentScan configures the packages that Spring should scan for components.
@Component marks a generic component class for Spring management.
@Service and @Repository are specializations of @Component for business‑logic and DAO layers respectively.
3. Spring Dependency Injection and Bean Scopes
@DependsOn forces the initialization order of beans.
@Bean declares a method that produces a bean. Its initMethod and destroyMethod attributes control lifecycle callbacks.
@Scope defines the bean’s lifecycle scope: singleton, prototype, request, session, globalSession, or a custom scope. Example for prototype scope:
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)Singleton scope example:
4. Container Configuration Annotations
@Autowired injects dependencies via constructor, setter, or field.
@Primary designates a bean as the default when multiple candidates exist.
@PostConstruct and @PreDestroy (from JSR‑250) run before bean initialization and before bean destruction.
@Qualifier selects a specific bean among several candidates of the same type.
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 the classpath.
Conditional annotations allow fine‑grained bean registration:
@ConditionalOnClass / @ConditionalOnMissingClass – based on class presence.
@ConditionalOnBean / @ConditionalOnMissingBean – based on bean existence.
@ConditionalOnProperty – based on configuration properties.
@ConditionalOnResource – based on resource availability.
@ConditionalOnWebApplication / @ConditionalOnNotWebApplication – based on web environment.
@ConditionalExpression – based on SpEL expressions.
@Conditional – for custom condition implementations.
Summary
The article reviews the most common Spring and Spring Boot annotations, demonstrating their purpose, typical usage patterns, and code examples, helping readers gain a comprehensive understanding of annotation‑driven configuration in Java backend development.
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 Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
