简体   繁体   中英

understanding of spring bean scopes in a web application

As per springsource documentation a singleton scoped bean is instantiated only once per
container. For example I have a singleton scoped UserDetails bean which contains information
about a user.
In my main() method:

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.

NOW here is my problem:
For my web application I have the following UserDetails bean in my dispatcher-servlet.xml.

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

first question: is singleton scope is the default for a web application too ?
IF YES:
This bean is autowired into AccountService and CustomerService classes.
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,

second question: does it get the same instance of UserDetails with "UserA" as the first name ?
third question: In the mean time if another client say clientB tries to get the first name in AccountService class will it get "UserA" ?
fourth question: will the same UserDetails instance be shared by clientA, clientB and others ? 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.

THANKS

Yes singleton is the default scope for web applications. So you get the same instance of UserDetails in all your services (and for all your users).

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
  • 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. 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.

But if bean A is being used by another Client B then Client A has to wait till Client B releases the bean A.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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