Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Spring bean scopes: singleton vs application

Tags:

spring

Could anyone explain the difference between these two Spring bean scopes? I'm familiar with the Singleton pattern.

Would this be the only difference? You can have a list of beans in the Spring container using application scope.

Also, are you able to run multiple web servers in one Spring container? If yes, that would be a reason to use the application scope over the singleton scope since otherwise the bean would get shared over the two servers.

like image 441
Jasper Kattoor Avatar asked Nov 06 '15 16:11

Jasper Kattoor


People also ask

What is the difference between Spring singleton and Spring application scope?

In application scope, container creates one instance per web application runtime. It is almost similar to singleton scope, with only two differences i.e. application scoped bean is singleton per ServletContext , whereas singleton scoped bean is singleton per ApplicationContext .

What is correct about singleton and application bean scopes?

A request can be made to the bean instance either programmatically using getBean() method or by XML for Dependency Injection of secondary type. Generally, we use the prototype scope for all beans that are stateful, while the singleton scope is used for the stateless beans.

Are the Spring beans with singleton scope thread safe?

Singleton beans does not provide thread safety and now you know that instance variables usage may lead to unexpected result, you have 2 options to solve the same : Don't use instance variables in multithreaded environment.

Why Spring bean scope is singleton by default?

When a bean is a singleton, only one shared instance of the bean will be managed and all requests for beans with an id or ids matching that bean definition will result in that one specific bean instance being returned. Only when you have to keep some session details you should use for example session scope.


2 Answers

The documentation explains it:

This is somewhat similar to a Spring singleton bean but differs in two important ways: It is a singleton per ServletContext, not per Spring 'ApplicationContext' (or which there may be several in any given web application), and it is actually exposed and therefore visible as a ServletContext attribute

like image 106
JB Nizet Avatar answered Sep 25 '22 07:09

JB Nizet


In application scope, the container creates one instance per web application runtime. The application scope is almost similar to singleton scope. So, the difference is

Application scoped bean is singleton per ServletContext however singleton scoped bean is singleton per ApplicationContext. It means that there can be multiple application contexts for single application.

SINGLETON SCOPED BEAN

//load the spring configuration file
ClassPathXmlApplicationContext context =
        new ClassPathXmlApplicationContext("context.xml");

// retrieve bean from spring container
MyBean myBean = context.getBean("myBean", MyBean.class);
MyBean myBean2 = context.getBean("myBean", MyBean.class);

// myBean == myBean2 - output is true.
like image 23
Murad Hajiyev Avatar answered Sep 23 '22 07:09

Murad Hajiyev