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
| Annotation | Put it on | It means |
|---|---|---|
@SpringBootApplication | The main class | Start the app, scan for components |
@RestController | A class | This handles web requests and returns data |
@Service | A class | This holds business logic (a bean) |
@Repository | A class | This talks to the database (a bean) |
@Component | A class | A general-purpose bean |
@Autowired | A field/constructor | Inject a bean here |
@GetMapping | A method | Handle 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:
@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:
@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?
✍️ Practice
- Make a flashcard for each of the seven annotations: name on one side, meaning on the other.
- Label a small class with
@RestControllerand one method with@GetMapping.
🏠 Homework
- Find a Spring code sample online and list every annotation you recognise, with what each does.