What are Components and Stereotypes in Spring Boot?

We will understand the concept of components within a Spring Boot application and the use of stereotypes.

What is a component (@Component)

A component is what allows Spring to detect the beans and place them inside its context to be used.
We learned in another post some concepts about what Spring dependency injection is. We understood the concept of a bean and saw that beans are objects managed by Spring.

When we indicate that a class is a @Component, what we are doing is telling Spring that we want it to create an instance and manage it.

@Component
public class MyClass {
    public void doSomething() {
        //.. 
    }
}

Spring will do the following when it starts the application:

  • It will search inside the application for the classes annotated with @Component.
  • Create an instance of the components
  • Leave the component in its context
  • It will look for who needs those components and will inject them into who needs them.

Spring Boot Component Scan

What are stereotypes?

Stereotypes are @Component’s extending components.

The most common stereotypes are:

  • @Controller / @RestController
  • @Service
  • @Repository

All of them are also @Component, but oriented to be part of a particular ‘layer’ of our service.

Spring Boot Component

A @Controller will be part of Spring’s controllers ‘layer’.
A @Service will be part of the service ‘layer’ in charge of business functionality.
A @Repository will be part of the persistence ‘layer’ whose function will be data access.

Note, for example, the @Service annotation which is an alias of an @Component. The same is true for the rest of the stereotypes.

Spring Boot Component Service

How to use @Controller, @Service, @Repository components

Recall that a rest controller is also a controller. We have several ways to use the components in our classes.

Reference by constructor:

Once we have created the components, we have several ways to require them.
One of them and the recommended one is through the constructor.

Spring, when creating the instance, will recognize that this class needs another component in its constructor and will build the class using the required component.
Note that the use of @Autowired is not necessary in this case.

Spring Boot Component Constructor

Reference by attribute with @Autowired:

We can also reference a dependency by using the attributes using the @Autowired annotation.
Autowired will load the attribute with the component using reflection.

Spring Boot Component Autowired

Reference by setter with @Autowired:

In addition to the reference by constructor and by attributes, it is possible to add references in a set methods.

Spring Boot Component Set

Conclusion

In this article, we learned what it is and how to use a Component in Spring and its stereotypes @Controller, @Service, @Repository to create a Rest application in Spring Boot.

Visit these other posts where about services and repositories.

Hi! If you find my posts helpful, please support me by inviting me for a coffee :)

See also