Junior — Middle
What is the scope of components marked with the @Component annotation, and how is it determined by default?
sobes.tech AI
Answer from AI
In the Spring Framework, components annotated with @Component are, by default, singleton scoped. This means that a single instance of such a component is created in the application context, and this instance is reused when dependencies are injected.
The scope of a component determines how many instances of that component are created and how long they live.
By default:
@Componentcreates a singleton bean.
If you need to change the scope, you can use the @Scope annotation, for example:
@Component
@Scope("prototype")
public class MyComponent {
// a new instance is created for each request
}
Thus, without explicitly specifying the scope, @Component components are singletons and live for the entire duration of the Spring context.