Those familiar with Spring development know that Spring provides 5 scopes: Singleton, Prototype, Request, Session, and Global Session. \

Here’s a screenshot from the official document, so if you’re interested, check out the differences between the five. Today, the first two of these five are the bean Scope Singleton and Prototype that Spring originally provided.

The official Spring documentation is shown below:

More information can be found in the official documentation, which is very detailed:

Docs. Spring. IO/spring/docs…

The difference between singleton beans and prototype beans

If a bean is declared as a singleton and only one bean is instantiated in the Spring container for multiple requests, the object is shared by subsequent requests and stored in a map. When a request comes in, it is checked from the cache (map) first. If it does, it uses the object directly. If it does not, it instantiates a new object, so this is a singleton. But for prototype beans, new beans are instantiated directly each time a request comes in, with no caching and look-ups from the cache.

1. Drawing and analysis

2. Source code analysis

Determine whether the bean is singleton or stereotype when generating it

If it is a singleton, try to fetch it from the cache first

Conclusion:

  1. Singleton beans are reused only the first time a new bean is created, so objects are not created frequently.
  2. The prototypical bean is created each time

Advantages of singleton beans

There are several performance advantages because new objects are not created every time:

1. Reduce the consumption of newly generated instances

The cost of newly generated instances is twofold. First, Spring generates bean instances through reflection or cglib, both of which are performance intensive operations, and second, allocating memory for objects also involves complex algorithms.

2. Reduce JVM garbage collection

Since no new bean instance is generated for every request, there are fewer objects to recycle naturally.

3. Beans can be obtained quickly

The singleton fetch bean operation is fast because all but the first generation is fetched from the cache.

Disadvantages of singleton beans

A big disadvantage of singleton beans is that they are not thread-safe!! Since all requests share a bean instance, a stateful bean may have problems in concurrent scenarios, whereas a prototypical bean does not (although there are exceptions, such as being dependent on a singleton bean) because a new instance is created for each request. I am preparing to write an article about this aspect. In the process of sorting out, interested friends can follow me, and I will write a detailed article later.

conclusion

Why does Spring design beans as singletons by default?

Answer: To improve performance!! In several ways,

  1. Create fewer instances
  2. The garbage collection
  3. Cache quick fetch

What are the disadvantages of singletons?

Threads are not safe in concurrent environments if they are stateful

From: juejin. Cn/user / 2506542242603479