Spring Boot @Component, @Controller, @Repository and @Service

In this tutorial, we will learn about Spring Stereotype Annotations such as @Component@Controller@Repository, and @Service annotations.

Spring Stereotype Annotations

Spring Stereotype annotations are used to create Spring beans automatically in the application context (Spring IoC container).

The main stereotype annotation is @Component. By using this annotation, Spring provides more Stereotype meta-annotations such as @Service, @Repository, and @Controller.

@Service annotation is used to create Spring beans at the Service layer.

@Repository is used to create Spring beans for the repositories at the DAO layer.

@Controller is used to create Spring beans at the controller layer.

YouTube Video - @Controller, @Repository and @Service

@Component

The @Component annotation indicates that an annotated class is a “spring bean/component”. Such classes are considered candidates for auto-detection when using annotation-based configuration and classpath scanning.

Here's an example of a simple Java class that is annotated as a Spring component:

import org.springframework.stereotype.Component;

@Component
public class HelloWorldComponent {
    public void printHello() {
        System.out.println("Hello, Spring!");
    }
}

In this example, the @Component annotation tells Spring that this class should be treated as a bean. 

Next, you can use this bean in other parts of your application by injecting it using the @Autowired annotation, as in:
import org.springframework.beans.factory.annotation.Autowired;

@Component
public class AnotherBean {
    @Autowired
    private HelloWorldComponent helloWorldComponent;

    public void printHello() {
        helloWorldComponent.printHello();
    }
}
Next, you need to specify the package to scan for classes annotated with @Component using the @ComponentScan annotation in a Spring configuration class. This will automatically register any classes annotated with @Component as beans in the application context.
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.javaguides.beans")
public class AppConfig {
}

The above configuration will scan the package "com.example.javaguides.beans" for classes annotated with @Component, @Service, @Repository, and @Controller and register them as beans in the Spring application context.

@Service 

The @Service annotation is also a specialization of @Component annotation. Basically, @Service annotation is used to create Spring beans at the Service layer.

import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
      // All the service layer logic goes here
}
Check out the complete example at Spring @Service Annotation

@Repository 

The @Repository annotation is also a specialization of @Component annotation. Basically, @Repository is used to create Spring beans for the repositories at the DAO layer.

import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {
      // All the dao layer logic goes here
}
Check out the complete example at Spring @Repository Annotation

@Controller

The @Controller annotation is also a specialization of @Component annotation. Basically, @Controller is used to create Spring beans at the controller layer.

import org.springframework.stereotype.Controller;

@Controller
public class UserController {
      // Build REST APIs
}

Check out the complete example at The Spring @Controller and @RestController Annotations with Examples

Conclusion

In this tutorial, we learned about Spring Stereotype Annotations such as @Component, @Controller, @Repository, and @Service annotations.

Related Spring and Spring Boot Annotations

Comments