Spring Boot Annotations Explained

Beginner
8 min read· Backend & Databases

Spring Boot is annotation-driven: small markers on classes and methods tell the framework how to wire, expose, and manage your code. Instead of XML configuration, you sprinkle @Service, @RestController, and @Autowired and Spring does the rest. Knowing what the everyday annotations actually do — and which are compositions of others — turns Spring from magic into something you can reason about.

Think of annotations as sticky labels on a warehouse

In a warehouse, labels tell workers what each box is and how to handle it: "fragile," "ship overnight," "store cold." Spring annotations are those labels. @RestController says "this box handles web requests," @Service says "this holds business logic," @Transactional says "handle this operation as all-or-nothing." Spring reads the labels at startup and routes, wires, and wraps everything accordingly — no manual instructions needed.

Step by Step

1 / 5

Key Concepts

@RestController vs @Controller

@Controller returns view names for server-rendered pages. @RestController = @Controller + @ResponseBody, so every method returns the object serialised to JSON — the standard for REST APIs.

Meta-annotations

Many Spring annotations are compositions of others. @SpringBootApplication and @RestController are single labels that bundle several behaviours, which is why they feel like magic.

Proxy-based annotations

@Transactional, @Cacheable, and @Async work because Spring wraps your bean in a proxy. This is why they only apply to public methods called from outside the class, not self-invocations.

@Configuration and @Bean

@Configuration classes define beans programmatically with @Bean methods — useful for third-party classes you cannot annotate directly.

Key Facts

  • A self-invoked @Transactional or @Cacheable method (this.method()) bypasses the proxy and the annotation has no effect — a very common gotcha.
  • @RequestBody deserialises JSON into an object; @ResponseBody (built into @RestController) serialises the return value back to JSON.
  • Prefer the specific mapping shortcuts (@GetMapping) over @RequestMapping(method=...) for readability.

Real-World Applications

A typical REST endpoint

A class marked @RestController with @RequestMapping("/api/orders"), methods annotated @GetMapping("/{id}") using @PathVariable, and a constructor-injected @Service is the standard shape of a Spring Boot API.

Declarative caching

Adding @Cacheable("products") to a slow lookup method makes Spring cache results transparently — no cache code in your method, just a label.

Frequently Asked Questions

What does @SpringBootApplication actually do?

It is a convenience annotation combining @Configuration, @EnableAutoConfiguration, and @ComponentScan. Together they let Spring define beans, auto-configure the app based on classpath dependencies, and scan your package for components — all from one annotation on the main class.

What is the difference between @Component, @Service, and @Repository?

All three register a bean. @Service marks business logic, @Repository marks a data-access bean and adds persistence-exception translation, and @Component is the generic base. Functionally similar, but they document each layer intent and @Repository adds behaviour.

Why does my @Transactional method not work?

The most common causes: the method is not public, or it is called from within the same class (self-invocation bypasses the Spring proxy), or the class is not a Spring-managed bean. @Transactional only works on proxied, externally-called public methods.

What is the difference between @RequestParam and @PathVariable?

@PathVariable binds a value embedded in the URL path (/orders/{id}), while @RequestParam binds a query-string parameter (?status=paid). Use path variables to identify a resource and request params to filter or modify a query.

Related Topics