Backend Development 17 min read

Master Spring Annotations: From @RequestMapping to Conditional Configuration

This comprehensive guide explains the purpose, attributes, and usage of Spring MVC, DI, and conditional annotations—including @RequestMapping, @GetMapping, @Autowired, @Scope, @ConditionalOnClass, and more—providing clear examples and code snippets for each.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Master Spring Annotations: From @RequestMapping to Conditional Configuration

Hello, I am your friend Architect Jun, a poet‑architect who also writes code.

Table of Contents

Spring Web MVC and Spring Bean Annotations

Spring Bean Annotations

Spring Dependency Injection Annotations

Scope Annotations

@Autowired

@Primary

@PostConstruct and @PreDestroy

@Qualifier

@SpringBootApplication and Conditional Annotations

Spring Web MVC Annotations

@RequestMapping maps web requests to handler methods. It is supported by

RequestMappingHandlerMapping

and

RequestMappingHandlerAdapter

. The annotation has six attributes:

value

: the URL or its alias

method

: HTTP method name

params

: filter requests based on HTTP parameters

header

: filter requests based on HTTP headers

consume

: allowed media types in the request body

produces

: allowed media types in the response body

Before using

@RequestMapping

, the class must be annotated with

@Controller

or

@RestController

.

Example 1:

Class‑level

@RequestMapping

concatenates its

value

with method‑level mappings.

@RequestBody

@RequestBody

binds the request body to an object using

HttpMessageConverter

. Validation can be added with

@Valid

.

@GetMapping

@GetMapping

handles HTTP GET requests and is a shortcut for

@RequestMapping(method=RequestMethod.GET)

.

@PostMapping

@PostMapping

handles HTTP POST requests and is a shortcut for

@RequestMapping(method=HttpMethod.POST)

.

@PutMapping

@PutMapping

handles HTTP PUT requests and is a shortcut for

@RequestMapping(method=HttpMethod.PUT)

.

@DeleteMapping

@DeleteMapping

handles HTTP DELETE requests and is a shortcut for

@RequestMapping(method=HttpMethod.DELETE)

.

@PatchMapping

@PatchMapping

handles HTTP PATCH requests and is a shortcut for

@RequestMapping(method=HttpMethod.PATCH)

.

@ControllerAdvice

@ControllerAdvice

extends

@Component

and works with

@ExceptionHandler

,

@InitBinder

, and

@ModelAttribute

to handle exceptions globally.

@ResponseBody

@ResponseBody

writes the method return value directly to the HTTP response. It is implicit in

@RestController

.

@ExceptionHandler

@ExceptionHandler

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

@ResponseStatus

@ResponseStatus

sets the HTTP status code for a handler method.

@PathVariable

@PathVariable

binds a method parameter to a URI template variable. It supports

value

or

name

as an alias.

@RequestParam

@RequestParam

binds a method parameter to a web request parameter, with optional

defaultValue

.

@Controller

@Controller

is a specialization of

@Component

for Spring MVC controllers.

@RestController

@RestController

combines

@Controller

and

@ResponseBody

, eliminating the need for the latter on each method.

@ModelAttribute

@ModelAttribute

adds an attribute to the model or binds a method’s return value to the model.

@CrossOrigin

@CrossOrigin

enables CORS support for a controller or method.

@InitBinder

@InitBinder

customizes data binding for web request parameters, such as date formatting.

Spring Bean Annotations

@ComponentScan

configures packages to scan for components.

@Component

marks a generic component class for Spring management.

@Service

is a specialization of

@Component

for business logic.

@Repository

marks DAO classes for persistence.

Spring DI Annotations

@DependsOn

forces bean initialization order.

@Bean

declares a bean method; its

initMethod

and

destroyMethod

control lifecycle callbacks.

Scope Annotations

@Scope

defines bean scope such as

singleton

,

prototype

,

request

,

session

, etc.

@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)

Examples of singleton and prototype scopes are illustrated with images.

@Autowired

@Autowired

injects dependencies into constructors, setters, or fields.

@Primary

@Primary

gives higher priority to a bean when multiple candidates of the same type exist.

@PostConstruct and @PreDestroy

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

@Qualifier

@Qualifier

disambiguates which bean should be injected when multiple candidates exist.

@SpringBootApplication and Conditional Annotations

@SpringBootApplication

combines

@Configuration

,

@EnableAutoConfiguration

, and

@ComponentScan

.

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

Conditional annotations such as

@ConditionalOnClass

,

@ConditionalOnMissingBean

,

@ConditionalOnProperty

, and

@Conditional

enable bean creation based on classpath, bean presence, property values, or custom conditions.

@Configuration
@ConditionalOnClass(DataSource.class)
class MySQLAutoConfiguration { /* ... */ }
@Bean
@ConditionalOnBean(name="dataSource")
LocalContainerEntityManagerFactoryBean entityManagerFactory() { /* ... */ }
@Bean
@ConditionalOnMissingBean
public MyBean myBean() { return new MyBean(); }
@Bean
@ConditionalOnProperty(name="alipay", havingValue="on")
Alipay alipay() { return new Alipay(); }
BackendJavaSpringannotationsSpringBootDependencyInjection
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

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.