WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Framework · Basic · question 17 of 100

What is the difference between @Component, @Service, @Repository, and @Controller annotations in Spring?

📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

All the four annotations (@Component, @Service, @Repository, @Controller) are used for component scanning in Spring and help Spring to identify specific types of classes.

@Component is a generic annotation used for any Spring-managed component. It means that any class can be marked as a component using this annotation. For example:

@Component
public class MyComponent {
   // class implementation
}

@Service is used to annotate classes representing a service in a business logic layer. It indicates that this class is a business service and helps in implementing the Service layer. For example:

@Service
public class UserService {
   // class implementation
}

@Repository is used to annotate classes accessing the database. It automatically handles the DAO (Data Access Object) exceptions and makes it easier to implement the data access layer. For example:

@Repository
public class UserDaoImpl implements UserDao {
   // class implementation
}

@Controller is used to annotate web request handler classes. It indicates that the class is responsible for handling requests and serves as the middleman between the Model and the View. For example:

@Controller
public class UserController {
   // class implementation
}

In summary, each of the annotations represents a different type of class and helps in identifying what the class does in your Spring application. @Component is a more generic annotation, while @Service, @Repository, and @Controller represent more specific use cases of components in a Spring application.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Framework interview — then scores it.
📞 Practice Spring Framework — free 15 min
📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

All 100 Spring Framework questions · All topics