Consider a class annotated with @Component in a Spring Boot application. What is the main effect of this annotation?
Think about what Spring does with classes marked as components during startup.
The @Component annotation tells Spring to detect the class during component scanning and create a singleton bean instance. This bean can then be injected into other parts of the application.
Choose the correct syntax to mark a class as a Spring component using the @Component annotation.
Check the correct package for the @Component annotation and Java syntax rules.
The @Component annotation is in the org.springframework.stereotype package. It is used above the class declaration with no parentheses needed unless specifying attributes.
When you annotate a class with @Component in Spring Boot without specifying any scope, what is the default scope of the bean?
Think about how Spring manages beans by default unless told otherwise.
By default, Spring creates beans with singleton scope, meaning one shared instance is used throughout the application context.
Given the following code, why does Spring fail to inject MyService into MyController?
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
@Component
public class MyService {}
@Controller
public class MyController {
private final MyService myService;
public MyController(MyService myService) {
this.myService = myService;
}
}Check if Spring can find the classes during scanning.
If the package where MyService is located is not included in component scanning, Spring will not create the bean and injection will fail. The annotations used are correct and constructor injection is supported.
Which statement best describes the difference between @Component, @Service, and @Repository annotations in Spring Boot?
Think about the purpose and behavior of these annotations beyond just creating beans.
@Component is a general annotation for Spring beans. @Service and @Repository are specialized stereotypes that also help with readability and may add extra behaviors, like exception translation in @Repository.