About Us Contact Us Write for Us Advertise
Home > Java > Essential Spring Boot Annotations Explained (Beginner Guide)
Java

Essential Spring Boot Annotations Explained (Beginner Guide)

A clear, grouped guide to the Spring Boot annotations you use every day: stereotypes, @SpringBootApplication, web mappings, and dependency injection annotations.

Shiv Pandey
Shiv Pandey
Oct 04, 2026 | 31 views
Essential Spring Boot Annotations Explained (Beginner Guide)

Spring Boot code is covered in annotations — those little @Something labels above classes and methods. To a beginner they can feel like mysterious magic words. In this lesson we'll demystify the ones you'll actually use every day, grouped by what they do, so you can read and write Spring Boot code with confidence instead of copy-paste guesswork.

What even is an annotation?

An annotation is a label you attach to a class, method, or field that gives Spring instructions. @Service says "this is a service." @GetMapping says "this method handles GET requests." Spring reads these labels at startup and wires your app together accordingly. They're how you tell Spring what things are without writing configuration files.

Group 1: Marking your components (stereotype annotations)

These tell Spring "manage this class as a bean" (remember beans from the DI lesson). They're functionally similar but express your intent — which layer the class belongs to:

Annotation Put it on…
@Component any generic Spring-managed class
@Service business-logic classes
@Repository database-access classes
@Controller / @RestController classes handling web requests

Under the hood @Service, @Repository, and @Controller are all specialised versions of @Component. Using the specific one makes your code's intent obvious at a glance — and occasionally adds behaviour (e.g. @Repository improves database exception handling).

Group 2: The master annotation

@SpringBootApplication
public class MyApp { ... }

You met this in Lesson 1. It's actually three annotations in one: @Configuration (this class can define beans), @EnableAutoConfiguration (turn on Spring Boot's auto-setup), and @ComponentScan (find all my @Component-style classes in this package). One label, huge effect.

Group 3: Web request annotations

These map URLs to your methods — the heart of building APIs (much more in the REST API lesson):

@RestController
@RequestMapping("/users")   // base path for all methods here
public class UserController {

    @GetMapping              // GET /users
    public List<User> all() { ... }

    @GetMapping("/{id}")     // GET /users/42
    public User one(@PathVariable Long id) { ... }

    @PostMapping             // POST /users (create)
    public User create(@RequestBody User user) { ... }
}
  • @RequestMapping("/users") — a shared base path for the whole controller.
  • @GetMapping, @PostMapping, @PutMapping, @DeleteMapping — map the four common HTTP methods.
  • @PathVariable — grab a value from the URL (the {id} part).
  • @RequestBody — convert the incoming JSON request body into a Java object automatically.

Group 4: Dependency injection & config

@Service
public class UserService {
    private final UserRepository repo;

    // constructor injection — no @Autowired needed on a single constructor
    public UserService(UserRepository repo) { this.repo = repo; }

    @Value("${app.page-size:20}")   // inject a config value (default 20)
    private int pageSize;
}
  • @Autowired — asks Spring to inject a dependency. (Tip: not needed on a single constructor — Spring does it automatically. Prefer constructor injection over field @Autowired.)
  • @Value — injects a value from your configuration files (more in the config & profiles lesson).
You don't need to memorise all of these today. Keep this lesson as a reference and come back to it. In practice you'll use a small core — @RestController, @Service, @Repository, the mapping annotations, and constructor injection — constantly, and they'll become second nature within a week of building.

When I started, annotations felt like superstition — I'd sprinkle @Autowired around hoping things worked, without really knowing why. The shift came when I stopped seeing them as magic and started reading them as plain instructions to Spring: "@Service = manage this as a service bean," "@GetMapping = route GET here." Once you read them that way, Spring Boot code becomes surprisingly readable. On this website, scanning a file's annotations tells me instantly what each class does and how it's wired. Learn the handful in Group 1 and Group 3 well, and you'll understand the vast majority of real Spring Boot code you meet.

Key takeaways

  • Annotations are labels that instruct Spring — read them as plain-English intent, not magic.
  • Stereotypes (@Component/@Service/@Repository/@Controller) mark classes as beans by layer.
  • Web annotations (@RestController, @GetMapping/@PostMapping, @PathVariable, @RequestBody) map URLs to methods.
  • Use a small core daily; keep the rest as a reference — no need to memorise everything at once.

← Previous: Spring Lesson 3 — Your First Spring Boot App
Next: Spring Lesson 5 — Building a REST API →
↑ Back to the Spring Boot roadmap

Related Articles

Spring Boot Project Ideas (Beginner to Advanced)
Java

Spring Boot Project Ideas (Beginner to Advanced)

Deploying a Spring Boot App to Production (Beginner Guide)
Java

Deploying a Spring Boot App to Production (Beginner Guide)

Dockerizing a Spring Boot App (Beginner Guide)
Java

Dockerizing a Spring Boot App (Beginner Guide)

Spring Boot Async & Scheduling (Beginner Guide)
Java

Spring Boot Async & Scheduling (Beginner Guide)