springspring-mvcsingletonapplicationcontextweb-application-design

Will a new instance of Singleton scoped object in a WebApplicationContext be created if the application is closed and started again in Spring MVC


In a Spring MVC app, will a new instance of Singleton Class in WebApplicationContext ,be created if the application is closed and then started again?

Say there is a singleton scoped DAO class with some member variables(states) that were modified in the application and then the application was closed. Now when we run that app again, would the previous changes(made before the application was closed) be still there for that DAO or it will be a fresh singleton instance when the app restarts ?


Solution

  • Fresh singleton instance.

    Assuming that by "Singleton" you mean bean scope:

    ApplicationContext "lives" only when application lives. When application is stopped, ApplicationContext dies, and loses all beans with all their variables values.

    DAO pattern concerns creating interface for communication with data source (in persistence layer, by using e.g. EntityManager, configured properly with metadata stored in e.g. application.properties). If you have a domain object (object, which "represents" database record), that have been modified inside application and not saved somewhere externally (e.g. in database), than it is lost, when application stops.