Junior — Middle
Can you explain what Singleton is when initializing a Bean in the context of development?
sobes.tech AI
Answer from AI
Singleton when initializing a Bean means that only one instance of this Bean is created within the Spring container for the entire lifecycle of the application. This means that every time this Bean is requested, the same object reference is returned.
This approach is useful for services that should have a single state or manage shared resources.
Example in Spring:
@Service
public class MyService {
// This Bean will be Singleton by default
}
By default, the scope of a Bean in Spring is singleton, meaning Spring creates one instance and reuses it.