Master Spring Boot Annotations: From @RequestMapping to @SpringBootApplication

This comprehensive guide explains the most commonly used Spring Boot annotations—including MVC mapping, bean definitions, dependency injection, scope management, container configuration, and bootstrapping—providing clear examples and visual diagrams to help developers write cleaner, more efficient Java code.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Master Spring Boot Annotations: From @RequestMapping to @SpringBootApplication

1. Spring Web MVC Annotations

@RequestMapping maps web requests to handler methods using RequestMappingHandlerMapping and RequestMappingHandlerAdapter. It supports six attributes: value, method, params, header, consume, and product. The controller class must be annotated with @Controller or @RestController.

Examples of @RequestMapping usage are illustrated below:

@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. It can be combined with @Valid for validation.

@GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @PatchMapping

These shortcut annotations are equivalents of @RequestMapping with specific HTTP methods: @GetMapping

@RequestMapping(method=RequestMethod.GET)
@PostMapping

@RequestMapping(method=HttpMethod.POST)
@PutMapping

@RequestMapping(method=HttpMethod.PUT)
@DeleteMapping

@RequestMapping(method=HttpMethod.DELETE)
@PatchMapping

@RequestMapping(method=HttpMethod.PATCH) Each annotation is demonstrated with a screenshot:

2. Spring Bean Annotations

@ComponentScan configures packages to scan for components.

@Component marks a generic bean.

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

@Repository marks DAO classes.

Examples are shown with screenshots.

3. Dependency Injection and Scope Annotations

@DependsOn forces bean initialization order.

@Bean declares a bean method; initMethod and destroyMethod customize lifecycle.

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

@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)

Visual examples for prototype and singleton scopes are provided:

4. Container Configuration Annotations

@Autowired injects dependencies (constructor, setter, or field).

Constructor injection example:

Setter injection example:

Field injection example:

@Primary gives priority when multiple beans of the same type exist.

Output example: this is send DingDing method message. @PostConstruct and @PreDestroy (from JSR‑250) run before bean initialization and before bean destruction respectively.

@Qualifier resolves ambiguity 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 , @ConditionalOnMissingClass , @ConditionalOnBean , @ConditionalOnMissingBean , @ConditionalOnProperty , @ConditionalOnResource , @ConditionalOnWebApplication , @ConditionalOnNotWebApplication , @ConditionalExpression , and @Conditional allow fine‑grained conditional bean registration. Sample snippets are shown for each.

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

Summary

This article consolidates the most frequently used Spring Boot annotations, providing developers with a clear reference to improve code readability, maintainability, and overall development efficiency.

Annotationsspring-bootrest-controllerdependency-injectionspring-mvc
Java Backend Technology
Written by

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!

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.