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 are the contents of SpringBean scope management

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what is the content of SpringBean scope management". 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!

I. Preface

When you create a BeanDefinition, you create a recipe for creating an instance of the class defined by BeanDefinition. The idea that BeanDefinition is a recipe is important because it means that, as with classes, multiple object instances can be created through a single recipe.

It has the following advantages:

You can control the various dependencies and configuration values to be inserted into objects created from a specific BeanDefinition

You can control the scope of objects created from a specific BeanDefinition.

This approach is powerful and flexible because developers can choose to configure the scope of objects created without having to consider the scope of objects at the Java class level.

Of course, as a flexible framework, Spring also allows developers to create custom scopes.

1.1 describe the scope of singleton in detail?

Only one shared instance of a singleton bean is managed, and all requests for a bean with an ID or a matching ID for that BeanDefinition will cause that particular bean instance to be returned by the Spring container.

In other words, when we define a BeanDefinition and its scope is singleton, the IoC container will create an instance of the object defined by that BeanDefinition. The singleton instance is stored in the cache of such a singleton bean, and all subsequent requests and references to the named bean return the cached object.

What is the difference between 1.2 and the singleton model?

Spring's concept of singleton bean is not the same as the singleton pattern defined in the legendary book Gang of Four (GoF) patterns created by the Gang of four.

GoF's singleton pattern hardcodes the scope of objects so that each class loader can only create a unique instance of a particular class.

Therefore, it is most appropriate to describe the scope of a Spring singleton as a container corresponding to a bean. If we define a bean for a particular class in a single Spring container, the Spring container will create one and only one instance of the class defined by that BeanDefinition.

The singleton scope is the default scope in Spring. To define bean as a singleton in XML, you can define bean, as shown in the following example:

1.3 Prototype scope

The non-single prototype scope of Bean deployment creates a new bean instance each time a specific bean is requested. That is, the Bean is injected into another Bean, or you can request it through the getBean () method call on the container. In general, you should use the prototype scope for all stateful Bean and the singleton scope for stateless Bean.

In Spring, the objects that make up the body of the application and are managed by the Spring IOC container are called bean.

In a nutshell, bean is an object initialized, assembled, and managed by the IOC container. Apart from that, bean is no different from other objects in the application.

The definition of bean and the dependence of bean on each other will be described by configuration metadata.

Bean in Spring is singleton by default. How can these singleton Bean ensure thread safety in multithreaded programs?

For example, for Web applications, the Web container creates a separate Sevlet thread for each user request to process the request. After the introduction of the Spring framework, each Action is a singleton, so for the singleton Service Bean hosted by Spring, how to ensure its security?

The singleton of Spring is based on BeanFactory, that is, the Spring container. The singleton of Bean in this container is only one Java based on JVM, and there is only one instance in each JVM.

Second, the scope of bean

To create a bean definition, the essence is to use the corresponding class of the bean definition to create a "recipe" for a real instance.

It makes sense to think of the bean definition as a recipe, which is similar to class in that multiple instances can be created based on a single "prescription."

You can control not only the various dependencies and configuration values that are injected into the object, but also the scope of the object.

This gives you the flexibility to choose the scope of the object you build without having to define the scope at the Java Class level.

Spring Framework supports five scopes, as described in the table below.

Of the five scopes, request, session and global session are only used in web-based applications (regardless of what web application framework you use), and can only be used in web-based Spring ApplicationContext environments.

3. Singleton-- the only bean instance

When the scope of a bean is singleton, there will be only one shared bean instance in the Spring IoC container, and all requests for bean will only return the same instance of bean as long as the id matches the bean definition.

Singleton is a singleton type (corresponding to the singleton mode), that is, a bean object is automatically created when the container is created, regardless of whether you use it or not, but we can specify the lazy-init= "true" of the Bean node to delay initializing bean. In this case, the bean will be initialized only when the bean is obtained for the first time, that is, when the bean is requested for the first time.

Each time the object is obtained is the same object. Note that the singleton scope is the default scope in Spring. To define bean as singleton in XML, you can configure:

You can also use the @ Scope annotation, which shows the scope of the specified bean. )

@ Service@Scope ("singleton") public class ServiceImpl {} 4. Prototype-- creates a new bean instance for each request

When the scope of a bean is prototype, it means that a bean definition corresponds to multiple object instances.

The bean of the prototype scope causes a new bean instance to be created each time the bean request is injected into another bean, or the container's getBean () method is called programmatically. Prototype is a prototype type that is not instantiated when we create the container, but only creates an object when we get the bean, and the object we get each time is not the same object.

You should use prototype scope for stateful bean and singleton scope for stateless bean.

Define bean as prototype in XML

Or

There is no demonstration through the @ Scope annotation.

5. Request-- every HTTP request will generate a new bean

This bean is only valid within the current HTTP request

Request is only applicable to Web programs. Each HTTP request generates a new bean, and the bean is only valid within the current HTTP request. When the request ends, the life cycle of the object ends.

Bean is defined as request in XML, which can be configured as follows:

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