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

Spring Boot & Hibernate · Advanced · question 42 of 100

What is the difference between @Component, @Repository, and @Service annotations in Spring Boot? When should each be used?

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

In Spring Boot, @Component, @Repository, and @Service are all stereotype annotations that can be used to define a Spring-managed component. While they are functionally equivalent, they are used to provide additional context about the purpose of the component.

Here’s a brief overview of each annotation:

@Component: This is the most generic stereotype annotation and can be used to annotate any class that is a Spring-managed component. This annotation is typically used for general-purpose components that do not fall into any other specific category.

@Repository: This annotation is used to annotate classes that serve as data access objects (DAOs) and are responsible for interacting with the database. It is typically used for classes that perform CRUD operations and other database-related tasks.

@Service: This annotation is used to annotate classes that provide business logic and application-level functionality. It is typically used for classes that implement business logic, perform complex calculations, and interact with multiple DAOs.

When deciding which annotation to use, it’s important to consider the purpose of the component and the semantics of the annotations. Using the appropriate annotation can make the code more readable and help other developers understand the purpose of the component.

Here’s an example of how these annotations might be used in a Spring Boot application:

    @Component
    public class EmailService {
        // generic email service component
    }
    
    @Repository
    public class UserRepository {
        // DAO for interacting with the user table in the database
    }
    
    @Service
    public class UserService {
        private final UserRepository userRepository;
        
        public UserService(UserRepository userRepository) {
            this.userRepository = userRepository;
        }
        
        public List<User> getUsers() {
            return userRepository.findAll();
        }
        
        // additional business logic methods...
    }

In this example, the EmailService class is a generic component that doesn’t fall into any specific category, so it is annotated with @Component. The UserRepository class is responsible for interacting with the database, so it is annotated with @Repository. Finally, the UserService class provides business logic and interacts with the UserRepository, so it is annotated with @Service.

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

All 100 Spring Boot & Hibernate questions · All topics