Analysis of the reason why Spring design bean as singleton by default

Those who are familiar with Spring development know that Spring provides five scopes: singleton, prototype, request, session, and global session.

The following figure is a screenshot from the official document. Interested friends can go in and see the difference between these five types. Today I will introduce the first two of these five, which are also the bean scope singleton and prototype originally provided by Spring.

The official Spring document introduces the following picture:
Insert picture description here

The difference between singleton bean and prototype bean

If a bean is declared as a singleton, only one bean is instantiated in the Spring container when multiple requests are processed, and subsequent requests share this object, and this object will be stored in a map. When there is a request, it will first check whether it is in the cache (map), if it does, use this object directly, if not, it will instantiate a new object, so this is a singleton. But for a prototype bean, a new bean is instantiated directly every time a request comes, and there is no process of caching and checking from the cache.

2. Source code analysis

When generating a bean, first determine whether
Insert picture description here
it is a singleton or a prototype. If it is a singleton, try to get it from the cache first, and not create a new one.
Insert picture description here

in conclusion:

1. A singleton bean will reuse the bean only when a new bean is created for the first time, so objects are not created frequently.
2. The prototype bean will be newly created every time

Advantages of singleton beans

Since new objects are not created every time, there are several performance advantages:

1. Reduce the consumption of newly generated instances

The consumption of newly generated instances includes two aspects. First, spring will generate bean instances through reflection or cglib. These are all performance-consuming operations. Secondly, allocating memory to objects also involves complex algorithms.

2. Reduce jvm garbage collection

Since a new bean instance is not generated for every request, fewer objects are naturally recycled.

3. Beans can be obtained quickly

Because the singleton get bean operation except for the first generation is obtained from the cache, it is very fast.

Disadvantages of singleton beans

A big disadvantage of a singleton bean is that it cannot be thread-safe! ! ! Since all requests share a bean instance, if the bean is a stateful bean, there may be problems in concurrency scenarios, while the prototype bean will not have such problems (but there are exceptions, such as being dependent on a singleton bean) , Because a new instance is created for each request.

to sum up

Why does Spring design a bean as a singleton by default?

Answer: To improve performance! ! ! From several aspects,

Create fewer instances
Garbage collection
Fast cache access What are the
disadvantages of singleton?

If it is stateful, it is not thread safe in a concurrent environment

Guess you like

Origin blog.csdn.net/weixin_46011971/article/details/107369974