In this tutorial, we will learn about Spring Stereotype Annotations such as @Component, @Controller, @Repository, and @Service annotations.
Spring Stereotype Annotations
YouTube Video - @Controller, @Repository and @Service
@Component
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
Related Spring and Spring Boot Annotations
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
Comments
Post a Comment
Leave Comment