Do you know why Spring designs beans as singletons 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:

For more information, please see the official document introduction, which is very detailed:

https://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s04.html

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.

1. Drawing analysis

2. Source code analysis

When generating the bean, first determine whether the singleton or the prototype is

If it is a singleton, try to get it from the cache first, and not create a new one

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 prototype beans will not have such problems (but there are exceptions, such as being dependent on singleton beans) , Because a new instance is created for each request. I am preparing to write an article on this aspect. Interested friends can follow me in the process of collating, and I will write a detailed article later.

to sum up

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

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

  1. Create fewer instances

  2. Garbage collection

  3. Cache fast 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/bjmsb/article/details/108503557