Can anyone shade some light on the difference between ApplicationScope(JSF) and Singleton(Spring MVC). I have a application written in jsf in which one of the class uses application scope. And while converting to spring I used Singleton scope which I believe slightly equivalent to Application scope. But want to dig in deep to know what actually varies between both in areas such as performance etc.
1 Answer
Both are similar in the sense that, once started they will live on until the application ends (or the class is garbage collected, which you not happen in a typical Java EE application until you undeploy).
- Both are shared instances and you should make sure that they are thread safe.
From Java EE 7 Tutorial
@ApplicationScoped Shared state across all users' interactions with a web application.
From Spring Documentation:
Spring's concept of a singleton bean differs from the Singleton pattern as defined in the Gang of Four (GoF) patterns book. The GoF Singleton hard-codes the scope of an object such that one and only one instance of a particular class is created per ClassLoader. The scope of the Spring singleton is best described as per container and per bean. This means that if you define one bean for a particular class in a single Spring container, then the Spring container creates one and only one instance of the class defined by that bean definition.
So unless you are, for example, using a back end singleton bean to serve multiple web applications (say in a EAR project with multiple web projects - in this case, the Singleton Bean may outlive the Application bean if you stop / undeploy the web-application but not the EAR) or destroy your Spring application context with your web application still running (in this case your Application Bean may outlive your Singleton Bean) both life cycles are very similar.
Anyway, I think that mixing Spring, JSF and DI xml / annotations is not a good idea. This article goes in detail about how to integrate the three (also, how to make it in a way that you can work with only one of the technology for the annotations).
So, @Named @ApplicationScoped + make sure that you get concurrency right = profit! :)