Junior — Middle
What is the role of the @Component annotation in the Spring Framework and when is it used?
sobes.tech AI
Answer from AI
The @Component annotation in the Spring Framework is used to mark a class as a component managed by the Spring container. This allows Spring to automatically detect such classes during scanning and create their instances (beans) for dependency injection.
@Component is used in cases where the class does not fit more specialized stereotypes such as @Service, @Repository, or @Controller. It is a general way to register any class as a Spring bean.
Example:
@Component
public class MyComponent {
// component logic
}
Thus, @Component helps organize and manage dependencies in a Spring application.