Spring Annotations Explained for Beginners 2026 | Built-in & Custom Examples

Author: Ritika
0

 

If you are learning Spring or Spring Boot for the first time, I can almost guarantee one thing — annotations will confuse you at least once 😅

 

When I first started working on a Spring Boot project in my 6th semester, I saw so many @Something annotations everywhere — @Controller, @Autowired, @Service, @Component… and I was like, “Bhai, what is happening? Why is everything starting with @?”

 

But once I understood the concept properly, things became much simpler.

 

So in this blog, I’ll explain Spring Annotations in very simple language — the way I wish someone had explained it to me during college.

Spring Annotations Explained for Beginners 2026 | Built-in & Custom Examples
(toc) #title=(Table of Content)

 

What Are Spring Annotations?

In simple words, annotations are special markers that give instructions to the Spring framework.

 

Think of annotations like:

  • Notes you write on your assignment paper for the teacher
  • Or instructions you give to your friend while doing a group project

 

Instead of writing long XML configuration files (which older Spring versions used), we now use annotations to tell Spring what to do.

 

For example:

@Controller
public class HomeController {}

Here, @Controller tells Spring:

“Hey Spring, this class is responsible for handling web requests.”

 

That’s it. Clean. Simple. Powerful.

 

Why Do We Use Annotations in Spring?

Before annotations, developers used XML configuration. It was long, boring, and easy to mess up.

 

Annotations:

  • Reduce configuration
  • Make code cleaner
  • Improve readability
  • Save time during projects

 

Honestly, for college mini-projects or internships, annotations make development much faster.

 

Built-in Annotations in Spring

Spring provides many built-in annotations. Let’s discuss the most important ones that every beginner should know.

 

1. @Component

This is the basic stereotype annotation.

It tells Spring:

“Create an object of this class and manage it.”

@Component
public class Student {}

When Spring starts, it creates a Student object automatically.

 

When I first learned this, I realized:

We don’t need to manually create objects using new keyword all the time.

 

2. @Controller

Used in web applications.

It tells Spring that this class handles HTTP requests.

@Controller
public class LoginController {}

If you are building a login page in your project, this annotation is a must.

 

Many students get confused between @Controller and @RestController. Don’t worry, we’ll discuss that too.

 

3. @RestController

Used in REST APIs.

Basically:

@Controller + @ResponseBody = @RestController

 

@RestController
public class StudentController {}

If you are building APIs for a React frontend (like I did in my Employee Management System project), you will mostly use @RestController.

 

4. @Service

Used for business logic layer.

@Service
public class StudentService {}

In simple words:

  • Controller → Handles request
  • Service → Contains logic
  • Repository → Talks to database

 

This structure is very common in projects and exams.

 

5. @Repository

Used for database layer.

@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {}

It helps Spring handle database exceptions properly.

 

6. @Autowired

This one is very important.

It is used for Dependency Injection.

@Autowired
private StudentService studentService;

Instead of creating object manually like:

StudentService service = new StudentService();

Spring automatically injects it.

 

When I learned Dependency Injection properly, I understood why Spring is so powerful.

 

7. @RequestMapping

Used to map URLs.

@RequestMapping("/home")

It can be used at class level or method level.

 

But nowadays we mostly use:

  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping

These are shortcut annotations.

@GetMapping("/students")
public List<Student> getAllStudents() {}

 

8. @SpringBootApplication

If you are using Spring Boot, you have definitely seen this:

@SpringBootApplication
public class DemoApplication {}

This is a combination of:

  • @Configuration
  • @EnableAutoConfiguration
  • @ComponentScan

 

In simple words:

It tells Spring Boot to start the application.

 

Quick Comparison Table

Annotation Layer Purpose
@Component General Creates bean
@Controller Web Handles requests
@RestController Web API Returns JSON data
@Service Business Contains logic
@Repository Database Handles DB operations
@Autowired Any Injects dependency

 

This table helped me a lot during exam revision 😄

 

Custom Annotations in Spring

Now comes the interesting part.

Sometimes built-in annotations are not enough.

So we create Custom Annotations.

Don’t worry — it’s not that scary.

 

What Is a Custom Annotation?

It’s an annotation that we create ourselves using @interface.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime {}

Here:

  • @Target defines where it can be used
  • @Retention defines how long it is available

 

Example Use Case

Suppose in your college project, you want to log execution time of methods.

You can create:

@LogExecutionTime
public void processData() {}

Then use Aspect Oriented Programming (AOP) to handle it.

This is advanced, but useful in real-world projects.

 

When I first saw custom annotations, I thought:

“Why do we need this?”

 

But later I realized — in large applications, they help maintain clean and reusable logic.

 

Usage Examples in Real Projects

Let’s take a simple example of a Student Management System.

 

Controller

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public List<Student> getStudents() {
        return studentService.getAll();
    }
}

 

Service

@Service
public class StudentService {

    @Autowired
    private StudentRepository repository;

    public List<Student> getAll() {
        return repository.findAll();
    }
}

 

Repository

@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {}

 

See how clean it looks?

Without annotations, this would require long configuration.

 

Advantages of Spring Annotations

Let’s be honest. Why do we actually care?

Here are the major advantages:

 

1. Less Configuration

No need to write XML.

Perfect for students doing projects under deadline pressure.

 

2. Cleaner Code

Everything is written directly in the class.

Easy to understand during viva.

 

3. Faster Development

For hackathons, internships, or final year projects — speed matters.

Annotations make development quicker.

 

4. Better Readability

Anyone looking at your project can understand structure easily.

 

5. Supports Dependency Injection

This is the backbone of Spring.

Makes application loosely coupled.

 

And in interviews, this topic is very important.

 

Common Mistakes Students Make

Let me share some mistakes I made:

  • Forgetting @ComponentScan
  • Mixing @Controller and @RestController
  • Not understanding Dependency Injection
  • Manually creating objects even when using Spring

 

If you understand the role of each annotation clearly, these mistakes reduce.

 

FAQs – Spring Annotations

1. What is the difference between @Controller and @RestController?

@Controller returns view (like JSP/HTML).

@RestController returns JSON data.

 

2. What does @Autowired do?

It automatically injects dependencies instead of creating objects manually.

 

3. Can we create our own annotations?

Yes, using @interface. They are called Custom Annotations.

 

4. Is @SpringBootApplication mandatory?

Yes, in Spring Boot main class. It starts the application.

 

5. Are annotations better than XML configuration?

For modern applications — yes. They are simpler and cleaner.

 

Conclusion

When I first started learning Spring, annotations looked complicated and confusing.

 

But once I understood that:

  • They are just instructions for the Spring framework

 

Everything became much easier.

 

If you are a beginner:

  • Start with basic annotations
  • Build a small CRUD project
  • Practice Dependency Injection
  • Don’t memorize — understand the purpose

 

Trust me, once you build 2–3 projects using Spring Boot, annotations will feel completely natural.

 

And in interviews, if you can explain annotations clearly with examples from your own projects, that creates a strong impression.

 

So don’t just read.

Open your IDE.

Create a small project.

Use these annotations.

 

That’s how real learning happens 💻🔥

 

Read Also : Java Annotations Explained for Beginners | Built-in & Custom Annotations in Java


 

Tags

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !)#days=(20)

We use cookies to personalize content and ads, provide social media features, and analyze our traffic. By clicking "Accept", you consent to our use of cookies. Privacy Policy
Accept !
Breaking News | News |