简体   繁体   English

了解Web应用程序中的spring bean范围

[英]understanding of spring bean scopes in a web application

As per springsource documentation a singleton scoped bean is instantiated only once per 根据springsource文档, singleton作用域bean只实例化一次
container. 容器。 For example I have a singleton scoped UserDetails bean which contains information 例如,我有一个包含信息的单例作用域UserDetails bean
about a user. 关于用户。
In my main() method: 在我的main()方法中:

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});  
UserDetails ud1 = (UserDetails)context.getBean("userDetails");  
custA.setAddress("Address set by ud1");  
System.out.println("Address : " + ud1.getAddress());  

UserDetails ud2 = (UserDetails)context.getBean("userDetails");  
System.out.println("Address : " + ud2.getAddress());

The output will be 输出将是

Address set by ud1  
Address set by ud1

Because of userDetails is a singleton bean, the second retrieval by ud2 will give the same result as that of ud1. 由于userDetails是一个单独的bean, ud2的第二次检索将得到与ud2相同的结果。

NOW here is my problem: 现在这是我的问题:
For my web application I have the following UserDetails bean in my dispatcher-servlet.xml. 对于我的Web应用程序,我在dispatcher-servlet.xml.有以下UserDetails bean dispatcher-servlet.xml.

<bean id="userDetails" class="com.mukund.DTO.UserDetails" />  

first question: is singleton scope is the default for a web application too ? 第一个问题:单例范围是否也是Web应用程序的默认范围?
IF YES: 如是:
This bean is autowired into AccountService and CustomerService classes. 此bean自动装入AccountServiceCustomerService类。
If a client say clientA has set the first name of the user to "UserA" in CustomerService class and after some time it retrieves the first name from AccountService class, 如果客户端说clientA已在CustomerService类中将用户的第一个名称设置为“UserA” ,并且一段时间后它从AccountService类中检索第一个名称,

second question: does it get the same instance of UserDetails with "UserA" as the first name ? 第二个问题:它是否以“UserA”作为名字获得UserDetails的相同实例?
third question: In the mean time if another client say clientB tries to get the first name in AccountService class will it get "UserA" ? 第三个问题:同时如果另一个客户端说clientB试图获得AccountService类中的第一个名称,它会得到“UserA”吗?
fourth question: will the same UserDetails instance be shared by clientA, clientB and others ? 第四个问题: clientA,clientB和其他人是否会共享同一个UserDetails实例? If yes: what scope to choose prototype, request or session. 如果是:选择原型,请求或会话的范围。

I hope you understand my point. 我希望你理解我的观点。 Please explain me spring bean scopes with regards to a web application. 请解释一下关于Web应用程序的spring bean范围。

THANKS 谢谢

Yes singleton is the default scope for web applications. 是单例是Web应用程序的默认范围。 So you get the same instance of UserDetails in all your services (and for all your users). 因此,您在所有服务(以及所有用户)中获得了相同的UserDetails实例。

What scope is the right one for you depends on what you exactly want. 适合您的范围取决于您的确切需求。 Do you really want to inject a data transfer object into services? 您真的想将数据传输对象注入服务吗? How long should the object exist? 物体存在多久?

  • Prototype scope: Each service gets its own UserDetails object 原型范围:每个服务都有自己的UserDetails对象
  • Request scope: You get same instance for the time of the request 请求范围:您在请求时获得相同的实例
  • Session scope: You get the same instance as long as you are in the same session. 会话范围:只要您在同一会话中,就可以获得相同的实例。

By Default the scope of spring beans is singleton that means one instance per container. 默认情况下,spring bean的范围是singleton,表示每个容器一个实例。 But that doesn't mean that the same instance is used by all requests. 但这并不意味着所有请求都使用相同的实例。

It works like this. 它的工作原理如下。

Client A requests for bean A, the container will look for the instance of that bean A, if the instance is not available it will create an instance and then will give it to client A. 客户端A请求bean A,容器将查找该Bean A的实例,如果该实例不可用,它将创建一个实例,然后将其提供给客户端A.

But if bean A is being used by another Client B then Client A has to wait till Client B releases the bean A. 但是如果另一个客户端B正在使用bean A,那么客户端A必须等到客户端B释放bean A.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM