Basic

13. Can you explain the difference between @Component, @Service, @Repository, and @Controller annotations in Spring?

Overview

Understanding the difference between @Component, @Service, @Repository, and @Controller annotations in Spring is crucial for developers working with Spring Framework. These annotations are used for defining beans in the Spring container, and each serves a specific layer in an application, thus helping in the separation of concerns and cleaner codebase management.

Key Concepts

  • Stereotype Annotations: @Component, @Service, @Repository, and @Controller are known as stereotype annotations in Spring.
  • Layer Separation: Each annotation is designed for a different layer of an application (e.g., presentation, service, persistence).
  • Dependency Injection: These annotations make classes eligible for Spring's dependency injection mechanism.

Common Interview Questions

Basic Level

  1. What are the differences between @Component, @Service, @Repository, and @Controller annotations in Spring?
  2. How does Spring discover beans annotated with @Component, @Service, @Repository, or @Controller?

Intermediate Level

  1. Why would you choose @Repository over @Component for a DAO class?

Advanced Level

  1. How can you customize the behavior of these annotations (e.g., custom stereotype annotations)?

Detailed Answers

1. What are the differences between @Component, @Service, @Repository, and @Controller annotations in Spring?

Answer: While all four annotations are used to auto-detect and configure beans in Spring's ApplicationContext, they are intended for different layers of an application. @Component is a generic stereotype for any Spring-managed component. @Repository is specialized for persistence layer and encapsulates database access logic. @Service is intended for service layer and holds business logic. Lastly, @Controller is for the presentation layer (Spring MVC controllers).

Key Points:
- @Component is a generic stereotype for any Spring-managed component.
- @Repository is for the persistence layer and can integrate with Spring Data.
- @Service indicates a service layer and contains business logic.
- @Controller is for the presentation layer, particularly in Spring MVC for handling HTTP requests.

Example:

@Component
public class UtilityComponent {
    // Utility methods
}

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    // Database access methods
}

@Service
public class UserService {
    // Business logic
}

@Controller
public class UserController {
    // HTTP request handling
}

2. How does Spring discover beans annotated with @Component, @Service, @Repository, or @Controller?

Answer: Spring uses classpath scanning to discover beans annotated with these stereotypes. During the application startup, Spring scans the packages specified by the developer (using @ComponentScan annotation or XML configuration) to find and register beans marked with @Component, @Service, @Repository, or @Controller.

Key Points:
- Use of @ComponentScan for specifying packages to scan.
- Automatic registration of beans in Spring's ApplicationContext.
- These annotations make classes eligible for dependency injection.

Example:

@Configuration
@ComponentScan(basePackages = "com.example.application")
public class AppConfig {
    // Configuration class
}

3. Why would you choose @Repository over @Component for a DAO class?

Answer: Choosing @Repository for DAO classes is preferable because it’s specifically designed for the data access layer. It provides additional benefits like translating database access exceptions into Spring's DataAccessException. This makes exception handling more abstract and decoupled from the database API.

Key Points:
- @Repository provides automatic exception translation.
- It clearly indicates the DAO’s role in the application.
- It supports integration with Spring Data and JPA.

Example:

@Repository
public class UserDAO {
    // Data access methods
}

4. How can you customize the behavior of these annotations (e.g., custom stereotype annotations)?

Answer: Spring allows the creation of custom stereotype annotations. You can define a new annotation that itself is annotated with @Component (or any other stereotype annotation) and add meta-annotations or default attributes to it. This is useful for creating domain-specific layers or cross-cutting concerns like logging or security.

Key Points:
- Custom stereotype annotations enhance modularity.
- They can inherit behavior and add specific attributes or functionality.
- Useful for domain-specific layers or cross-cutting concerns.

Example:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface CustomService {
    // Custom attributes
}

This preparation guide covers the essentials of understanding the different stereotype annotations in Spring, which is fundamental for both beginners and experienced Spring developers.