Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of SpringBean scope

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the use of SpringBean scope". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Singleton: singleton mode, Bean defined with singleton will have only one instance in the entire Spring IoC container

Prototype: prototype pattern, each time a Bean defined by prototype is obtained through the container's getBean method, a new Bean instance is generated.

Request: for each HTTP request, the Bean defined using request will generate a new instance, that is, a different Bean instance will be generated for each HTTP request. This scope is valid only if Spring is used in Web applications

Session: for each HTTP Session, a new instance is generated using Bean soy milk defined by session. Again, this scope is valid only if Spring is used in Web applications.

Globalsession: for each global HTTP Session, a new instance of the Bean defined with session will be generated. Typically, it works only when using portlet context. Again, this scope is valid only if Spring is used in Web applications.

Among them, the more commonly used are singleton and prototype scopes. For a singleton-scoped Bean, the same instance will be obtained each time the Bean is requested. The container is responsible for tracking the status of the Bean instance and maintaining the life cycle behavior of the Bean instance. If a Bean is set to prototype scope, each time the program requests the Bean,Spring of the id, it will create a new Bean instance and return it to the program. In this case, the Spring container only uses the new keyword to create the Bean instance. Once created, the container is no longer tracking the instance and does not maintain the state of the Bean instance.

If you do not specify the scope of Bean, Spring uses singleton scope by default. Java needs to apply for memory when creating a Java instance, and garbage collection needs to be completed when the instance is destroyed, which will lead to an increase in system overhead. Therefore, the creation and destruction of prototype scope Bean is expensive. Once the Bean instance of singleton scope is created successfully, it can be reused. Therefore, unless necessary, try to avoid setting Bean to prototype scope.

This is the end of the content of "what is the use of SpringBean scope"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report