I am not able to understant that only one bean object is created in spring mvc using dispatcher servlet or a new object gets created with every request?
Controller c
Spring's application context (the bean IoC container, which is responsible for managing bean lifecycle from its instantiation to its destruction) contains bean definitions. These definitions among other attributes contain so called scope
. This scope can have the following values:
singleton
- only one instance of the bean is created during the application lifetimeprototype
- every time someone asks (applicationContext.getBean(...)
) for this bean a new instance is createdYou can have some special scopes as well:
request
- bean lifecycle is bound to the HTTP requestsession
- bean lifecycle is bound to the HTTP sessionYou can even create your own scopes. The default scope for beans is singleton
. So if you don't specify otherwise, the bean is singleton
(single instance per application).
If you are using component-scan
which searches for classes annotated with @Component
-like annotations (e.g. @Controller
), then these classes are automatically registered as bean definitions in application context. The default scope applies to them as well.
If you want to alter scope of these auto-registered beans, you have to use @Scope
annotation. Check its JavaDoc if you are interested in how to use it.
TL;DR Your LoginController
is singleton
.