Stereotype Annotation in Spring

Stereotype annotations in Spring, such as @Component, @Service, and @Repository, play a crucial role in simplifying the configuration of Spring applications. These annotations are used to declare classes as beans to be managed by the Spring context, thereby reducing boilerplate code and enhancing the clarity of the class design by explicitly marking the responsibilities of the components.

Overview of Stereotype Annotations

Stereotype annotations help define the roles of components within a Spring application:

  • @Component: A generic stereotype for any Spring-managed component. It is the most basic form of a stereotype annotation and can be used to mark a class as a Spring bean.
  • @Service: Specializes the @Component annotation to indicate that a class is a service, typically implementing business logic or use cases.
  • @Repository: Also a specialization of @Component, used to indicate that a class is a repository, responsible for data persistence.

These annotations not only simplify the configuration by automatically registering beans but also make the design more understandable by clearly defining the responsibilities of each component.

Adding Beans to the Spring Context

To add a class as a bean in the Spring context using stereotype annotations, you typically follow these steps:

  1. Annotate the Class: Use a stereotype annotation like @Component to mark the class you want Spring to manage.
  2. Component Scanning: Use the @ComponentScan annotation to instruct Spring on where to look for classes annotated with stereotype annotations.

Example: Using @Component and @ComponentScan

Consider the following example where a Parrot class is annotated with @Component:

Listing 2.16 Using a stereotype annotation for the Parrot class

@Component               // #1
public class Parrot {
 
  private String name;
 
  public String getName() {
    return name;
  }
 
  public void setName(String name) {
    this.name = name;
  }
}
// #1 By using the @Component annotation over the class, we instruct Spring to create an instance of this class and add it to its context.

However, simply annotating the class is not enough. By default, Spring does not search for classes annotated with stereotype annotations. To enable this, you must use the @ComponentScan annotation over the configuration class, specifying the package to scan:

Listing 2.17 Using the @ComponentScan annotation to tell Spring where to look

@Configuration
@ComponentScan(basePackages = "main")     // #1
public class ProjectConfig {
 
}
// #1 Using the basePackages attribute of the annotation, we tell Spring where to look for classes annotated with stereotype annotations.

Visual Representation

Figure 2.12 When using stereotype annotations, consider two steps. First, use the stereotype annotation (@Component) to annotate the class for which you want Spring to add a bean to its context. Second, use the @ComponentScan annotation to tell Spring where to look for classes annotated with stereotype annotations. Figure 2.12 When using stereotype annotations, consider two steps. First, use the stereotype annotation (@Component) to annotate the class for which you want Spring to add a bean to its context. Second, use the @ComponentScan annotation to tell Spring where to look for classes annotated with stereotype annotations.

Specialized Stereotype Annotations

In addition to @Component, Spring provides specialized stereotype annotations to further clarify the role of a class:

  • @Service: Used for classes that implement business logic. For example:

    @Service                     // #1
    public class CommentService {
      // Omitted code
    }
    // #1 We use @Service to define this object as a component having the responsibility of service.
    
  • @Repository: Used for classes that handle data persistence. For example:

    @Repository                                                     // #1
    public class DBCommentRepository implements CommentRepository {
      // Omitted code
    }
    // #1 We use @Repository to define this object as a component with the responsibility of the repository.
    

Declaring Prototype-Scoped Beans

To declare a prototype-scoped bean using stereotype annotations, you can use the @Scope annotation. For instance, to change the scope of a CommentRepository class to prototype, you would annotate it with @Scope(BeanDefinition.SCOPE_PROTOTYPE). This allows Spring to create a new instance of the bean every time it is requested.

FAQ (Frequently asked questions)

How do stereotype annotations simplify configuration in Spring?