What is the purpose of the @Component annotation in Spring?
A) To mark a Java class as a Spring Bean
B) To configure a database connection
C) To define a transaction boundary
D) To set up a REST controller
Answer:
A) To mark a Java class as a Spring Bean
Explanation:
The @Component
annotation in Spring is used to mark a Java class as a Spring Bean. When a class is annotated with @Component
, it becomes a candidate for component scanning, meaning that Spring will automatically detect and manage this class as a bean in the Spring IoC container.
Classes annotated with @Component
are typically service classes, DAO classes, or any other class that should be managed by Spring. This annotation is part of Spring’s stereotype annotations, which also include @Service
, @Repository
, and @Controller
, each serving specific roles within the application.
Reference links:
https://www.javaguides.net/p/spring-tutorial-beginners-to-expert.html