How to choose singleton multiple instances in spring scope

Scope Description Interpretation
singleton (Default) Scopes a single bean definition to a single object instance for each Spring IoC container. When the scope is not marked, the default management of spring is the singleton mode. The singleton pattern here and the singleton pattern in the design pattern are not a concept. The purpose of the singleton pattern of the design pattern is to always ensure that there is only one instance of the class, while the singleton in spring is for all request references. Return the same instance.
prototype Scopes a single bean definition to any number of object instances. This mode is more accurately translated into multiple cases mode here. This mode will return a new instance for all requests.

How to choose singleton multiple instances? If in the program, the instance of the object contains member variables, and its member variables may be changed every time it is called, then multiple cases are required to ensure data security (such as front-end interaction with action).
If we only call the method in the instance, or our call to member variables is consistent (such as obtaining a database connection), then we can use the singleton mode to save space.

Guess you like

Origin blog.csdn.net/weixin_44159662/article/details/110420953