Spring BasicsCore· 30 min read

The Common Annotations

Annotations are the @-labels that tell Spring what each class or method is — learn the handful you will use every day.

What you will learn

  • Read the most common Spring annotations
  • Know which annotation goes where
  • Recognise them in real code

Annotations are labels

An annotation starts with @ and labels a class, method or field. Spring reads these labels to understand your code. You met @SpringBootApplication and @Service already; here is the everyday set in one place.

The ones you will use most

AnnotationPut it onIt means
@SpringBootApplicationThe main classStart the app, scan for components
@RestControllerA classThis handles web requests and returns data
@ServiceA classThis holds business logic (a bean)
@RepositoryA classThis talks to the database (a bean)
@ComponentA classA general-purpose bean
@AutowiredA field/constructorInject a bean here
@GetMappingA methodHandle a GET request to a URL

Seeing them together

Each annotation has one clear job. Read this and match each label to the table above:

Two annotations doing two clear jobs
@RestController              // handles web requests, returns data
public class HelloController {

    @GetMapping("/hello")    // run this method for GET /hello
    public String hello() {
        return "Hello from Spring Boot!";
    }
}

Note: Output (visiting /hello): Hello from Spring Boot! @RestController marks the whole class as a web handler. @GetMapping("/hello") connects the URL /hello to this exact method.

A bigger example: four annotations cooperating

In a real app several annotations work together. Here a @Service bean holds the logic, and a @RestController asks Spring to inject that bean (@Autowired on the constructor) and exposes it at a URL (@GetMapping). Read each label and say its job out loud:

Four annotations cooperating: @Service, @RestController, @Autowired and @GetMapping
@Service                                  // a bean that holds logic
public class PriceService {
    public double withTax(double price) {
        return price * 1.18;              // add 18% tax
    }
}

@RestController                            // handles web requests
public class PriceController {
    private final PriceService service;

    @Autowired                            // inject the PriceService bean here
    public PriceController(PriceService service) {
        this.service = service;
    }

    @GetMapping("/price")                 // run for GET /price
    public String price() {
        return "Total with tax: " + service.withTax(1000);
    }
}

Note: Output (visiting /price): Total with tax: 1180.0 @Service made PriceService a bean; @Autowired told Spring to inject it into the controller; @RestController marked the class as a web handler; @GetMapping("/price") wired the URL to the method. Four labels, four clear jobs — and 1000 × 1.18 = 1180.0.

You do not need to memorise every annotation Spring offers — there are hundreds. The seven above cover almost everything in this course. The rest you can look up when you need them.

Tip: Think of annotations as sticky notes for Spring: “this class handles web requests”, “inject a bean here”, “run this method for this URL”. They turn ordinary Java into Spring-aware code.

Watch out: Annotations only work if Spring scans the class. Keep your annotated classes in the same package tree as the main ...Application.java, and Spring will read their labels automatically.

Q. Which annotation marks a class that handles web requests and returns data?

Answer: @RestController marks a class as a web-request handler whose methods return data (usually JSON). @Service and @Repository are for logic and database beans.

✍️ Practice

  1. Make a flashcard for each of the seven annotations: name on one side, meaning on the other.
  2. Label a small class with @RestController and one method with @GetMapping.

🏠 Homework

  1. Find a Spring code sample online and list every annotation you recognise, with what each does.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →