java - understanding of spring bean scopes in a web application -
as per springsource
documentation singleton
scoped bean instantiated once per
container. example have singleton scoped userdetails
bean contains information
user.
in main() method:
applicationcontext context = new classpathxmlapplicationcontext(new string[] {"spring-customer.xml"}); userdetails ud1 = (userdetails)context.getbean("userdetails"); custa.setaddress("address set ud1"); system.out.println("address : " + ud1.getaddress()); userdetails ud2 = (userdetails)context.getbean("userdetails"); system.out.println("address : " + ud2.getaddress());
the output
address set ud1 address set ud1
because of userdetails singleton bean, second retrieval ud2
give same result of ud1.
now here problem:
web application have following userdetails
bean in dispatcher-servlet.xml.
<bean id="userdetails" class="com.mukund.dto.userdetails" />
first question: singleton scope default web application ?
if yes:
bean autowired accountservice
, customerservice
classes.
if client clienta
has set first name of user "usera" in customerservice class , after time retrieves first name accountservice class,
second question: same instance of userdetails "usera" first name ?
third question: in mean time if client clientb
tries first name in accountservice class "usera" ?
fourth question: same userdetails instance shared clienta, clientb , others ? if yes: scope choose prototype, request or session.
i hope understand point. please explain me spring bean scopes regards web application.
thanks
yes singleton default scope web applications. same instance of userdetails in services (and users).
what scope right 1 depends on want. want inject data transfer object services? how long should object exist?
- prototype scope: each service gets own userdetails object
- request scope: same instance time of request
- session scope: same instance long in same session.
Comments
Post a Comment